How to Combine a Bunch of Images into a PDF File

I will describe two methods: one using convert and one using pdfjoin.

Method 1 is to use the convert tool from ImageMagick. Advantages: This is simple, and you probably already have ImageMagick installed. Disadvantages: This will fail when you have a lot of large images, because it will try to load them all in memory at the same time.

Basic usage:

convert *.png output.pdf

If that uses all your machine's memory, you can force it to swap to disk when it exceeds a given amount of memory:

convert -limit memory 2GiB -limit map 4GiB *.png output.pdf

If that fills up your /tmp, you can tell it to swap to some other location:

MAGICK_TEMPORARY_PATH="/media/bigdisk" convert -limit memory 2GiB -limit map 4GiB *.png output.pdf

Unfortunately, if you have a lot of large images, swapping to disk is slow, and the command above gave me occasional segmentation faults. But if you only have a few images, convert is the easiest method.

Method 2 is to use the pdfjoin tool from the pdfjam package. Advantages: This works fine in cases where convert runs out of memory. Disadvantages: pdfjam uses LaTeX, and installing it will pull in about 500MB of dependencies.

First you have to convert your images into one-page PDFs:

for i in *.png; do convert $i $(basename $i png)pdf; done

Then you can join those PDFs together into one file:

pdfjoin --fitpaper 'false' --rotateoversize 'false' *.pdf -o output.pdf

This works well, without running out of memory, even if you have hundreds of high-resolution images.