Reportlab: Mixing Fixed Content and Flowables

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 location, then that would rock! It took a lot of Googling and trial and error, but I finally got a decent template put together that I could use for mailings. In this article, I’m going to show you how to do this too.

Getting Started

You’ll need to make sure you have Reportlab or you’ll end up with a whole lot of nothing. You can go here to grab it. While you wait for it to download you can continue reading this article or go do something else productive. Are you ready now? Then let’s get this show on the road!

Now we just need to come up with an example. Fortunately I was working on something at my job that I’ve been able to dummy up into the following silly and incomplete form letter. Study the code closely because you never know when there will be a test.

from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import mm, inch
from reportlab.pdfgen import canvas
from reportlab.platypus import Image, Paragraph, Table


########################################################################
class LetterMaker(object):
    """"""
    
    #----------------------------------------------------------------------
    def __init__(self, pdf_file, org, seconds):
        self.c = canvas.Canvas(pdf_file, pagesize=letter)
        self.styles = getSampleStyleSheet()
        self.width, self.height = letter
        self.organization = org
        self.seconds  = seconds
        
        
    #----------------------------------------------------------------------
    def createDocument(self):
        """"""
        voffset = 65
        
        # create return address
        address = """
        Jack Spratt
222 Ioway Blvd, Suite 100
Galls, TX 75081-4016
""" p = Paragraph(address, self.styles["Normal"]) # add a logo and size it logo = Image("snakehead.jpg") logo.drawHeight = 2*inch logo.drawWidth = 2*inch ## logo.wrapOn(self.c, self.width, self.height) ## logo.drawOn(self.c, *self.coord(140, 60, mm)) ## data = [[p, logo]] table = Table(data, colWidths=4*inch) table.setStyle([("VALIGN", (0,0), (0,0), "TOP")]) table.wrapOn(self.c, self.width, self.height) table.drawOn(self.c, *self.coord(18, 60, mm)) # insert body of letter ptext = "Dear Sir or Madam:" self.createParagraph(ptext, 20, voffset+35) ptext = """ The document you are holding is a set of requirements for your next mission, should you choose to accept it. In any event, this document will self-destruct %s seconds after you read it. Yes, %s can tell when you're done...usually. """ % (self.seconds, self.organization) p = Paragraph(ptext, self.styles["Normal"]) p.wrapOn(self.c, self.width-70, self.height) p.drawOn(self.c, *self.coord(20, voffset+48, mm)) #---------------------------------------------------------------------- def coord(self, x, y, unit=1): """ # http://stackoverflow.com/questions/4726011/wrap-text-in-a-table-reportlab Helper class to help position flowables in Canvas objects """ x, y = x * unit, self.height - y * unit return x, y #---------------------------------------------------------------------- def createParagraph(self, ptext, x, y, style=None): """""" if not style: style = self.styles["Normal"] p = Paragraph(ptext, style=style) p.wrapOn(self.c, self.width, self.height) p.drawOn(self.c, *self.coord(x, y, mm)) #---------------------------------------------------------------------- def savePDF(self): """""" self.c.save() #---------------------------------------------------------------------- if __name__ == "__main__": doc = LetterMaker("example.pdf", "The MVP", 10) doc.createDocument() doc.savePDF()

Now you’ve seen the code, so we’ll spend a little time going over how it works. First off we create a Canvas object that we can use without our LetterMaker class. We also create a styles dict and set up a few other class variables. In the createDocument method, we create a Paragraph (an address) using some HTML-like tags to control the font and line breaking behavior. Then we create a logo and size it before putting both items into a Reportlab Table object. You’ll note that I’ve left in a couple commented out lines that show how to place the logo without the table. We use the coord method to help position the flowable. I found it on StackOverflow and thought it was pretty handy.

The body of the letter uses a little string substitution and puts the result into another Paragraph. We also use a stored offset to help us position things. I find that storing a couple of offsets for certain portions of the code is very helpful. If you use them carefully then you can just change a couple of offsets to move the content around on the document rather than having to edit the position of each element. If you need to draw lines or shapes, you can do them in the usual way with your canvas object.

Wrapping Up

I hope this code will help you in your PDF creation endeavors. I have to admit that I’m posting it on here as much for my own future benefit as for your own. I’m a little sad I had to strip out so much from it, but my organization wouldn’t like it very much if I posted the original. Regardless, you now have the tools to create some pretty fancy PDF documents with Python. Now you just have to get out there and do it!

Further Reading

12 thoughts on “Reportlab: Mixing Fixed Content and Flowables”

  1. Thanks so much for sharing this example! I was trying to do a similar thing (mixing flowables with the canvas so I can draw lines) and found your post, you saved me a lot time. 🙂

  2. Pingback: Mike Driscoll: Reportlab: How to Combine Static Content and Multipage Tables | The Black Velvet Room

  3. Pingback: Reportlab: How to Combine Static Content and Multipage Tables | Hello Linux

  4. your explanation is great, thanks. But, reading this code, seems like it is able to write judst one single PDF page….so what would you do if, for example, you just want an image in page1 and the rest like text, more images etc in pages 2, 3 4…etc??? how do you jump to the next page?

  5. What I do is figure out what’s static on a page and put that into a specific method. Anything that needs to flow across multiple pages needs to go in a flowable, like a Paragraph, Table, etc and added the the flowable list.

  6. Oh well…since I am not very expert using reportlab, I guess I can adapt your code to create some single-page PDF files and after that I will have to merge them somehow into a single PDF with more than 1 page. It could be a good solution

  7. Pingback: Installer ReportLab et utiliser des curseurs d’ArcPy pour extraire des données sous forme de tableau. Les différents essais et leurs résultats (2/2).

  8. Pingback: Reportlab: How to Combine Static Content and Multipage Tables - The Mouse Vs. The Python

  9. Pingback: Reportlab - How to Create Barcodes in Your PDFs with Python - The Mouse Vs. The Python

  10. Pingback: An example of reporting with ArcGIS Pro, ReportLab and ArcPy. – GeoDataSolutions

  11. Pingback: Reportlab - All About Fonts - Mouse Vs Python

Comments are closed.