将二进制解码为pdf [英] Decoding binary to pdf

查看:88
本文介绍了将二进制解码为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个平台,当您将pdf上传到该平台时,该平台会使用Python中的base64编码转换pdf.然后将二进制字符串存储在数据库中.

I'm using a platform that, when you upload pdf's to it, converts the pdf with the base64 encode in Python. Then it stores the binary string in the database.

现在,我想解码字符串并将其写入本地文件夹,因此我想使用"with open"结构并将b参数传递给二进制文件,然后它应根据解码后的字符串创建test.pdf并将其写入我的桌面? 但这没有任何结果,我在这里做错什么了?

Now I want to decode the strings and write them to a local folder, so I thought to use the 'with open' structure and pass it the b parameter for binary and then it should create test.pdf based of my decoded string and write it to my desktop? Yet this yields no results, what am I doing wrong here?

code = "My binary string"

with open('test.pdf', 'wb') as fout:
     fout.write(base64.decode(code, '~/Desktop'))

code = "My binary string"

with open('~/Desktop/test.pdf', 'wb') as fout:
     fout.write(base64.decodestring(code))

数据库中的示例二进制字符串:"65/658e9014babd33786821f3130c5f3a1cc1322ddf" 所以我假设它在'/'标记之后开始?

Example binary string in database: "65/658e9014babd33786821f3130c5f3a1cc1322ddf" So I'm assuming it starts after the '/' mark?

推荐答案

base64.decode(,)将文件作为args.您想尝试

base64.decode(,) takes files as args. you want to try

fout.write(base64.decodestring(code))

尽管您的代码示例未编码. 这是一个工作示例:

though your code example is not encoded. Here's a working example:

#!/usr/bin/python
import base64, os
code = 'TXkgYmluYXJ5IHN0cmluZw==\n'
with open(os.path.expanduser('~/Desktop/test.pdf'), 'wb') as fout:
     fout.write(base64.decodestring(code))

这篇关于将二进制解码为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆