سؤال

I'm trying to merge two pdfs with pyPdf library using python and google app engine. I read the files from blobstore and I create PdfFileWriter object with the information that I need but then I'm having troubles to convert this PdfFileWriter in a blobstore file. Any idea to solve it? Thank you :)

Here is my code:

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)
هل كانت مفيدة؟

المحلول

you may try to output your data into the string buffer, and then write the buffer into the blob file:

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() )

then finalize and do the usual stuff.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top