When I convert a png-image to pdf, I have been using convert in.png -page A4 out.pdf to get a pdf where the image quality is retained and the image is aligned to the bottom left corner by default. To align it to the center, I can add -gravity center.

However, I recently discover that when trying convert in.png -gravity center -page A4 out.pdf with an image that has larger dimensions than an A4 sheet, the top right corner of the image will be centred in the pdf and much of the image is cropped as it only fills the bottom left corner of the pdf. Without -gravity center everything works as expected and the image is resized and fills the pdf starting from the lower left corner of the pdf while retaining what seems to be full resolution.

I have tried this suggestion and different combinations of -density, -extent, -quality, and resize, but I can't get the image centred with full resolution. The closest I have gotten is convert in.png -gravity centre -page A4 -resize 595x842 out.pdf, which centres and resizes the image, but reduces quality substantially.

When the image is larger than the pdf output size, how should I use convert to create a pdf of fixed size where the image is resized to fit the pdf, centred and retains full resolution?


It's bound to be ugly when using 595x842.

After lots of googling and cursing and failures I ended up with this one (rotate 90 for "landscape mode"):

for num in 1 2 3 4 5 6
  convert image$num.jpeg -rotate 90 -density 300 -resize 2200x3400 -gravity center $num.pdf
  lpr $num.pdf

I found the resize parameters by multiplying the density with the A4 page size in inches, as found on the wikipedia article, and then just rounding down a bit; it could probably be tuned better. It seems like it should be 2481x3510 without rounding, but then I suspect the images may get a bit cropped.

"-gravity center" seems not to work very well, those images didn't get very well centered. Adding "-page A4" made it all worse, either blank pages or just part of the image on the actual page.

If it's really high-quality stuff, it's possible to raise density even more, i.e. up to 1200, and raise the resize parameters the same. The resulting files will be bigger, and the processing time by the printer as well.

Weirdly enough, it got out in black&white on my local printer - even though printing stuff from the browser usually works out well. I tried to convert to .ps instead of .pdf, same result, just that the printer spent ages processing the postscript. I eventually solved it by printing to another printer ... go figure!?

Printers are evil.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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