convert images to pdf: How to make PDF Pages same size

Last time I used convert for such a task I explicitly specified the size of the destination via resizing:

$ i=150; convert a.png b.png -compress jpeg -quality 70 \
      -density ${i}x${i} -units PixelsPerInch \
      -resize $((i*827/100))x$((i*1169/100)) \
      -repage $((i*827/100))x$((i*1169/100)) multipage.pdf

The convert command doesn't always use DPI as default density/page format unit, thus we explicitly specify DPI with the -units option (otherwise you may get different results with different versions/input format combinations). The new size (specified via -resize) is the dimension of a DIN A4 page in pixels. The resize argument specifies the maximal page size. What resolution and quality to pick exactly depends on the use case - I selected 150 DPI and average quality to save some space while it doesn't look too bad when printed on paper.

Note that convert by default does not change the aspect ratio with the resize operation:

Resize will fit the image into the requested size. It does NOT fill, the requested box size.

(ImageMagick manual)

Depending on the ImageMagick version and the involved input formats it might be ok to omit the -repage option. But sometimes it is required and without that option the PDF header might contain too small dimensions. In any case, the -repage shouldn't hurt.

The computations use integer arithmetic since bash only supports that. With zsh the expressions can be simplified - i.e. replaced with $((i*8.27))x$((i*11.69)).

Lineart Images

If the PNG files are bi-level (black & white a.k.a lineart) images then the img2pdf tool yields superior results over ImageMagick convert. That means img2pdf is faster and yields smaller PDFs.

Example:

$ img2pdf -o multipage.pdf a.png b.png

or:

$ img2pdf --pagesize A4 -o multipage.pdf a.png b.png

What you really want to use is:

$ convert a.png b.png -compress jpeg -resize 1240x1753 \
                      -extent 1240x1753 -gravity center \
                      -units PixelsPerInch -density 150x150 multipage.pdf

-extent actually extends the image to be 1240x1753, while -resize keeps the image's ratio, fitting it into either 1240x... or ...x1753.

The -gravity parameter is optional but can be used to center the image when extending.


Addition to caugner's answer:

having installed IM v6.6.9-7 i found out the -gravity parameter needs to be placed in between -resize and -extent to have an effect.

additionally (altough not part of the o.p. question) i found setting a different background-color appealing which would result in the total command of

convert in.jpg -resize 1240x1750 -background black -compose Copy\
               -gravity center -extent 1240x1750\
               -units PixelsPerInch -density 150 out.pdf

another useful variation i often use when i don't want to re-scale an image that already comes in the correct aspect-ratio but keep its individual resolution is

convert in.jpg -units PixelsPerInch -set density '%[fx:w/8.27]'\
               -repage a4 out.pdf

where the target density is dynamically determined by calculating the width divided by 8.27 (which is the width in inch of an A4 page). the -repage a4 parameter can be omitted most of the time but i've had a few cases where the resulting .pdf would have a different format sligtly off the A4 dimensions of 210x297mm (8.27x11.6")