Lela Jennings Lela Jennings - 1 year ago 63
Python Question

Need to Programmatically Convert PDF Paper Size (e.g. US-Letter to A4) in Python (or command line)

I need to convert PDF documents from one arbitrary page size to another.

For example, I might have a bunch of PDF documents formatted for us-letter paper (8.5" x 11") that I need to convert to the A4 paper size (21 cm x 29.7 cm), or I might need to convert A3 to ledger-size, etc.

This seems like it should be a simple task but my Google-fu is failing me.

It would be neat if I could actually re-flow the text of the documents to take better advantage of the new size and aspect ratio, but I'm not expecting that. It is sufficient to scale the up or down to fit within the new page size.

The rest of my program happens to be written in Python, and a Python-based solution would be nice, but a command line tool would be fine as long as I can run it on Linux/Unix/OSX.

Any suggestions on how best to go about this?

Answer Source

You could use

  • Ghostscript using the -sPAPERSIZE=a4 switch, see chapter 3.3 the docs or a full solution here on SO, or
  • pdfjam(a shell script that is part of texlive), with the --paper a4paper option, as described of unix.SE