在 Linux 命令行上编辑 PDF
PDF

> 
> 使用 qpdf 和 poppler-utils 来分割、修改和合并 PDF 文件。
> 
> 
> 


![](/data/attachment/album/202107/15/093249xh6dmg846py8bgbc.jpg "Text editor on a browser, in blue")


你收到的许多文件都是 PDF 格式的。有时这些 PDF 需要进行处理。例如，可能需要删除或添加页面，或者你可能需要签署或修改一个特定的页面。


不管是好是坏，这就是我们所处的现实。


有一些花哨的图形用户界面工具可以让你编辑 PDF，但我一直对命令行感到最舒服。在这个任务的许多命令行工具中，当我想修改一个 PDF 时，我使用的是 `qpdf` 和 `poppler-utils`。


### 安装


在 Linux 上，你可以用你的包管理器（如 `apt` 或 `dnf`）来安装 `qpdf` 和 `poppler-utils`。比如在 Fedora 上：



```
$ sudo dnf install qpdf poppler-utils

```

在 macOS 上，使用 [MacPorts](https://opensource.com/article/20/11/macports) 或 [Homebrew](https://opensource.com/article/20/6/homebrew-mac)。在 Windows 上，使用 [Chocolatey](https://opensource.com/article/20/3/chocolatey)。


### qpdf


`qpdf` 命令可以做很多事情，但我主要用它来：


1. 将一个 PDF 分割成不同的页面
2. 将多个 PDF 文件合并成一个文件


要将一个 PDF 分割成不同的页面：



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

```

这就会生成像 `split-01.pdf`、`split-02.pdf` 这样的文件。每个文件都是一个单页的 PDF 文件。


合并文件比较微妙：



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

```

这就是 `qpdf` 默认的做法。`--empty` 选项告诉 qpdf 从一个空文件开始。结尾处的两个破折号（`--`）表示没有更多的文件需要处理。这是一个参数反映内部模型的例子，而不是人们使用它的目的，但至少它能运行并产生有效的 PDF！


### poppler-utils


这个软件包包含几个工具，但我用得最多的是 [pdftoppm](https://www.xpdfreader.com/pdftoppm-man.html)，它把 PDF 文件转换为可移植的像素图（`ppm`）文件。我通常在用 `qpdf` 分割页面后使用它，并需要将特定页面转换为我可以修改的图像。`ppm` 格式并不为人所知，但重要的是大多数图像处理方法，包括 [ImageMagick](https://opensource.com/article/17/8/imagemagick)、[Pillow](https://opensource.com/article/20/8/edit-images-python) 等，都可以使用它。这些工具中的大多数也可以将文件保存为 PDF。


### 工作流程


我通常的工作流程是：


* 使用 `qpdf` 将 PDF 分割成若干页。
* 使用 `poppler-utils` 将需要修改的页面转换为图像。
* 根据需要修改图像，并将其保存为 PDF。
* 使用 `qpdf` 将各页合并成一个 PDF。


### 其他工具


有许多很好的开源命令来处理 PDF，无论你是 [缩小它们](https://opensource.com/article/20/8/reduce-pdf)、[从文本文件创建它们](https://opensource.com/article/20/5/pandoc-cheat-sheet)、[转换文档](https://opensource.com/article/21/3/libreoffice-command-line)，还是尽量 [完全避免它们](https://opensource.com/article/19/3/comic-book-archive-djvu)。你最喜欢的开源 PDF 工具是什么？请在评论中分享它们。




---


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


作者：[Moshe Zadka](https://opensource.com/users/moshez) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
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](https://opensource.com/article/20/11/macports) or [Homebrew](https://opensource.com/article/20/6/homebrew-mac). On Windows, use [Chocolatey](https://opensource.com/article/20/3/chocolatey).

## qpdf

The `qpdf`

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

- Splitting a PDF into separate pages
- 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](https://www.xpdfreader.com/pdftoppm-man.html), 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](https://opensource.com/article/17/8/imagemagick), [Pillow](https://opensource.com/article/20/8/edit-images-python), 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](https://opensource.com/article/20/8/reduce-pdf), [creating them from text files](https://opensource.com/article/20/5/pandoc-cheat-sheet), [converting documents](https://opensource.com/article/21/3/libreoffice-command-line), or trying your best to [avoid them altogether](https://opensource.com/article/19/3/comic-book-archive-djvu). What are your favorite open source PDF utilities? Please share them in the comments.

## Comments are closed.