I recently had the need to convert several scanned images into one multi-page PDF. While there are probably tools to help do this manually, I knew that there was a good chance I’d have to do something like this again, quite possibly with a large number of images, so I did what any good geek would do: I scripted it. In this entry, I’ll show how I went about that.
For starters, let’s take a look at the very small, simple Python script:
There’s not a lot to it, thanks in large part to PythonMagick and pyPDF. This script takes at least two parameters: the final name of the PDF, and at least on image file. The bulk of the work flow is this:
Create a PdfFileWriter object. This handles the heavy lifting in actually writing the PDF
Iterate over the image file names given
Create an Image object and read the image source into it
Write the image to a temporary PDF file. This implicitly converts the image to a PDF.
Read the temporary PDF into memory via PdfFileReader
For each page in the temporary PDF (which should be exactly 1), add it to the real, final PDF
Delete the temporary PDF
Write the newly constructed PDF to disk and exit
It’s very simple, and pretty dumb (I added only enough error checking to make it work for me ;), and it may be a suboptimal use of the APIs, but it works pretty well for me. Hopefully, it will help someone else out.