The PDF file format is a ubiquitous file format and the only real way to guarantee document layout and display, no matter the device or operating system used to view it . Python, being a language that excels at data processing and manipulation, naturally has a lot of tools for working with PDF files to generate reports. This post is a brief introduction to the tools that are available and the tasks they work best for.
Wand is a set of Python bindings for do-it-all ImageMagick library. As such it really a Swiss-army knife for PDF (and image) manipulation and generation. In the context of PDF, ImageMagick (and therefore Wand) is mostly a frontend for GhostScript.
Its API is quite Pythonic and can help you split, convert, and even draw on PDF files. Wand is perfect for manipulating existing PDFs and doing light editing on them.
ReportLab is a commercial/open-source PDF generation library. If you’re making complex PDFs from scratch this is the library you should pick. Period.
PikePDF is a PDF manipulation library based on qpdf. As its underpinnings are written in C++ it’s super fast. Unlike other python pdf libraries, PikePDF supports unlocking encrypted / password protected PDFs.
PikePDF also lets you explore the internal structure of a PDF document, which make it a perfect tool for debugging problem PDFs and extracting content/images.
The APIs are very Pythonic, and let you work with manipulate pages / page order like a regular list. PikePDF is also great for repairing and prepping PDF to be manipulated by Wand.
Pillow is a PDF library, but it supports exporting images to PDF, making it a powerful tool in your Python PDF toolbox.
While there’s some overlap of functionality Wand in terms of image editing capabilities, it’s more focused on editing images, rather than transforming and converting images. Pillow is an indispensable tool when working with any user-supplied images in your PDF workflow.
There is no single PDF tool in Python that fits all needs, all the time. These libraries can, and are, often used in conjunction with one another to quickly and easily generate PDFs on demand. However, depending on how you combine them, there can be some issues resulting in large file sizes and slow generation times. I’ll cover these gotchas in future posts.