Search code examples
pdfimagemagickimagemagick-convert

ImageMagick convert adds whitespace when converting PDF to PNG


I'm using ImageMagick to convert the following PDF to an PNG file.

Click here to download the PDF from IMSLP (Permalink if the direct download is broken)

In a PDF viewer it looks nice:

enter image description here

but when converting with convert -density 300 -background white -alpha off -alpha remove file.pdf /tmp/file.png

the image gets a large white margin:

enter image description here

I do not want to trim the image afterwards, I just want ImageMagick to somehow respect the view-port or however that viewing information is being encoded in the PDF. Does anyone know which command-line parameter might enable this behavior?

Edit 10.03.2022: I'm using ImageMagick 7.1.0.16 with Ghostscript 9.55.0 inside an Alpine Linux docker image.


Solution

  • The hint from KenS was exactly what I was looking for - the PDF defines a CropBox that ImageMagick 7.1.0 was not using by default. The solution therefore is to modify the command to include the following -define information:

    convert -define pdf:use-cropbox=true file.pdf /tmp/file.png 
    

    Thank you all for your help!