Scale an image to fit on A4 pdf-file, with some margins

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
gvdmoort
Posts: 4
Joined: 2010-10-05T02:06:31-07:00
Authentication code: 8675308

Scale an image to fit on A4 pdf-file, with some margins

Post by gvdmoort »

Hello,

I'd like to convert an image from arbitrary size to pdf
so that it fit «almost» an A4 paper (I make a little margin
so that the image can be printed without loss on the edges)

For example, I create an image from scratch with these
dimensions:

$ convert -size 100x100 gradient:red-blue gradient.png

The width of an A4 is 595 pt; to get the correct size,
I calculate:

actual size / wished size * 72

100*72/595 = 12.1

$ convert -density 12.1 gradient.png gradient.pdf
$ grep -a Box gradient.pdf
/MediaBox [0 0 595.041 595.041]
/CropBox [0 0 595.041 595.041]

To get some margins, I do 100*72/538 = 13.4

$ convert -density 13.4 gradient.png gradient.pdf
$ grep -a Box gradient.pdf
/MediaBox [0 0 537.313 537.313]
/CropBox [0 0 537.313 537.313]

Now, I'd like to get a PDF with the «canonical» size of
595x842 pt, with the image correctly scaled centered on it,
and white margins around it. How to do that ?

Thanks in advance,

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

Re: Scale an image to fit on A4 pdf-file, with some margins

Post by fmw42 »

The width of an A4 is 595 pt; to get the correct size,
I calculate:

actual size / wished size * 72

100*72/595 = 12.1

$ convert -density 12.1 gradient.png gradient.pdf
I think you have it wrong. I believe you want to compute:

72* wished size / actual size
Now, I'd like to get a PDF with the «canonical» size of
595x842 pt, with the image correctly scaled centered on it,
and white margins around it. How to do that ?
You cannot scale an arbitrary image to fit 595x842 unless the aspect ratio is the same as 595/842; otherwise, you will get distortion in one dimension or the other.

If your image is not the correct aspect ratio, then scale it to fit into 595x842 and pad with white to 595x842, then convert to pdf.

try

convert yourimage -resize 595x842 -gravity center -background white -extent 595x842 resultimage

or

directly to pdf as

convert yourimage -resize 595x842 -gravity center -background white -extent 595x842 resultimage.pdf
Post Reply