Skip to content Skip to sidebar Skip to footer

How To Add Total Page Count In Pdf Using Reportlab

def analysis_report(request): response = HttpResponse(mimetype='application/pdf') response['Content-Disposition'] = 'attachment;filename=ANALYSIS_REPORT.pdf'

Solution 1:

Here is the improved recipe http://code.activestate.com/recipes/576832/ which should work with images.

Solution 2:

Another possible workaround would be to use pyPDF (or any other pdf-lib with the funcionality) to read the total number of pages after doc.build() and then rebuild the story with the gathered information by exchanging the corresponding Paragraph()'s. This approach might be more hackish, but does the trick with no subclassing.

Example:

from pyPdf import PdfFileReader
[...]
story.append(Paragraph('temp paragraph. this will be exchanged with the total page number'))
post_story = story[:] #copy the story because build consumes it
doc.build(story) #build the pdf with name temp.pdf
temp_pdf = PdfFileReader(file("temp.pdf", "rb"))
total_pages = cert_temp.getNumPages()
post_story[-1] = Paragraph('total pages: ' + str(total_pages))
doc.build(post_story)

Post a Comment for "How To Add Total Page Count In Pdf Using Reportlab"