Merge Existing PDF into new ReportLab PDF via flowables

reportlab documentation
reportlab drawimage
reportlab new page
reportlab flowable
reportlab open existing pdf
reportlab add paragraph to canvas
reportlab django
reportlab html to pdf

I have a reportlab SimpleDocTemplate and returning it as a dynamic PDF. I am generating it's content based on some Django model metadata. Here's my template setup:

buff = StringIO()
doc = SimpleDocTemplate(buff, pagesize=letter,
                        rightMargin=72,leftMargin=72,
                        topMargin=72,bottomMargin=18)
Story = []

I can easily add textual metadata from the Entry model into the Story list to be built later:

    ptext = '<font size=20>%s</font>' % entry.title.title()
    paragraph = Paragraph(ptext, custom_styles["Custom"])
    Story.append(paragraph)

And then generate the PDF to be returned in the response by calling build on the 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 on the model is a file attachment. When those file attachments are PDFs, I'd like to merge them into the Story that I am generating; IE meaning a PDF of reportlab "flowable" type.

I'm attempting to do so using pdfrw, but haven't had any luck. Ideally I'd love to just call:

from pdfrw import PdfReader
pdf = pPdfReader(entry.document.file.path)
Story.append(pdf)

and append the pdf to the existing Story list to be included in the generation of the final document, as noted above.

Anyone have any ideas? I tried something similar using pagexobj to create the pdf, 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 didn't have any luck either. Can someone explain to me the best way to merge an existing PDF file into a reportlab flowable? I'm no good with this stuff and have been banging my head on pdf-generation for days now. :) Any direction greatly appreciated!

I just had a similar task in a project. I used reportlab (open source version) to generate pdf files and pyPDF to facilitate the merge. My requirements were slightly different in that I just needed one page from each attachment, but I'm sure this is probably close enough for you to get the general idea.

from pyPdf import PdfFileReader, PdfFileWriter

def create_merged_pdf(user):
    basepath = settings.MEDIA_ROOT + "/"
    # following block calls the function that uses reportlab to generate a pdf
    coversheet_path = basepath + "%s_%s_cover_%s.pdf" %(user.first_name, user.last_name, datetime.now().strftime("%f"))
    create_cover_sheet(coversheet_path, user, user.performancereview_set.all())

    # now user the cover sheet and all of the performance reviews to create a merged pdf
    merged_path = basepath + "%s_%s_merged_%s.pdf" %(user.first_name, user.last_name, datetime.now().strftime("%f"))

    # for merged file result
    output = PdfFileWriter()

    # for each pdf file to add, open in a PdfFileReader object and add page to output
    cover_pdf = PdfFileReader(file( coversheet_path, "rb"))
    output.addPage(cover_pdf.getPage(0))

    # iterate through attached files and merge.  I only needed the first page, YMMV
    for review in user.performancereview_set.all():
        review_pdf = PdfFileReader(file(review.pdf_file.file.name, "rb"))
        output.addPage(review_pdf.getPage(0)) # only first page of attachment

    # write out the merged file
    outputStream = file(merged_path, "wb")
    output.write(outputStream)
    outputStream.close()

[PDF] ReportLab PDF Generation User Guide, Chapter 4 Exposing PDF Special Capabilities Chapter 10 Writing your own Flowable Objects when you advance to a new page in pdfgen. chemicals is more difficult than combining light there is a fourth parameter for� RML is the fastest and easiest way to develop rich PDF workflows. You work in a markup language at a sim-ilar level to HTML, using your favorite templating system to populate an RML document; then call our rml2pdf API function to generate a PDF. It's what ReportLab staff use to build all of the solutions you can see on reportlab.com. Key

I used the following class to solve my issue. It inserts the PDFs as vector PDF images. It works great because I needed to have a table of contents. The flowable object allowed the built in TOC functionality to work like a charm.

Is there a matplotlib flowable for ReportLab?

Note: If you have multiple pages in the file, you have to modify the class slightly. The sample class is designed to just read the first page of the PDF.

pdf - Python PyPDF2 merge rotated pages, python - Merge Existing PDF into new ReportLab PDF via flowables - Sta python django pdf pdf-generation reportlab. Recently I needed the ability to use Reportlab’s flowables, but place them in fixed locations. Some of you are probably wondering why I would want to do that. The nice thing about flowables, like the Paragraph, is that they’re easily styled. If I could bold something or center something AND put it in a fixed … Continue reading Reportlab: Mixing Fixed Content and Flowables →

Use this custom flowable:

class PDF_Flowable(Flowable):
#----------------------------------------------------------------------
def __init__(self,P,page_no):
    Flowable.__init__(self)
    self.P = P
    self.page_no = page_no
#----------------------------------------------------------------------
def draw(self):
    """
    draw the line
    """
    canv = self.canv
    pages = self.P
    page_no = self.page_no
    canv.translate(x, y)  
    canv.doForm(makerl(canv, pages[page_no]))
    canv.restoreState()

and then after opening existing pdf i.e.

    pages = PdfReader(BASE_DIR + "/out3.pdf").pages
    pages = [pagexobj(x) for x in pages]
    for i in range(0, len(pages)):
        F = PDF_Flowable(pages,i)
        elements.append(F)
        elements.append(PageBreak())

use this code to add this custom flowable in elements[].

Generating pdfs with ReportLab, There are a few Python libraries for pdf creation (PyPDF2, pdfkit, The downside to ReportLab is that it was founded in 2000 Now that we have flowables, let's build! starts a new page, something that might be useful for a� merger('pdf_merger2.pdf', paths) Here we just need to create the PdfFileMerger object and then loop through the PDF paths, appending them to our merging object. PyPDF2 will automatically append the entire document so you don’t need to loop through all the pages of each document yourself. Then we just write it out to disk.

Reportlab: How to Combine Static Content and Multipage Tables, The coord method is something fun I found on StackOverflow that helps a lot in positioning flowables on� The problem is that mixing Reportlab’s canvas object with flowables can be messy. Reportlab talks a little about using templates in its user guide, but it only shows how to add header and footer type information. That’s actually all the information I needed, but it took me quite a while to realize that.

Reportlab: Mixing Fixed Content and Flowables, I hope this code will help you in your PDF creation endeavors. I have to admit that I'm posting it on here� It's an XML dialect which, combined with a python module which converts the XML files into PDF. Because RML files are just plain text, and Python runs on a plethora of platforms, it's very easy to plug RML into your existing solution or build something new using your favourite language. Using RML inside a web framework

Arcpy Creating Multi-page Table/PDF with Reportlab/PyPDF2 , I had a similar problem four years ago and worked through it as� The simplest method is to use File->New Document, and choose the option to Combine Files into a Single PDF. A file-list box will open. Drag into it the files that you want to combine into a single

Comments
  • I think you can do this with the paid version of ReportLab.
  • Ugh, I don't think the paid version of ReportLab is an option for me, unfortunately. :( Anyone have any alternatives?