JPG to PDF with exact A4 dimensions

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
kexec
Posts: 4
Joined: 2020-02-06T14:30:18-07:00
Authentication code: 1152

JPG to PDF with exact A4 dimensions

Post by kexec »

Please, could someone help with this problem? I have JPG images (scans) and I want to convert them to PDF with exact A4 dimensions, without quality loss, without scaling, and with centered images in the PDF. The images are scanned with 300 DPI and I want to preserve this. The images' resolution is smaller than resolution of A4 @ 300 DPI = 2480x3508, thats why I dont want any scaling but want to preserve the original 300 DPI and center the images in the PDF (both horizontally and vertically), so there would be a small white frame around them.

Command "magick *.jpg output.pdf" results in a PDF which doesnt have exact A4 dimensions (210x297 mm or 595x842 px @ 72 DPI). I tried many combinations of parameters like -page, -density, -gravity center, etc., but the output PDF either had wrong dimensions (not A4) or scaled or not centered images.

Please, could someone help me how can I acheive this with imagemagick?

User avatar
fmw42
Posts: 26294
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: JPG to PDF with exact A4 dimensions

Post by fmw42 »

You cannot achieve that without either scaling or padding unless your JPG files have pixel dimension of exactly 595 x 842 for W x H or some exact multiple so that the same factor will achieve the required pixel dimensions of 595 x 842 in both dimensions.

kexec
Posts: 4
Joined: 2020-02-06T14:30:18-07:00
Authentication code: 1152

Re: JPG to PDF with exact A4 dimensions

Post by kexec »

Well, it seems that padding is what I am looking for. Does it mean that the image will not be scaled and there will be white space (margins) around the (centered and non-scaled) image in the PDF?

snibgo
Posts: 12938
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: JPG to PDF with exact A4 dimensions

Post by snibgo »

The problem boils down to: your inputs are smaller than 2480x3508 pixels. You want outputs of that size, without scaling your images. So want a white border around the image.

Code: Select all

magick in.jpg -gravity center -extent 2480x3508 out.pdf
snibgo's IM pages: im.snibgo.com

kexec
Posts: 4
Joined: 2020-02-06T14:30:18-07:00
Authentication code: 1152

Re: JPG to PDF with exact A4 dimensions

Post by kexec »

Thanks a lot man, its nearly exactly what I want. Just there is one another problem. With your command, ImageMagick seems to edit the images itself and embed the white border (or padding or how to call it) directly into the images. So if I open the PDF file in Adobe Reader and select and copy the image, its copied including the white borders (as the borders are part of the images), but I would prefer to copy just the original (untouched) image in its original resolution without these white borders. Isn't it possible to make this in a way that the image itself will not be touched, so the white border will not be part of the images but will be part only of the PDF?

I tried this command:

Code: Select all

magick *.jpg -density 72 -page a4 output.pdf
The output PDF has A4 dimensions (as I want) and when I select and copy the image (in Adobe Reader), its also copied as I want (the original image in its original resolution). But the problem here is that the images in the PDF are scaled to enitre page, which I dont want, I want the white borders instead.

kexec
Posts: 4
Joined: 2020-02-06T14:30:18-07:00
Authentication code: 1152

Re: JPG to PDF with exact A4 dimensions

Post by kexec »

And if I try this command:

Code: Select all

magick *.jpg -gravity center -density 72 -page a4 output.pdf
the resulting PDF is just blank... Am I doing something wrong or this is a bug? I am using latest version of ImageMagick: 7.0.9-21 Q16 x64 2020-02-01

snibgo
Posts: 12938
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: JPG to PDF with exact A4 dimensions

Post by snibgo »

kexec wrote:Isn't it possible to make this in a way that the image itself will not be touched, so the white border will not be part of the images but will be part only of the PDF?
Not with ImageMagick, no. When writing a pdf, IM wraps the page boundary tightly around the image.
snibgo's IM pages: im.snibgo.com

colouredFisH
Posts: 1
Joined: 2020-02-14T02:16:28-07:00
Authentication code: 1152
Location: germany

Re: JPG to PDF with exact A4 dimensions

Post by colouredFisH »

snibgo wrote:
2020-02-07T08:57:05-07:00
kexec wrote:Isn't it possible to make this in a way that the image itself will not be touched, so the white border will not be part of the images but will be part only of the PDF?
Not with ImageMagick, no. When writing a pdf, IM wraps the page boundary tightly around the image.
I suggest using https://gitlab.mister-muffin.de/josch/i ... ree/master (img2pdf) which just wraps your images into pdf-frames without any touching.
You might want to use

Code: Select all

--auto-orient -S A4
which is for A4-page-sizing or

Code: Select all

--auto-orient -S A4 --border 2cm:2cm
for A4 incl. borders to keep a certain distance from the page-edge.

This is Python-based. But Windows-Users can use a PyInstaller-created .exe (easiest way is to follow the given link, I created my own) which works flawlessly.

1st post announcement:
With that same idea in mind I came in contact with ImageMagick - which is absolutely awesome. Thanks to the Devs and the active community here (with all the lovely and power-releasing topics) !

Post Reply