【问题标题】:Python:Conversion of PDF to blob and back to pdf leads to corruptPython:将 PDF 转换为 blob 并返回 pdf 会导致损坏
【发布时间】:2018-06-25 12:54:45
【问题描述】:

我正在测试我存储 PDF 数据块的 nosql 数据库设置的场景之一。以后,如果它可以检索原始的pdf文档,就想使用它。只是为了测试它,我用 Python 写了一个小测试代码。

import base64
with open('test.pdf', 'rb') as f:
 blob = base64.b64encode(f.read())

text_file = open('test_blob.txt', "w")
text_file.write(blob)
text_file.close()   

with open('test_blob.txt', 'r') as f:
  blob=f.read().decode('base64')
text_file = open('result.pdf', "w")
text_file.write(blob)
text_file.close()

当我查看 result.pdf 时,它已损坏。可能是什么问题?

【问题讨论】:

    标签: python base64 blob


    【解决方案1】:

    我尝试了您提供的相同代码,但不幸的是,我在解码部分出错。我稍微修改了代码。请试试这个,让我知道它是否有效。

    import base64
    with open('path/sample.pdf', 'rb') as f:
        blob = base64.b64encode(f.read())
    text_file = open('test_blob.txt', "wb")
    text_file.write(blob)
    text_file.close()
    with open('test_blob.txt', 'r') as f:
        blob=f.read()
    blob = base64.b64decode(blob)
    text_file = open('result.pdf','wb')
    text_file.write(blob)
    text_file.close()
    

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 2014-11-12
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多