簡體   English   中英

從 PdfFileWriter 創建 blobstore 文件

[英]Create blobstore file from PdfFileWriter

我正在嘗試使用 python 和谷歌應用程序引擎將兩個 pdf 與 pyPdf 庫合並。 我從 blobstore 讀取文件,並使用我需要的信息創建 PdfFileWriter 對象,但隨后我無法將這個 PdfFileWriter 轉換為 blobstore 文件。 有什么想法可以解決嗎? 謝謝 :)

這是我的代碼:

blob_reader1 = blobstore.BlobReader(blob_key1)
blob_reader2 = blobstore.BlobReader(blob_key2)

writer = PdfFileWriter()

input1 = PdfFileReader(blob_reader1)
input2 = PdfFileReader(blob_reader2)

writer.addPage(input1.getPage(0))
writer.addPage(input1.getPage(1))
writer.addPage(input2.getPage(0))


# finally, write "output" to document-output.pdf
# This lines are comment because it is the way to do it without google app engine---
# outputStream = file("document-output.pdf", "wb")
# output.write(outputStream)
# outputStream.close()
#        
# Open the file and write to it
#I do not how write the information are inside writer in blobstore file
file_name = files.blobstore.create(mime_type='application/pdf')    
#with files.open(file_name, 'a') as f:
#    f.write(writer) it does not work

# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)

blob_key = files.blobstore.get_blob_key(file_name)

您可以嘗試將數據輸出到字符串緩沖區,然后將緩沖區寫入 blob 文件:

import StringIO

stream = StringIO.StringIO()
writer.write( stream )    # write PDF content

# Open the file and write to it
with files.open(file_name, 'a') as f:
    f.write( stream.getvalue() )

然后完成並做通常的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM