Programster's Blog

Tutorials focusing on Linux, programming, and open-source

PDF Cheatsheet

PDF

As with all of my cheatsheets, this is just a dumping ground for as things come up.

Save Unlocked PDF

If you get a PDF that requires a password to unlock, you can use this command to save a version that won't need unlocking in future.

qpdf \
  --password=$PASSWORD \
  --decrypt $INPUT.pdf \
  $OUTPUT.pdf

This requires having installed qpdf with sudo apt-get install qpdf.

Create Encrypted PDF

PASSWORD="somePasswordHere"

qpdf \
  --encrypt \
  $PASSWORD $PASSWORD \
  256 -- input.pdf \
 output.pdf

That is not a mistake, one has to provide the password twice.

Create A PDF From A Series Of Images

The following command will create a PDF document where all the images are the same width.

convert -quality 100% -resize 629 \
  "path/to/image.png" -resize 629 \
  "path/to/image2.png" -resize 629 \
  output.pdf

If you want the images to not all be the same width, then remove the -resize 629. Also, all your images need to have the same DPI, otherwise it doesn't work.

Fixing Error Message

If you get the error message:

convert-im6.q16: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408.

Then do the following. Edit your ImageMagick policy configuration:

editor /etc/ImageMagick-6/policy.xml

Add the following just before the final </policy> tag:

  <policy domain="coder" rights="read | write" pattern="PDF" />

E.g.

  <policy domain="coder" rights="none" pattern="PDF" />
  <policy domain="coder" rights="none" pattern="XPS" />
  <policy domain="coder" rights="read | write" pattern="PDF" />
</policymap>

It should now work!

Split PDF Into Set Of PDFs

If you wish to split a PDF into a set of single-page PDFs, you can do so like this:

pdftk myoldfile.pdf burst 

Pdftk can be installed by running sudo apt install pdftk-java.

Reverse PDF Pages

If your PDF is in the reverse order of what it should be, you can rearrange the pages with the following command:

$INPUT_FILENAME=input.pdf
$OUTPUT_FILENAME=reversed.pdf

pdftk $INPUT_FILENAME \
  cat end-1 \
  output $OUTPUT_FILENAME

Convert PDF To Set Of Images

Tools like Figma make it really easy to export as a single PDF. However, I need to convert that to just a set of PNG images. Luckily this is really easy, and essentially the same as before in reverse (using ImageMagick).

convert \
  input-file.pdf \
  output.png

That will create a set of incremented filenames like so:

  • output-0.png
  • output-1.png
  • output-2.png

Crop PDF

If you have a PDF, and you find that the margins are too large, which is causing it to be hard to read easily on a tablet, you can crop the margins with pdfcrop as shown below:

LEFT=-20
RIGHT=-25
TOP=-60
BOTTOM=-175

pdfcrop \
  --margins "$LEFT $TOP $RIGHT $BOTTOM" \
  /path/to/pdf.pdf

This can also be useful for when printing if you want to "zoom in" on the content as the printer can scale to fit the width.

Merge Two PDFs In Interleaving Manner

My HP printer can only bulk scan documents on one side at a time. This means that I have to feed them through twice. Once for the front side of the pages, and once again for the back side. Each of these scan jobs come down in a single PDF file, meaning I have one PDF file for the front set of pages, and one set for the back. To gracefully merge these two PDF documents into a single PDF document, one simply needs to run the following command:

pdftk A=front.pdf B=back.pdf shuffle A B output collated.pdf

Cut Pages Out Of A PDF

If you need to remove a page or multiple pages from a PDF, this is pretty straightforward. However, we really just need to create a new PDF and specify which pages we wish to keep, rather than the ones we wish to remove. E.g. like so:

pdftk input.pdf \
  cat 1 3 5-21 23-end \
  output new-version.pdf

The command above create a new version called new-version.pdf, which would not have the second, fourth and 22nd pages, whilst retaining all of the rest.

References

Last updated: 9th June 2024
First published: 14th April 2019