How to generate a PDF from a series of images?

I have a series of images (a scanned book, actually) that I want to make into a PDF in A4 format.

Why I'm doing this: I'm helping a less computer savvy person in a different country to get a printed copy of this book. I know they have the ability to print PDFs, but I don't want to go through the hassle of figuring out and trying to explain how to resize and print a series of images. – I just want to email them something they can throw at a printer with a minimum of fuss.


I found a solution which uses ImageMagick, but it took me a while to figure out that I had to use the option -page A4 to generate an A4 formatted PDF. In the end this simple command did the trick for me:

convert *.gif -page A4 book.pdf

Troubleshooting

While googling for an answer I realized that other people also have tried this, and encountered problems which I never saw. These hints may be helpful to some of you.

Images of different sizes: If your images do not all have the same size, have a look at this YouTube video: “How to convert multiple images to A4 pdf using imagemagick”. The guy in the video uses the command (which he states produces A4 output):

convert *.jpg -resize 595x842 -gravity center -background white \
    -extent 595x842 resultimage.pdf

Postamble

This question has been brought up on stackoverflow, but closed as off topic (“How can I convert a series of images to a PDF from the command line on linux?”). As this gave me a hint, but not the full solution I'm creating (and answering) this question here, in the hope that someone else will find it useful, too.


Use img2pdf:

img2pdf --pagesize A4 file1.jpg file2.jpg > book.pdf

From its description:

Losslessly convert raster images to PDF without re-encoding JPEG and JPEG2000 images. This leads to a lossless conversion of JPEG and JPEG2000 images with the only added file size coming from the PDF container itself.

ImageMagick on the other hand will decode and re-encode the JPEG data, resulting in generation loss and performance 10–100 worse than img2pdf.