Shell scripts: Change page-width of a PDF?

1

I have a scanned PDF file, where different pages have different page widths due to how the PDF was concatenated from multiple sources. This tends to trip up viewers.

Is there some command line tool, that can assign all pages the same width?

Since the scan is large and already strongly compressed, the actual bitmap images should remain unchanged, and OCR text, if any, should be preserved in the process.

For example:

  • Before.
    • Page size 15x10 cm
    • Image display size 15x10 cm
    • Image pixel size 1500x1000
  • After.
    • Page size 30x20 cm
    • Image display size 30x20 cm
    • Image pixel size 1500x1000, unchanged binary data stream.

A solution using imagemagick would be preferred, as it is readily available to shell scripts through Cygwin. I do however suspect, that it may not be possible to prevent imagemagick from re-encoding the images.

kdb

Posted 2018-09-27T15:17:48.493

Reputation: 1 076

Answers

1

You could try Ghostscript :

gs -sOutputFile=output.pdf -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dFIXEDMEDIA -dPDFFitPage -f input.pdf

Change a4 to whatever size that better fits your needs.

See Choosing paper size in the documentation.

You should be able to install GhostScript (gs) through the Cygwin setup.exe program.

harrymc

Posted 2018-09-27T15:17:48.493

Reputation: 306 093

That works indeed! Except for the weird corner case, that pages with landscape proportions are automatically rotated 90° counter-clockwise. Do you know, if there is any way to keep the orientation? – kdb – 2018-09-28T16:12:07.280