Created
July 17, 2013 03:50
-
-
Save imwilsonxu/6017552 to your computer and use it in GitHub Desktop.
PyPdf: How to Write a PDF to Memory
http://www.blog.pythonlibrary.org/2013/07/16/pypdf-how-to-write-a-pdf-to-memory/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyPdf | |
from StringIO import StringIO | |
#---------------------------------------------------------------------- | |
def mergePDFs(pdfOne, pdfTwo): | |
""" | |
Merge PDFs | |
""" | |
tmp = StringIO() | |
output = pyPdf.PdfFileWriter() | |
pdfOne = pyPdf.PdfFileReader(file(pdfOne, "rb")) | |
for page in range(pdfOne.getNumPages()): | |
output.addPage(pdfOne.getPage(page)) | |
pdfTwo = pyPdf.PdfFileReader(file(pdfTwo, "rb")) | |
for page in range(pdfTwo.getNumPages()): | |
output.addPage(pdfTwo.getPage(page)) | |
output.write(tmp) | |
return tmp.getvalue() | |
if __name__ == "__main__": | |
pdfOne = '/path/to/pdf/one' | |
pdfTwo = '/path/to/pdf/two' | |
pdfObj = mergePDFs(pdfOne, pdfTwo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment