Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering a .jpg image in PDF using Imagemagick?

I'm using convert version ImageMagick 6.6.2-6 2011-03-16, and I'd like to use it to generate an A4 pdf from an image, where the image will be non-scaled and centered.

I'm running the following (as a modification of Overlaying Images with ImageMagick):

# generate a 100x100 JPG with just red color
convert -size 100x100 xc:red red.jpg

# generate PDF from JPG
convert -page A4 xc:white red.jpg -gravity center -composite -format pdf out.pdf

... but, basically nothing shows? Same thing happens for a png image...

Note that

  • Just 'convert -page A4 red.jpg out.pdf' works - but the image is not centered; (-gravity center causes image not to show)
  • If the image is png, 'convert -page A4 -gravity center red.png out.pdf' does indeed work fine

... however, I'd like convert to embed the contents of the JPEG stream directly - hence, I wouldn't like to convert the JPG to PNG first.

So, would it be possible to use convert to center a JPG image in an A4 PDF page directly?

Many thanks in advance for any answers,
Cheers!

 

EDIT2: @John Keyes answer works for the example above; where the image is "smaller" than the PDF size -- however if the image is bigger, e.g.:

$ convert -size 1228x1706 -background \#f44 -rotate 45 gradient:\#f00-\#fff red.jpg
$ identify red.jpg 
red.jpg JPEG 2075x2075 2075x2075+0+0 8-bit DirectClass 120KB 0.000u 0:00.000

... then it will fail. However, it turns out: "if you change -extent to 50x50, then play with -gravity, you'll see changes" - except, the question is: which extent do you change, that of the image - or that of the final PDF?

Well, it turns out - it is the extent of the final PDF... To find that size as convert sees it, check the page: Magick::Geometry - however, note that the "Postscript page size specifications" like "A4+43+43>" unfortunately, cause convert to crash in this context... But at least the respective numbers for the size (595x842) can be copied from the page; and finally this works:

convert -page A4 -gravity center -resize 595x842 -extent 595x842 red.jpg out.pdf

... and actually, the -extent part is not really needed - the -resize part is the important one to have the large image show..

However, the problem here is that the image included seems to be resampled - however, I'd just like to show it scaled so it fits the page, but would otherwise like the original JPG stream to be inserted in the file.. So I guess the question is still partially open :)

EDIT: Related:

  • ImageMagick Gravity parameter - Stack Overflow
  • ImageMagick and Geometry Issue - resizing with > - Stack Overflow
  • command line - Resizing and croping images to an aspect ratio of 6x4 with width of 1024 pixels - Unix and Linux - Stack Exchange
  • conversion - using imagemagick or ghostscript (or something) to scale PDF to fit page? - Stack Overflow
like image 967
sdaau Avatar asked Sep 28 '11 23:09

sdaau


1 Answers

The following works perfectly for me:

convert -page A4 red.jpg  -gravity center -format pdf out.pdf

and if you change the order of the "files" it works too:

convert -page A4 red.jpg xc:white -gravity center -composite -format pdf out.pdf

I think the red.jpg is centered but the white is drawn on top of it.

like image 149
John Keyes Avatar answered Sep 21 '22 13:09

John Keyes