Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ImageMagick or Ghostscript (or something) to scale PDF to fit page?

I've been bashing my head against this to no avail.

I need to shrink some large PDFs to print on an 8.5x11 inch (standard letter) page. Can ImageMagick/Ghostscript handle this sort of thing, or am I having so much trouble because I'm using the wrong tool for the job?

Just relying on the 'shrink to page' option in client-side print dialogs is not an option, as we'd like for this to be easy-to-use for the end users.

like image 337
ceejayoz Avatar asked Feb 13 '09 19:02

ceejayoz


People also ask

Does ImageMagick use ghostscript?

The ghostscript interpreter is used by ImageMagick and GraphicsMagick to convert Postscript and similar formats into images.

Can ImageMagick convert PDF?

Use ImageMagick® to create, edit, compose, or convert digital images. It can read and write images in a variety of formats (over 200) including PNG, JPEG, GIF, WebP, HEIC, SVG, PDF, DPX, EXR and TIFF.


2 Answers

I would not use convert. It uses Ghostscript in the background, but is much slower. I'd use Ghostscript directly, since it gives me much more direct control (and also some control over settings which are much more difficult to achieve with convert). And for convert to work for PDF-to-PDF conversion you'll have Ghostscript installed anyway:

  gs \
    -o /path/to/resized.pdf \
    -sDEVICE=pdfwrite \
    -dPDFFitPage \
    -r300x300 \
    -g2550x3300 \
    /path/to/original.pdf
like image 72
Kurt Pfeifle Avatar answered Sep 28 '22 02:09

Kurt Pfeifle


ImageMagick's mogrify/convert commands will indeed do the job. Stephen Page had just about the right idea, but you do need to set the dpi of the file as well, or you won't get the job done.

Assuming you have a file that's 300 dpi and already the same aspect ratio as 8.5 x 11 the command would be:

// 300dpi x 8.5 -2550, 300dpi x 11 -3300
convert original.pdf -density "300" -resize "2550x3300" resized.pdf

If the aspect ratio is different, then you need to do some slightly trickier cropping.

like image 31
danieltalsky Avatar answered Sep 28 '22 04:09

danieltalsky