Centering a .jpg image in PDF using Imagemagick?

12,747

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.

Share:
12,747

Related videos on Youtube

sdaau
Author by

sdaau

Updated on September 16, 2022

Comments

  • sdaau
    sdaau almost 2 years

    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:

  • sdaau
    sdaau almost 13 years
    Awesome - many thanks, @John Keyes! I could have sworn I had already tried something with -format pdf and I failed; but I'm trying yer first command line and it works great! :) Thanks again - cheers!
  • John Keyes
    John Keyes almost 13 years
    You're welcome. I've gone down the rabbit hole of ImageMagick options many times.
  • mischka
    mischka about 8 years
    This does not work for large images (Version 6.9.2) , in my case converting 5 MByte JPEG. It just produces a blank page. Without the "gravity center" option it works, but is not centered, of course. Seems to be an open issue: imagemagick.org/discourse-server/viewtopic.php?t=22759

Related