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
-repage a4
I get ainvalid argument for option '-repage': a4
– Scolytus Mar 07 '14 at 11:36-repage
does not support the a4 name anymore. I've worked around this via shell arithmetic:-repage $((150*8.27))x$((150*11.69))
– maxschlepzig Mar 07 '14 at 15:17-density 150
argument was important to add . – dma_k Apr 21 '15 at 15:54-density 150x150
(I was trying to convert a JPG). Why is it necessary to include the density, and how do you know what it is, anyway? – Faheem Mitha Aug 03 '15 at 21:26bash: i*8.27: syntax error: invalid arithmetic operator (error token is ".27")
inGNU bash 4.3.11
. I had to use the following form instead:-resize $(echo ${i}*8.27 | bc)x$(echo ${i}*11.69 | bc)
. BTW, the-repage
was also necessary. – Marcus Junius Brutus Aug 24 '16 at 12:04echo $((i*827/100))x$((i*1169/100))
. This saves thebc
calls. I'll update my answer. – maxschlepzig Aug 24 '16 at 18:38