Wednesday, August 19, 2009

Convert Images to A4 PDF

Converting raster images to PDF in a printable format can be achieved using the ImageMagick convert utility with the page parameter:

convert -page a4 *.png images.pdf


The converter, however, not quite does what I expected. Images are resized to fill the A4 page but the aspect ratio is preserved and no margin is added. This actually leads to different sized pages for images with different ratios (which is common for scanned documents for example).

In order to create equal-sized PDF pages from a bunch of images, a margin or border needs to be added to the images. Doing this manually is a cumbersome process. Therefore, I've written a little Python script which adds a (white) border to the individual images to enforce an aspect ratio compatible with A4 pages. The script creates a PDF file from a bunch of image files with uniform A4 page size:

import sys
from subprocess import Popen, PIPE

PAGE_WIDTH = 210.0
PAGE_HEIGHT = 297.0

files = [arg for arg in sys.argv[1:-1]]
output = sys.argv[-1]
tmp = ["a4%s" % f for f in files]
for f,t in zip(files, tmp):
p = Popen(["identify", f], stdout=PIPE)
dim = p.communicate()[0].split()[2]
w,h = [float(d) for d in dim.split('x')]
bw,bh = 0,0
if w/h < PAGE_WIDTH/PAGE_HEIGHT:
nw = PAGE_WIDTH * h / PAGE_HEIGHT
bw = int((nw - w) / 2)
else:
nh = PAGE_HEIGHT * w / PAGE_WIDTH
bh = int((nh - h) / 2)
Popen(["convert", "-border", "%dx%d" % (bw,bh),
"-bordercolor", "white", f, t]).communicate()
Popen(["convert", "-page", "a4"] + tmp + [output]).communicate()


Save the script to img2a4pdf.py and invoke it like that:

python img2a4pdf.py *.png output.pdf


Maybe someone will find it useful.