Linux下通过pdftk命令行操作PDF

  • Linux下通过pdftk命令行操作PDF已关闭评论
  • 10 views
  • A+
所属分类:linux

pdftk 命令提供了许多处理 PDF 的命令行操作,包括合并页面、加密文件、添加水印、压缩文件,甚至还有修复 PDF。

安装:

我这里是centos7 64位的,直接rpm安装如下:

$ yum install gcc gcc-java libgcj libgcj-devel gcc-c++
$ yum localinstall https://www.linuxglobal.com/static/blog/pdftk-2.02-1.el7.x86_64.rpm

也可以采用编译安装

$ yum install gcc gcc-java libgcj libgcj-devel gcc-c++
$ wget https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk-2.02-src.zip
$ unzip pdftk-2.02-src.zip
$ cd pdftk-2.02-dist/pdftk
$ make -f Makefile.Redhat
$ make -f Makefile.Redhat install

默认安装路径在 /usr/local/bin

如果编译错误如下: gcj: Internal error: Killed (program jc1), 则可能是您没有足够的计算机内存。我们在只有1GB内存的虚拟机上看到了这个错误。

用例:

 

1:加密

$ pdftk a.pdf output o.pdf user_pw 123456

加密参数选项提供 40(encrypt_40bit)和 128(encrypt_128bit)位加密。默认情况下使用 128 位加密。

如果使用40位加密则如下:

$ pdftk a.pdf output o.pdf encrypt_40bit user_pw 123456

2:文件合并

$ pdftk 1.pdf 2.pdf 3.pdf  cat output a.pdf

a.pdf 将包含上面显示的所有五个文档,命令将在几秒钟内运行完毕。请注意,cat 选项表示将文件连接在一起,output 选项指定新文件的名称。

或者

$ pdftk A=1.pdf B=2.pdf cat A B output a.pdf

或者通配批量合并

$ pdftk *.pdf cat output o.pdf

将从一个包含奇数页(1、3 等)的文件和一个包含偶数页(2、4 等)的文件创建一个整合文档,如下:

$ pdftk A=1.pdf B=2.pdf shuffle A B output o.pdf

3:文件拆分

将 PDF 文件分成单个页面,如下:

$ pdftk a.pdf burst

结果如下:

[root@k3s pdf]# pdftk a.pdf burst
[root@k3s pdf]# ls
a.pdf pg_0010.pdf pg_0021.pdf pg_0032.pdf pg_0043.pdf pg_0054.pdf pg_0065.pdf pg_0076.pdf pg_0087.pdf pg_0098.pdf
doc_data.txt pg_0011.pdf pg_0022.pdf pg_0033.pdf pg_0044.pdf pg_0055.pdf pg_0066.pdf pg_0077.pdf pg_0088.pdf pg_0099.pdf
pg_0001.pdf pg_0012.pdf pg_0023.pdf pg_0034.pdf pg_0045.pdf pg_0056.pdf pg_0067.pdf pg_0078.pdf pg_0089.pdf pg_0100.pdf
pg_0002.pdf pg_0013.pdf pg_0024.pdf pg_0035.pdf pg_0046.pdf pg_0057.pdf pg_0068.pdf pg_0079.pdf pg_0090.pdf pg_0101.pdf
pg_0003.pdf pg_0014.pdf pg_0025.pdf pg_0036.pdf pg_0047.pdf pg_0058.pdf pg_0069.pdf pg_0080.pdf pg_0091.pdf
pg_0004.pdf pg_0015.pdf pg_0026.pdf pg_0037.pdf pg_0048.pdf pg_0059.pdf pg_0070.pdf pg_0081.pdf pg_0092.pdf
pg_0005.pdf pg_0016.pdf pg_0027.pdf pg_0038.pdf pg_0049.pdf pg_0060.pdf pg_0071.pdf pg_0082.pdf pg_0093.pdf
pg_0006.pdf pg_0017.pdf pg_0028.pdf pg_0039.pdf pg_0050.pdf pg_0061.pdf pg_0072.pdf pg_0083.pdf pg_0094.pdf
pg_0007.pdf pg_0018.pdf pg_0029.pdf pg_0040.pdf pg_0051.pdf pg_0062.pdf pg_0073.pdf pg_0084.pdf pg_0095.pdf
pg_0008.pdf pg_0019.pdf pg_0030.pdf pg_0041.pdf pg_0052.pdf pg_0063.pdf pg_0074.pdf pg_0085.pdf pg_0096.pdf
pg_0009.pdf pg_0020.pdf pg_0031.pdf pg_0042.pdf pg_0053.pdf pg_0064.pdf pg_0075.pdf pg_0086.pdf pg_0097.pdf

4:删除指定页

比如我们要删除第11页,如下:

$ pdftk a.pdf cat 1-10 12-end output o.pdf

或者

$ pdftk A=a.pdf cat A1-10 A12-end output o.pdf

5:提取指定页

将a.pdf中的1,2,3,5页提取创建新文件o.pdf,如下:

$ pdftk a.pdf cat 1-3 5 output o.pdf

6:导出元数据和书签

将PDF文档元数据和书签的报告写入report.txt

$ pdftk a.pdf dump_data output report.txt
  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar