Bennett Kanuka bio photo

Bennett Kanuka

Using math and software to solve real world problems

Twitter Google+ LinkedIn Github

img2pdf: Merge and resize images into a PDF

I was recently emailed a bunch of JPEG files of a scanned document. There was a file for every page, and the image files were very large. I wanted to get them all in a single PDF file on letter size paper. Because I find PDF mystical and difficult to work with, I decided to stick to tools I know - and I know LaTeX.

In no time I learned how to use the ImageMagick tool convert to convert to PDF. Assuming that our original files are named page1.jpg, page2.jpg, etc. I could do:

convert page* -format pdf merge.pdf

However, this made a PDF sized to the images, rather than forcing a letter size page.

Instead, I did the following. First, I resized the images to something reasonable using convert (they were insanely large).

convert page* -resize 50% -format jpg page.jpg

This created files page-0.jpg through page-4.jpg with smaller sizes.

Then I wrote the following script, which I called img2pdf which takes images files as arguments and converts to PDF. Save the GitHub gist to a file and mark it as executable. img2pdf -h has usage instructions, but it’s simple enough to figure out.