matzahboy matzahboy - 1 month ago 8
Python Question

Using python to convert PNG's into PDF's

I want to write a python script to convert PNG's into 2-page pdfs (i.e. 2 PNGs per PDF). The software needs to run on both a Mac and Windows 7.

My current solution is using ReportLab, but that doesn't install easily on a Mac. According to its website, it only has a compiled version for Windows. It has a cross-platform version that requires a C compiler to install.

Is there a better way to do this (so that I don't have to install a C compiler on the Mac)? Should I use a different library, or a different language entirely? As long as I can call the program from a python script, I could use any language for the pdf creation. Or, is there a very straightforward (i.e a non-programmer could install it) C compiler that I could install on a Mac?

What do you recommend?

Answer

The unix program convert can help you for conversion.

convert file.png file.pdf

But you said you want to have it under windows too. PIL imaging library has a PDF module. you should try a simple

pilconvert file.png file.pdf

to put more than one image you can play with the library which is quite flexible for resizing, stitching, etc. It is also available for mac and windows

Update 2011-02-15

On my macosx, I have installed reportlab without difficulties.

% sudo easy_install reportlab
Searching for reportlab
Reading http://pypi.python.org/simple/reportlab/
Reading http://www.reportlab.com/
Best match: reportlab 2.5
Downloading http://pypi.python.org/packages/source/r/reportlab/reportlab-2.5.tar.gz#md5=cdf8b87a6cf1501de1b0a8d341a217d3
Processing reportlab-2.5.tar.gz

So you could use a combination of PIL and Reportlab for your own needs.

Comments