[reportlab-users] Combining reportlab's PDF and pypdf to partially create, partially copy?

Yoann Roman yroman-reportlab at altalang.com
Fri Apr 17 11:01:17 EDT 2009



> I was afraid people would say "It's not possible". Rats. I was

> trying to get lazy, build up the table of contents programmatically,

> and then just include each further section without any further ado.

> I guess I'll have to build the whole document programmatically and

> give up on pypdf.


We've used pyPdf to merge ReportLab's output with pre-existing PDFs
without saving anything to disk by just using a StringIO buffer as a
temporary destination for ReportLab's output. Taking your example:

class GetPDFPage(AllPages):

def get(self):
self.response.headers['Content-Type'] = 'application/pdf'

outputPDF = PdfFileWriter()

report_buffer = cStringIO.StringIO()
p = Canvas(report_buffer)
p.drawString(100, 750, "Hey, it's easy!.")
p.save()

pdfInput = PdfFileReader(cStringIO.StringIO(report_buffer.getvalue()))
outputPDF.addPage(pdfInput.getPage(0))
report_buffer.close()

pdfList = glob.glob( '*.pdf')
for chapPdf in pdfList:
pdfInput = PdfFileReader(file(chapPdf, "rb"))
for i in xrange(pdfInput.getNumPages()):
outputPDF.addPage(pdfInput.getPage(i))

outputPDF.write(self.response.out)

I haven't tested this, but that's the basic gist of what we do.

HTH,

--
Yoann Roman



More information about the reportlab-users mailing list