Explore Books related to Python. Click Here

Python: PDF Creation with pdfdocument

I do a lot of PDF report creation with Python using Reportlab. Occasionally I'll throw PyPDF in as well. So I'm always on the lookout for other mature Python PDF tools. PDFDocument isn't exactly mature, but it's kind of interesting. The PDFDocument project is actually a wrapper for Reportlab. You can get it on github. I found the project easy to use, but pretty limiting. Let's take a few minutes to examine how it works.

Diving into PDFDocument

The best way to learn something new is usually just to try it out. The only exception to this rule is if it's fragile and it cost a lot of money. Then you might want to read the manual. Since that's not the case with open source software, let's just give it a try!

Note: To run this code, you will need to download and install it into your local Python installation or into a virtualenv

from pdfdocument.document import PDFDocument

def createPDF(path):
    Create a simple PDF
    pdf = PDFDocument(path)
    addr = {'first_name':"John", 'last_name':"Hanes",
            'address':"123 Ding Dong Lane", 
            'zip_code':"75002", 'city':"Dakota"}
    pdf.p("This is a paragraph!")

if __name__ == "__main__":

If all goes well, you should end up with a PDF document that looks something like the following:


You can download the PDF itself here: test.pdf.

You may notice that for some reason PDFDocument is putting the zip code before the city. You will also note that you cannot add a state value, so you may have difficulty mailing this document. All of this is oddly hard-coded into the package. You can see it in the latest version (as of 10/17/2013) on github in the address method, which is near the end of class. I have to create a lot of reports that require additional address lines as well, which this also won't support. So just an FYI: if you do a lot of mailing with Python reports, this project probably won't work for you.

According to the documentation, you can use PDFDocument to create letters and reports via PDF templates. The usage looks like this:

# Or:

There is also support for a "confidential" report that includes a red cross at the top and a watermark. There is also some special wrapping to make style changes easier, such as setting font attributes like bold, heading1, small, etc. Finally, there are some helper methods to make Django integration easier.

Wrapping Up

While I don't find this project very useful, you might be able to find some uses for it in your own job or hobby project. Or you might find one of the projects in the related articles below useful to you.

Related Articles

Copyright © 2022 Mouse Vs Python | Powered by Pythonlibrary