65

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

Mariusz Pala
  • 961
  • 1
  • 8
  • 17
  • ImageMagick does not have a page size of a4x5x5. Use -resize 595 x 842 ... -extent 595 x 842 – fmw42 Aug 21 '19 at 21:55

5 Answers5

71

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.

JoKalliauer
  • 1,648
  • 1
  • 13
  • 17
  • 1
    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. – ziggystar Oct 16 '17 at 09:08
  • 7
    It seems it actually does not upscale small images to fit the whole page. – m4tx Oct 30 '17 at 23:16
  • 3
    You need to specify the `-density` to avoid any issue when printing with "real size". https://stackoverflow.com/a/55125655/376454 – Wok Jun 18 '20 at 09:02
  • using this I got images as landscapes not portraits as it was in original – Wakan Tanka Oct 06 '20 at 06:41
  • 2
    @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. – Kubuntuer82 Apr 07 '21 at 13:40
23

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.

toliveira
  • 1,533
  • 16
  • 27
  • 3
    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. – Floyd Feb 18 '19 at 10:31
20

Found this somewhere on stackoverflow:

convert *.jpg -resize 1240x1753 \
                      -extent 1240x1753 -gravity center \
                      -units PixelsPerInch -density 150x150 multipage.pdf
nomadSK25
  • 2,350
  • 3
  • 25
  • 36
  • 5
    -gravity should come before -extent – fmw42 Aug 21 '19 at 21:56
  • 1
    Thanks. Exactly what I've been looking for! – Abbas Jan 10 '20 at 16:55
  • 1
    This was exactly what I needed -- Thanks! – jckdnk111 Mar 19 '21 at 14:56
  • @fmw42 It doesn't matter. – nomadSK25 May 11 '22 at 03:08
  • 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. – fmw42 May 11 '22 at 03:55
  • Unfortunately, this would modify the images themselves (to add some white pixels around them). What I want is to add the images (resized or not) within an A4 page in a PDF, without the need to enlarge some images with white. – Totor Oct 13 '22 at 14:50
14

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
JoKalliauer
  • 1,648
  • 1
  • 13
  • 17
Mariusz Pala
  • 961
  • 1
  • 8
  • 17
  • 3
    You could probably escape the > like that: -resize 575x823^\> – chikamichi Dec 21 '15 at 12:54
  • 21
    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` – fedelibre Jun 13 '16 at 09:37
  • 4
    If the image is smaller, and you want to resize it, use `-page 'A4<'`. – domen Feb 27 '19 at 21:28
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

Wok
  • 4,956
  • 7
  • 42
  • 64
  • 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 .... . – JoKalliauer Feb 04 '21 at 14:34
  • 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 Feb 05 '21 at 11:24
  • 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). – Wok Feb 05 '21 at 11:30