TranslateProject/sources/tech/20210706 Edit PDFs on the Linux command line.md
DarkSun 771a58f6e9 选题[tech]: 20210706 Edit PDFs on the Linux command line
sources/tech/20210706 Edit PDFs on the Linux command line.md
2021-07-07 05:03:49 +08:00

4.3 KiB

Edit PDFs on the Linux command line

Use qpdf and poppler-utils to split, modify, and combine PDF files. 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.


via: https://opensource.com/article/21/7/qpdf-command-line

作者:Moshe Zadka 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出