I’ve been used the ImageMagick convert tool to make JPG images from PDF pages. Nothing too fancy, just generating a single 8.5 x 11 inch ratio JPG for each page in a PDF. This worked really well until I started seeing some PDF files generate the same size JPG, but instead of the page taking up the whole image it was about one quarter size, in the lower left corner.
Inspecting the PDFs showed that they had much larger dimensions. So I tried tweaking several settings; density, scale, resize and resample, all with no luck. No matter how I adjusted the settings I couldn’t get the page to use the whole image size. After more searching and trial and error I finally came across the information I needed to make this work, the define option. Specifically: -define pdf:use-cropbox=true
.
By default ImageMagick uses the MediaBox geometry to figure out the page size. But PDFs may have a CropBox or TrimBox value that is smaller than the MediaBox, which is exactly what I was looking for. From the ImageMagick PDF format description:
Requires Ghostscript to read. By default, ImageMagick sets the page size to the MediaBox. Some PDF files, however, have a CropBox or TrimBox that is smaller than the MediaBox and may include white space, registration or cutting marks outside the CropBox or TrimBox. To force ImageMagick to use the CropBox or TrimBox rather than the MediaBox, use -define (e.g. -define pdf:use-cropbox=true or -define pdf:use-trimbox=true). Use -density to improve the appearance of your PDF rendering (e.g. -density 300×300).
In my case adding the use-cropbox=true
fixed the problem.
10 replies on “ImageMagick convert – PDF to JPG, Partial Image Size Problem”
Hi! You make my day!
My problem was that with some pages the conversion doubles its size, so when the average image size was 1024px, on some pages it creates 2048px width with a blank padding on its left.
This define works very good.
Thank you very much for this. You saved me hours of tedious cropping 🙂
Hi,
Brilliant !!! I’ve been searching for hours !
Thanks a lot !
Thank you, I have spent hours trying to solve this problem. I can’t believe that this is not noted in the documentation under cropping.
You are welcome. I figured if it is frustrating to me then someone else may have problems with it too.
Please help me if you have solustion.
I have pdf but without bleed and trimbox so I want to add Trimbox using PHP.
How to add dynamically Trimbox in pdf.
The same issue is found in graphicsmagick (i.e.: “gm convert…”), and your solution applies there too. Thanks for saving me a lot of hair tearing.
Glad to hear it helped.
Just wanna let you know that 8 years later, your blog post just saved our lives ! Thanks !
Thank you so much Joseph. You saved my life.