Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I did something like

convert -page A4 -compress A4 *.png CH00.pdf

But the 1st page is much larger than the subsequent pages. This happens even though the image dimensions are similar. These images are scanned & cropped thus may have slight differences in dimensions

I thought -page A4 should fix the size of the pages?

share|improve this question

5 Answers 5

up vote 23 down vote accepted

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

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

Where 1240x1753 are exactly DIN A4 when 150 DPI is chosen. I computed the values using bc and looked up the dimensions in inches in the Wikipedia article. The resize argument specifies the maximal page size.

This assumes that convert by default does not change the aspect ratio with the resize operation - which is the case:

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

(ImageMagick manual)

Thus the -page a4 should be added, such that DIN A4 is specified in the PDF header:

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

Update: Tested it again with another viewer, and it seems that one has to use -repage a4 instead of -page a4 to get the right page information into the resulting PDF.

share|improve this answer
when using -repage a4 I get a invalid argument for option '-repage': a4 –  Scolytus Mar 7 '14 at 11:36
@Scolytus, on a Fedora 19 system I've observed a similar issue - it seems that -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 7 '14 at 15:17
I assume those magic numbers are 150dpi, and A4 expressed in legacy units? –  Michael Scheper Mar 19 at 3:07
@MichaelScheper, yes, dpi and inches. –  maxschlepzig Mar 19 at 9:26

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.

share|improve this answer
Thank you! -extent is really what I want to use :) -- please, add missed left quote in front of -extent, thanks! –  brownian May 17 '13 at 12:18
Thank you, I have eventually added the missing tick! ;-) –  caugner May 28 '13 at 11:22
works nicely! thanks. –  markusN Jan 15 '14 at 0:11

this is meant as a comment to caugners answer (but i'm not having sufficient reputation level):

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")

share|improve this answer

I was struggling with that stuff, too. Based on the above info, I wrote a script which adds alphabetically sorted image files into a single PDF.

Some variables are settable inside the script. It depends on ImageMagick and pdftk.

NB: If the input image has a higher resolution (dpi) than the wanted resolution of output.pdf, the image is resampled to the lower resolution. Otherwise, the image is not resampled and it is only extended to fit the page canvas.


export LOCALE=C

[[ "${2}x" == "x" ]] && \
 { echo "Usage: $( basename $0 ) output.pdf extension"
   echo "       merges all files (*.extension) into a single PDF"
   echo "If files z_merged.pdf, z_temp.pdf or $1 exist, they will be overwritten"
 exit 1
 } || \

# Set basic variables
unset Debug #; Debug="yes" # print extra messages
IMBackground="white"      # what colour for paper
IMQuality="91"            # JPEG compression level
PaperWdthMetr="210"       # milimeters, 210 for ISO A4
PaperHghtMetr="297"       # milimeters, 297 for ISO A4
PaperDens="200"           # maximum (wanted) dpi for a page
PaperWdthInch=$( echo scale=5\; $PaperWdthMetr / 2.54 / 10      | bc -l ) # Inch
PaperHghtInch=$( echo scale=5\; $PaperHghtMetr / 2.54 / 10      | bc -l ) # Inch
PaperRtio=$(     echo scale=5\; $PaperWdthMetr / $PaperHghtMetr | bc -l )

# Remove temporary files from prior run
rm -rf z_merged.pdf z_temp.pdf 2>/dev/null

# Process any $ext file in the current directory
find . -maxdepth 1 -name "*.${ext}" -print0 | sort -z | while read -d '' -r FName
  echo "Converting $FName"
  ImgIdentify=$( identify -format "%w %h" "$FName" )
  ImgWdthOrig=$( echo $ImgIdentify | cut -d" " -f1  )
  ImgHghtOrig=$( echo $ImgIdentify | cut -d" " -f2  )
  ImgRtio=$( echo "scale=5; $ImgWdthOrig / $ImgHghtOrig"  | bc -l )
  [[ $( echo $ImgRtio'>'$PaperRtio | bc -l ) == 1 ]] \
    && ImgDens=$( echo scale=0\; $ImgWdthOrig / $PaperWdthInch | bc -l ) \
    || ImgDens=$( echo scale=0\; $ImgHghtOrig / $PaperHghtInch | bc -l )
  [[ $Debug ]] && echo "ImgDens1: $ImgDens"
  [[ $( echo $ImgDens'>'$PaperDens | bc -l ) == 1 ]] \
    && ImgDens=$PaperDens
  [[ $Debug ]] && echo "ImgDens2: $ImgDens"

  ImgWdth=$( echo $PaperWdthInch \* $ImgDens | bc -l ) # pixels
  ImgHght=$( echo $PaperHghtInch \* $ImgDens | bc -l ) # pixels

  [[ $Debug ]] && echo "ImgWdth: $ImgWdth".
  [[ $Debug ]] && echo "ImgHght: $ImgHght".

  convert "${FName}"                                 \
          -resize ${ImgWdth}x${ImgHght}              \
          -background $IMBackground -gravity center  \
          -extent ${ImgWdth}x${ImgHght}              \
          -units PixelsPerInch -set density $ImgDens \
          -repage ${ImgWdth}x${ImgHght}+0+0          \
          -compress JPEG                             \
          -quality $IMQuality                        \

  # Merge new PDF page with prior pages
  [[ -f z_merged.pdf ]] && \
   { pdftk z_merged.pdf "${FName%.$ext}.pdf" cat output z_temp.pdf
     mv z_temp.pdf z_merged.pdf
   } || \
     cp "${FName%.$ext}.pdf" z_merged.pdf
  [[ $Debug ]] || rm -rf "${FName%.$ext}.pdf"

[[ -f z_merged.pdf ]] && mv z_merged.pdf "$OutName"
echo "Done."
share|improve this answer

I find the following script convenient which combines the answers listed here as well as some problems I had with the floating point calculation:


quoted_args="$(printf " %q" "${@:1:$endInputArgs}")"
output_arg="$(printf " %q" "${@:$#:1}")"

ratiox=$(echo "150*8.27" | bc)
ratioy=$(echo "150*11.69" | bc)

bash -c "convert $quoted_args -compress jpeg -resize 1240x1753 \
  -units PixelsPerInch -density 150x150 -repage ${ratiox}x${ratioy} $output_arg"

The script is called (saved as a file images2pdf)

images2pdf file\ 1.jpg file\ 2.jpg file\ 3.jpg output.pdf
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.