Search code examples
pdfimagemagickgraphicsmagick

GraphicsMagick crop PDF


I've got a 8.5x11 PDF at 300dpi. It has a single UPC label in the top left corner of the PDF. Imagine that there could be 30 labels on a 1 sheet, but we just have 1 label.

I'm trying to crop the PDF to be just the size of the 1 label. So far I've got this

gm convert -density 300 single.pdf out.pdf

Which doesn't do any cropping. When I crop to say 300x100 it makes a 20MB file with 30000 pages.

I have not a clue how to use -crop to actually crop to the correct size. I need it to be 3.5inches by 1.125 inches.


Solution

  • Using the following input PDF (here converted to a PNG):

    wiz.pdf

    the following command will crop the label:

    gm wiz.pdf -crop 180x50+1+1  cropped.pdf
    

    cropped.pdf

    This label is sized 180x50 pixels.

    For an 8.5x11in PDF at 300 PPI you'd have a 2450x3300 pixels PDF (which I doubt you do, but that's another question) and you'd need to use -crop 1050x337+0+0 (more exactly, 1050x337.5+0+0 -- but you cannot crop half pixels!).

    Note, the +0+0 part crops the top left corner. If you need offset to the right by N pixels and to the bottom by M pixels use +N+M...


    Using ImageMagick instead...

    You could also use ImageMagick's convert command:

    convert wiz.pdf[180x50+1+1] cropped.pdf
    

    Comment about image sizes...

    One additional comment about this remark:

    "I have not a clue how to use -crop to actually crop to the correct size."

    There is no other real size for raster images than pixels. ABC pixels wide and XYZ pixels high...

    There is no such thing as an absolute, real size for a digital image that you can measure in inches... unless you additionally can state the resolution at which a given image is rendered on a display or a print device!

    An 8.50x11in sized image at 300 PPI will translate to 2550x3300 pixels. However, if your image does not contain this amount of pixels (which is the real, absolute size of any raster image), you may still be able to render it at 300 PPI -- but its size in inches will be different from 8.5x11in!

    So, whenever you want to crop, use the absolute number of pixels you want. Don't use resolution/density at all on your command line!