Scaling PDF content and page dimensions from command line

I have a set of images stored as separate PDF files. One image per file. Each image takes up one PDF page.

I need to re-scale all of these images, preferably at the command line, so that the image content and the page dimensions of the PDFs are both scaled relative to their original sizes/dimensions. For example: scale all the images by 50% should shrink the size of the image and the dimensions of the page.

Already tried:

  • pdfpages + latex: will re-scale the image but dimensions of the pages stay the same.
  • pdfjam: same problem; can re-scale but page dimensions want to be letterpaper or a4.
  • convert (imagemagick): converts to raster, which I don't want.
  • ghostscript: seems to scale based on absolute new page size, and I need relative page size.

I know one of these must work. I can't figure out where I've gone wrong. I'm on Mac but a Linux solution would work just as well.


Solution 1:

I think I found one: http://community.coherentpdf.com/

cpdf -scale-page "0.5 0.5" in.pdf -o out.pdf

Solution 2:

pdfjam works for this.

pdfjam --outfile out.pdf --paper a5paper in.pdf

Solution 3:

The following will do what you want:

https://github.com/tavinus/pdfScale

NB: As at 2021-07-14, there is no way to specify an arithmetic scale factor, but as pdfScale does accept custom page sizes, the following posix shell script overcomes that limitation:

#!/bin/sh
inFile=yourFilenameHere.pdf
scalePercent=50
# NB: 50% linear scaling takes A4 to A6
# For 50% area scaling (A4 to A5), use scalePercent=70
oldSize=$(pdfinfo $inFile | grep '^Page size')
oldWidth=$(echo "$oldSize" | awk '{print $3}')
oldHeight=$(echo "$oldSize" | awk '{print $5}')
newWidth=$(($oldWidth * scalePercent / 100))
newHeight=$(($oldHeight * scalePercent / 100))
pdfScale.sh -r "custom pts $newWidth $newHeight" $inFile
# default output filename is yourFilenameHere.CUSTOM.pdf

There's a page at https://ma.juii.net/blog/scale-page-content-of-pdf-files which explains the history behind the project - interesting stuff! HTH.