Edit PDFs on the Linux command line

Use qpdf and poppler-utils to split, modify, and combine PDF files.
32 readers like this.
Text editor on a browser, in blue

Many of the documents you receive come in PDF format. Sometimes those PDFs need to be manipulated. For example, pages might need to be removed or added, or you might need to sign or change a specific page.

Whether good or bad, this is the reality we all live in.

There are some fancy graphical user interface tools that let you edit PDFs, but I have always been most comfortable with the command line. Of the many command-line tools for this task, the ones I use when I want to modify a PDF are qpdf and poppler-utils.

Install

On Linux, you can install qpdf and poppler-utils using your package manager (such as apt or dnf.) For example, on Fedora:

$ sudo dnf install qpdf poppler-utils

On macOS, use MacPorts or Homebrew. On Windows, use Chocolatey.

qpdf

The qpdf command can do a lot, but I mostly use it for:

  1. Splitting a PDF into separate pages
  2. Concatenating, or combining, PDFs into one file

To split a PDF into separate pages:

qpdf --split-pages original.pdf split.pdf

This generates files like split-01.pdf, split-02.pdf, and so on. Each file is a single-page PDF file.

Concatenating files is a little subtler:

qpdf --empty concatenated.pdf --pages split-*.pdf --

This is what qpdf does by default. The --empty option tells qpdf to start with an empty file. The two dashes (--) at the end signals that there are no more files to process. This is a case where the parameters reflect an internal model, rather than what people use it for, but at least it runs and produces valid PDFs!

poppler-utils

This package contains several utilities, but the one I use the most is pdftoppm, which converts PDF files to portable pixmap (ppm) image files. I usually use it after I split pages with qpdf and need to convert a specific page to an image that I can modify. The ppm format is not well known, but the important thing about it is that most image manipulation methods, including ImageMagick, Pillow, and many other options, work with it. Most of these tools can save files back to PDF, too.

Workflow

My usual workflow is:

  • Use qpdf to split the PDF into pages.
  • Use poppler-utils to convert the pages that need to be changed into images.
  • Modify the images as needed and save them to PDF.
  • Use qpdf to concatenate the pages back into one PDF.

Other tools

There are many great open source commands to deal with PDFs, whether you're shrinking them, creating them from text files, converting documents, or trying your best to avoid them altogether. What are your favorite open source PDF utilities? Please share them in the comments.

What to read next
Moshe sitting down, head slightly to the side. His t-shirt has Guardians of the Galaxy silhoutes against a background of sound visualization bars.
Moshe has been involved in the Linux community since 1998, helping in Linux "installation parties". He has been programming Python since 1999, and has contributed to the core Python interpreter. Moshe has been a DevOps/SRE since before those terms existed, caring deeply about software reliability, build reproducibility and other such things.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.