我有一个 PDF 作为 base64 字符串,我需要使用 Python 将它写入文件。我试过这个:
import base64
base64String = "data:application/pdf;base64,JVBERi0xLjQKJeHp69MKMSAwIG9iago8PC9Qcm9kdWNlciAoU2tpYS9..."
with open('temp.pdf', 'wb') as theFile:
theFile.write(base64.b64decode(base64String))
但它没有创建有效的 PDF 文件。我错过了什么?
原文由 Rafael Miller 发布,翻译遵循 CC BY-SA 4.0 许可协议
根据我的理解,base64decode 只接受一个 base64 字符串,看起来你的字符串上有一些未编码的标头。
我会删除“data:application/pdf;base64,”
在此处查看文档: https ://docs.python.org/2/library/base64.html
当我过去使用它时,我只使用编码字符串。