【CPDF】pdf合并、添加页码、切割、指定页插入

github地址:https://github.com/coherentgraphics/cpdf-binaries 开发文档:https://www.coherentpdf.com/cpdfmanual.pdf

1.业务需求

pdf合并AND添加页码

2.搜索

在网上简单搜罗了一番,有使用mpdf、fpdf等等的。当然还有很多付费服务(免费的真香),最后在github上面找到了cpdf这款工具,作者提供了各个平台的可执行文件,并没有开源代码。

3.安装

其实并不需要什么安装过程,直接把可执行文件上传到服务器就行了。 添加可执行权限:

chmod a+x cpdf
4.使用(先切换的文件所在目录)
# 合并pdf
./cpdf -merge test1.pdf test2.pdf -o merge.pdf

# 添加页码
cpdf -add-text "-%Page-" -font "Times-Roman" -bottom 10  in.pdf -o oout.pdf

# 合并pdf并添加页码
cpdf -merge test1.pdf test2.pdf AND -add-text "-%Page-" -font "Times-Roman" -bottom 10 -o merge_result.pdf

# 添加页码按照指定的开始页码
# 从20开始设置页码
cpdf out.pdf -add-text "%Bates" -bates 20 -o set_start_page_num.pdf

添加空白页:
# 在最开始添加一个空白页
cpdf -pad-before volume.pdf -o volume_add_black_page.pdf

# 在最后添一个加空白页
cpdf -pad-after volume.pdf -o out.pdf

# 如果一个文件有2页,我们要的结果是5页的文件,需要在末尾填充3页就可以使用-pad-multiple,结果就是后面的3页是空白页(如果需要在文件开始填充使用-pad-multiple-before)
cpdf -pad-multiple 5 in.pdf -o out.pdf

# 切割 in.pdf 的 1-3 页以及 12 至最后页并输出到 out.pdf
$ cpdf in.pdf 1-3,12-end -o out.pdf
PS:此外还有一种是使用gs
# 安装gs
wget https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs925/ghostscript-9.25.tar.gz
# 解压
tar zxvf ghostscript-9.25.tar.gz
# 编译
cd ghostscript-9.25
./configure --prefix=/usr
make all
make install

# 合并pdf
 gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf test1.pdf test2.pdf 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×