I have a reportlab SimpleDocTemplate and return it as a dynamic PDF. I create it based on some metadata of the Django model. Here is my template setting:
buff = StringIO() doc = SimpleDocTemplate(buff, pagesize=letter, rightMargin=72,leftMargin=72, topMargin=72,bottomMargin=18) Story = []
I can easily add text metadata from the Entry model to the Story list, which will be built later:
ptext = '<font size=20>%s</font>' % entry.title.title() paragraph = Paragraph(ptext, custom_styles["Custom"]) Story.append(paragraph)
And then generate a PDF that will be returned in response by calling build in SimpleDocTemplate :
doc.build(Story, onFirstPage=entry_page_template, onLaterPages=entry_page_template) pdf = buff.getvalue() resp = HttpResponse(mimetype='application/x-download') resp['Content-Disposition'] = 'attachment;filename=logbook.pdf' resp.write(pdf) return resp
One metadata field in the model is file attachment. When these file attachments are PDF files, I would like to combine them into the History that I generate; IE stands for fluid reportlab PDF file.
I am trying to do this using pdfrw , but no luck. Ideally, I would just like to call:
from pdfrw import PdfReader pdf = pPdfReader(entry.document.file.path) Story.append(pdf)
and add pdf to the existing Story list, which will be included in the final document, as described above.
Does anyone have any idea? I tried something similar, using pagexobj to create a pdf file, trying to follow this example:
http://code.google.com/p/pdfrw/source/browse/trunk/examples/rl1/subset.py
from pdfrw.buildxobj import pagexobj from pdfrw.toreportlab import makerl pdf = pagexobj(PdfReader(entry.document.file.path))
But there was no luck. Can someone explain to me the best way to merge an existing pdf file into reportlab fluid? I'm not very good at it, and the other day I hit my head about pdf format. :) Any direction is much appreciated!