ImageMagick: convert image to PDF with A4 page size and image fit to page

58,546

Solution 1

Thanks to the ImageMagick support forum I was able to find a solution:

convert image.tif -resize 575x823^> -gravity center -background white -extent 595x842 image.pdf

If you get an error try:

convert image.tif -resize 595x842^\> -gravity center -background white -extent 595x842 image.pdf

Solution 2

If you want to keep the original resolution (lossless) you can try the following command:

convert png.png -background white -page a4 myoutput.pdf

Based on a comment posted before: https://stackoverflow.com/a/24573341/6747994

@m4tx This command only makes sense if the picture has a resolution above 500x800px, it does not zoom in, to avoid pixelated thumbnails.

Solution 3

You can convert to pdf using ImageMagick

convert png.png myout.pdf

but use pdfjam instead of ImageMagick to adjust the page size

pdfjam --paper a4paper --outfile myoutA4.pdf myout.pdf

pdfjam offers other options, which may fit your needs.

Solution 4

Found this somewhere on stackoverflow:

convert *.jpg -resize 1240x1753 \
                      -extent 1240x1753 -gravity center \
                      -units PixelsPerInch -density 150x150 multipage.pdf

Solution 5

You need to specify the density:

convert -density 80 -page a4 input_A.jpg input_B.jpg output.pdf

Otherwise, there can be an issue when printing with "actual size" instead of "fit". I have used a public printer where I could not choose "fit" and had to choose "actual size", in which case the printed page was only a part of the page which I wanted to print.

Fit vs. Actual Size

Share:
58,546

Related videos on Youtube

Mariusz Pala
Author by

Mariusz Pala

Updated on April 08, 2021

Comments

  • Mariusz Pala
    Mariusz Pala about 3 years

    I want to convert different image formats (bmp,jpg,gif,png,tiff-incluging multipaged) into a PDF format with A4 page size and with images fit to page (resized if necessary). Image should be positioned at the center of the page and I'd like to define an offset. I tried the code below but there is no offset at the top and the image quality is really poor.

    convert png.png -gravity North -resize 500x500 -quality 100 -page a4x5x5 myout.pdf
    

    Is there any way to do that?

    Thanks in advance for any help, Mariusz

    • fmw42
      fmw42 almost 5 years
      ImageMagick does not have a page size of a4x5x5. Use -resize 595 x 842 ... -extent 595 x 842
  • chikamichi
    chikamichi over 8 years
    You could probably escape the > like that: -resize 575x823^\>
  • fedelibre
    fedelibre about 8 years
    A google search took me here because the title of this thread is quite general. For simple uses, option -page a4 does already a good job automatically: convert file-pattern*.jpg -page a4 output.pdf
  • ziggystar
    ziggystar over 6 years
    Sorry, I checked again today and now it appears to work. I don't know what went wrong, I even used the exact same command from the shell history. Unfortunately I can't retract the downvote, as it is locked in now.
  • m4tx
    m4tx over 6 years
    It seems it actually does not upscale small images to fit the whole page.
  • Floyd
    Floyd over 5 years
    Thanks, this is the only solution which works for me. When converting color tiff file to pdf, i have to use pdfjam to fix the image size to A4 page size.
  • domen
    domen over 5 years
    If the image is smaller, and you want to resize it, use -page 'A4<'.
  • fmw42
    fmw42 almost 5 years
    -gravity should come before -extent
  • Abbas
    Abbas over 4 years
    Thanks. Exactly what I've been looking for!
  • Wok
    Wok about 4 years
    You need to specify the -density to avoid any issue when printing with "real size". stackoverflow.com/a/55125655/376454
  • Wakan Tanka
    Wakan Tanka over 3 years
    using this I got images as landscapes not portraits as it was in original
  • JoKalliauer
    JoKalliauer over 3 years
    If you define -page a4 it shrinks the image to fit into a A4-PDF. If you print this with original size, than the paper is A4, independent on density. If you want to print it on a different size, you should neither use fit nor should use -density, you should change -page .... .
  • Wok
    Wok over 3 years
    No. If you don't specify the density, and even if you specify page A4, the relevant part of the page won't necessarily be A4, it could be a tiny rectangle in the middle of the A4 page. Try to print with "actual size" instead of "fit", and you will see for yourself. I know, I have done it, and I was not messing with formats other than A4. Take a pic with a camera, and convert it to PDF.
  • Wok
    Wok over 3 years
    I think the issue is that JoKalliauer speaks only about the page format (which is indeed an A4 paper), and I speak about the content in the page (which means that I want the content of the picture to fill the A4 paper as expected).
  • jckdnk111
    jckdnk111 over 3 years
    This was exactly what I needed -- Thanks!
  • Kubuntuer82
    Kubuntuer82 about 3 years
    @Wok Using -density 72 I get an image which is smaller than an A4 page. If I remove it, then it has the right size, i.e. the same as an A4 page.
  • nomadSK25
    nomadSK25 about 2 years
    @fmw42 It doesn't matter.
  • fmw42
    fmw42 about 2 years
    It might not in Imagemagick 6, but it would in Imagemagick 7. Correct syntax is as I mentioned, but Imagemagick 6 is rather forgiving. Imagemagick 7 is more strictly adhering to correct syntax.