标题: PDF合并方案 创建: 2024-02-27 19:51 更新: 2024-02-29 11:33 链接: https://scz.617.cn/misc/202402271951.txt -------------------------------------------------------------------------- 目录: ☆ 背景介绍 ☆ ghostscript 1) For Linux 2) For Windows ☆ PDF24 ☆ PDFPatcher(PDF补丁丁) ☆ Smallpdf 1.24.2.0 ☆ PDF-XChange Editor Plus (PDFXEdit) ☆ MuPDF 1) For Linux 2) For Windows ☆ 其他讨论 ☆ 后记 -------------------------------------------------------------------------- ☆ 背景介绍 场景一,考试前下发多个学习资料,想将多个PDF合并成单个PDF,便于搜索。 场景二,「图书馆文献传递」获取同一电子书各段PDF,需合并成整书。 场景三,「超星PDF阅读」获取同一电子书多个矢量PDF,需合并成整书。 还有许多场景涉及PDF合并。本文提及PDF合并时,暗含压缩且同时保画质的要求。后 文所有PDF合并均基于同一测试样本集,蔡志忠的「漫画成语故事1」,共291个矢量 PDF,要求合并成一个all.pdf。 -------------------------------------------------------------------------- PDFsam Basic https://pdfsam.org/ https://pdfsam.org/download-pdfsam-basic/ -------------------------------------------------------------------------- 只说PDF合并,免费的PDFsam Basic即可,但这是垃圾方案,合并后all.pdf有241MB。 相比之下,Adobe Acrobat XI Pro合并、优化、压缩,这一套流程下来,15.4MB;合 并39MB,优化31.1MB,压缩15.4MB。实测,用PDFsam合并后的PDF,可用Acrobat优化、 压缩至15.4MB。 有全功能版Acrobat,就不必瞎折腾其他方案了。本文只讨论PC版本地免费软件方案, 完全不考虑在线方案、联网方案、云端方案之类的。不考虑野鸡应用,有些人这辈子 都不明白啥叫野鸡应用,不解释。本文未讨论给PDF加挂书签的事,非我刚需。 ☆ ghostscript 1) For Linux From ZZ & UID(5615345989) apt-get install ghostscript qpdf gs -dCompatibilityLevel=1.7 -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -sOutputFile=/tmp/all.pdf $(ls *.pdf | sort -n) 用/ebook合并出来33.1MB,大致相当于Acrobat的优化。Acrobat的压缩是损了画质的, 不能简单比大小。 gs -dCompatibilityLevel=1.7 -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/default -sOutputFile=/tmp/all_tmp.pdf $(ls *.pdf | sort -n) qpdf --compress-streams=y --object-streams=generate /tmp/all_tmp.pdf /tmp/all.pdf 用/default合并出来31.7MB,比/ebook还小,画质一样。看了gs帮助文档,本来就不 建议乱设PDFSETTINGS参数。 用qpdf压缩/default结果,得到30.9MB。这个缩减占比可忽略,意义不大。解释得通, qpdf的"--compress-streams=y"与gs的"-dCompressStreams=true"应该是一回事。 当前测试版本如下: $ gs --version 9.55.0 $ qpdf --version qpdf version 10.6.3 gs官方文档: https://ghostscript.com/docs/9.55.0/VectorDevices.htm -------------------------------------------------------------------------- -dPDFSETTINGS=configuration /screen selects low-resolution output similar to the Acrobat Distiller (up to version X) "Screen Optimized" setting. /ebook selects medium-resolution output similar to the Acrobat Distiller (up to version X) "eBook" setting. /printer selects output similar to the Acrobat Distiller "Print Optimized" (up to version X) setting. /prepress selects output similar to Acrobat Distiller "Prepress Optimized" (up to version X) setting. /default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file. NB Adobe has recently changed the names of the presets it uses in Adobe Acrobat Distiller, in order to avoid confusion with earlier versions we do not plan to change the names of the PDFSETTINGS parameters. The precise value for each control is listed in the table above. Please be aware that the /prepress setting does not indicate the highest quality conversion. Using any of these presets will involve altering the input, and as such may result in a PDF of poorer quality (compared to the input) than simply using the defaults. The best quality (where best means closest to the original input) is obtained by not setting this parameter at all (or by using /default). The PDFSETTINGS presets should only be used if you are sure you understand that the output will be altered in a variety of ways from the input. It is usually better to adjust the controls individually (see the table below) if you have a genuine requirement to produce, for example, a PDF file where the images are reduced in resolution. -r resolution Sets the resolution for pattern fills, for fonts that must be converted to bitmaps and any other rendering required (eg rendering transparent pages for output to PDF versions < 14). The default internal resolution for pdfwrite is 720dpi. -dCompressFonts=boolean Defines whether pdfwrite will compress embedded fonts in the output. The default value is true; the false setting is intended only for debugging as it will result in larger output. -dCompressStreams=boolean Defines whether pdfwrite will compress streams other than those in fonts or pages in the output. The default value is true; the false setting is intended only for debugging as it will result in larger output. -------------------------------------------------------------------------- 2) For Windows From UID(6083767801) 有Windows版gs -------------------------------------------------------------------------- https://ghostscript.com/releases/gsdnld.html Ghostscript 10.02.1 for Windows (64 bit) https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs10021/gs10021w64.exe -------------------------------------------------------------------------- 缺省安装到: C:\Program Files\gs\gs10.02.1\ 这种软件从Linux移植而来,不依赖注册表及各种Windows环境变量,可以安装、复制、 卸载,得到便携版,我就这么干的。 cmd没有bash的"$(ls *.pdf | sort -n)",可先构造file.lst文件,其内容形如: -------------------------------------------------------------------------- X:\pdf\1.pdf X:\pdf\2.pdf ... X:\pdf\291.pdf -------------------------------------------------------------------------- 不必手写file.lst,可在资源管理器中全选X:\pdf\下所有文件,按住Shift,鼠标右 键"复制文件路径",再粘贴到file.lst中。不必删除双引号,但最后一行必须回车后 以空行结尾,否则最后一行文件不被合并。 执行如下命令进行PDF合并压缩: "C:\Program Files\gs\gs10.02.1\bin\gswin64c.exe" -dCompatibilityLevel=1.7 -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/default -sOutputFile=X:\some\all.pdf @X:\any\file.lst "X:\Green\gs\bin\gswin64c.exe" -dCompatibilityLevel=1.7 -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/default -sOutputFile=X:\some\all.pdf @X:\any\file.lst 注意file.lst路径前面有个@号,不可缺少。合并结果26.1MB。Windows版gs 10.02.1, Linux版gs 9.55.0,看上去新版gs压缩有改进。 ☆ PDF24 From UID(5941823583) & UID(1640026281) PDF24,一个德国软件,好像是Java开发的。免费不开源,有在线版、离线版,界面 语言有中文、英文。Win7/8用一个版本,Win10/11用另一个版本,别下错了。 -------------------------------------------------------------------------- PDF24 Tools (在线版) https://tools.pdf24.org PDF24 Creator (离线版) https://tools.pdf24.org/en/creator https://creator.pdf24.org/listVersions.php https://download.pdf24.org/pdf24-creator-11.16.0-x64.exe (Win10) -------------------------------------------------------------------------- PDF24的"合并PDF"结果有243MB,相当于PDFsam的合并。"优化PDF"意义不大。"压缩 PDF"可对单一PDF压缩,不一定是PDF24的合并结果。缺省压缩参数如下 DPI:150 图像质量:75 颜色:不变 压缩至36.1MB,放大至232%,仍算清晰。若追求高画质,可调整参数 DPI:200 图像质量:90 颜色:不变 压缩至46.8MB。实测后觉得,若非画质癖,就用缺省参数吧。 PDF24最大的坏处是,离线安装后占用空间较大,不够轻型。若这不是问题的话,可 用。 ☆ PDFPatcher(PDF补丁丁) From UID(2710998114) -------------------------------------------------------------------------- PDFPatcher(PDF补丁丁) https://github.com/wmjordan/PDFPatcher https://github.com/wmjordan/PDFPatcher/releases https://github.com/wmjordan/PDFPatcher/releases/download/v1.0.1/PDFPatcher.1.0.1.4220.7z -------------------------------------------------------------------------- 合并文档 添加文件 选择所有待合并PDF 功能选项 尝试合并PDF文档重复数据以缩小结果文件 生成合并文件 -------------------------------------------------------------------------- 合并、压缩结果35.7MB。PDFPatcher最大的好处是绿色、小巧,展开即用。 ☆ Smallpdf 1.24.2.0 From 小钻风 & UID(1242059990) -------------------------------------------------------------------------- Smallpdf https://smallpdf.com https://download.smallpdf.com/desktop/win/Smallpdf-Installer.exe (x64/1.24.2.0) Smallpdf-Installer.exe SHA256 5b6d7e3c3a474c135dd0d9b310027bd44fcfffcb0ec3c2fd3bd862f640430653 -------------------------------------------------------------------------- Smallpdf 1.24.0离线安装方法及无限试用 - 攻防之间 [2019-03-15] https://www.52pojie.cn/thread-899911-1-1.html -------------------------------------------------------------------------- 官网现在不再显式提供1.24.2.0版Smallpdf-Installer.exe,但实际仍提供下载,前 述下载链接由小钻风提供。此外,可通过SHA256从「微步在线」下载,前提是知道 SHA256。破解方案源自「吾爱破解」,只适用于1.24.x版,之后的新版不适用。 先离线安装,比如断网安装或用PFW阻止联网,我用后者。 netsh.exe advfirewall firewall add rule name="Block Smallpdf" enable=yes dir=out action=block profile=any program="C:\Users\scz\AppData\Local\Smallpdf\Smallpdf.exe" netsh.exe advfirewall firewall show rule name="Block Smallpdf" dir=out verbose 可用wf.msc添加上述规则,注意示例中是我的用户名,需要改成你自己的。 安装快结束时,会自动在浏览器中打开一个页面,安装界面有相应提示。不要理它们, 直接关闭浏览器中刚打开的页面,用Process Explorer或任务管理器杀掉所有 Smallpdf.exe实例,安装已经结束。编辑如下文件: notepad %USERPROFILE%\AppData\Roaming\Smallpdf\preferences.json -------------------------------------------------------------------------- old "system.local_token": null, "system.trial_duration_limit": 5, -------------------------------------------------------------------------- new "system.local_token": "1", "system.trial_duration_limit": 36500, -------------------------------------------------------------------------- 原理是,有全功能试用期5天,改成100年。将preferences.json设置成"只读": attrib +r %USERPROFILE%\AppData\Roaming\Smallpdf\preferences.json attrib %USERPROFILE%\AppData\Roaming\Smallpdf\preferences.json 此破解方案不修改PE,安全无毒,不会惹到杀软。 先Merge到242MB,再Compress到24MB,无法指定压缩参数,但压缩结果清晰度可接受。 Smallpdf比较傻瓜化,适合小白,安装破解过程不太适合小白。 ☆ PDF-XChange Editor Plus (PDFXEdit) -------------------------------------------------------------------------- PDF-XChange Editor Plus (PDFXEdit) https://www.pdf-xchange.com/ https://www.pdf-xchange.com/product/downloads https://downloads.pdf-xchange.com/PDFXVE10.zip (ZIP中是EXE安装包,同时支持32/64位) https://downloads.pdf-xchange.com/PDFXEdit10_Portable_x64.zip (64位便携版) https://downloads.pdf-xchange.com/PDFXEdit10_Portable_x86.zip (32位便携版) https://downloads.pdf-xchange.com/EditorV10.x64.msi (64位MSI安装包) https://downloads.pdf-xchange.com/EditorV10.x86.msi (32位MSI安装包) https://downloads.pdf-xchange.com/PDFXVE8.zip (ZIP中是EXE安装包,同时支持32/64位) https://downloads.pdf-xchange.com/PDFXEdit8_Portable.zip (32位便携版) -------------------------------------------------------------------------- PDFXVE10.zip SHA256 1c0e5c850b479258615aac2b42d0dc62bd6bee5db28838afb791a4907d12bbe2 PDFXEdit10_Portable_x64.zip SHA256 25ddcbd37bc4c4aebcb0512a12d7c8e601f414c3563c06c06013fd3506a59b7f PDFXEditCore.x64.dll (crack) SHA256 574d3ef726c6ba1f7af31823c3820af7d86892b7e760de6817dbc3be0a09fbc4 PDFXVE8.zip SHA256 537f18b5e9c862c2e3b509d6c0f8e5f8b62dcf51abc94279a9129984cd457183 PDFXEdit8_Portable.zip SHA256 7c16fbffce38ab10481201b081efff2ad7affab71c907bca35089d9b1078d63b -------------------------------------------------------------------------- PDF-XChange Editor Plus V8批量激活序列号 - [2023-03-21] https://www.52pojie.cn/thread-1762517-1-1.html V8批量激活序列号: PEP80-rc3yMbRY8jmrX5rbpFof3zXQ/03JMJX+91vO4mF+rsfQ11B3mGMEbFU5whpr1Awc UutsGpmcw5il5Mek+YOdioXs9y5wJWCpQeErTpiJzspDfLkW6aeTWeJSBR8PUvq6znwqt9Wl9Le3hnkm Wc3EMRFfTtdKI1YzZsrT2DPX0G2yjtwWh0U8VbmMIVKYmdJj0V7GVgROMoE9wTL5TvsANFipg9poNeoq K96vJbvNxk6oBAf3UxvntEuwUN890QcBQxxg7oztxBZC9CuTbTVSS6dhhSP4pBtm/1ADOh2gM058mf8C Da/OXdrz5zoFhflNWcLmtA5ILEn6csdvnGuGHY1cdrm0rF/CMXIKnEimHeYp6jDnzT9M1T4X4JbvRw= -------------------------------------------------------------------------- PDFXEdit对中国大陆地区有歧视,需挂线路下载,有官方便携版。 curl --proxy socks5h://: --ciphers DEFAULT --compressed -ksL -I https://downloads.pdf-xchange.com/PDFXVE8.zip 这是商业软件,V8有不用联网激活的序列号,kanxue有V9的破解,52pojie有V10的破 解,小钻风给我找了V10的单DLL破解。据说非序列号破解不完美,有暗桩未拔,不要 用来编辑重要PDF。若只是合并压缩PDF,用V10无妨,否则建议用序列号激活V8。 From UID(6813365764) & 小钻风 文件->新建文档->合并文件为单个PDF->添加文件->酌情调整选项->确定 上一步完成合并,242MB。可不保存,直接优化、压缩: 文件->另存为优化的副本->兼容性设成1.7->确定 默认配置压缩结果21MB。JPEG质量由"中等"改为"高",29.9MB。使用"JPEG2000 质量 中等",17.4MB。还有其他组合,清晰度可接受,不放大对比看不出。 文件->另存为优化的副本->审核空间使用 在此可看到原242MB合并结果的组成成份,比如: Images 35.81MB Fonts 205.24MB 这两项占了大头。作为对比,优化压缩结果的组成成份: Images 18.57MB Fonts 2.13MB 再想缩小尺寸,只能降低图像画质。金山PDF好像完全不处理Fonts。 scz: 上面是V10压缩效果,V8压缩得到26.6MB,没有数量级差异。有洁癖者、需要可靠性 者,推荐用V8。PDFXEdit用来合并压缩PDF有些大材小用,真正的优势是其强大的编 辑能力,只是我用不上。 ☆ MuPDF -------------------------------------------------------------------------- MuPDF https://mupdf.com/ https://mupdf.com/releases/index.html https://mupdf.com/downloads/archive/mupdf-1.23.0-windows.zip https://github.com/ArtifexSoftware/mupdf https://mupdf.readthedocs.io/en/latest/ MuPDF on the command line https://mupdf.readthedocs.io/en/latest/mupdf-command-line.html mutool merge https://mupdf.readthedocs.io/en/latest/mutool-merge.html mutool clean https://mupdf.readthedocs.io/en/latest/mutool-clean.html -------------------------------------------------------------------------- 1) For Linux From UID(2516417790) apt-get install mupdf-tools mutool merge -o /tmp/all_tmp.pdf $(ls *.pdf | sort -n) mutool clean -gggg -l -z -f -i -c -s /tmp/all_tmp.pdf /tmp/all.pdf merge合并到243MB,clean压缩到37.9MB,压缩效果一般。 $ dpkg -l mupdf-tools | grep ^ii ii mupdf-tools 1.19.0+ds1-2 amd64 command line tools for the MuPDF viewer 文档中merge有一堆-O参数: -------------------------------------------------------------------------- [-O options] Comma separated list of output options compress Compress all streams. compress-fonts Compress embedded fonts. compress-images Compress images. linearize Optimize for web browsers. sanitize Sanitize graphics commands in content streams. garbage[=compact|deduplicate] Garbage collect unused objects. With compact the cross-reference table will also be compacted. With deduplicate duplicate objects will also be recombined. -------------------------------------------------------------------------- 实测上述merge参数,无效,还得clean压缩。 2) For Windows Windows版mutool最大的问题是,没法用$(ls *.pdf | sort -n),不支持@file.lst。 相比之下,Windows版gs支持@file.lst。假设这样merge: cd /d X:\pdf\ "X:\Green\MuPDF\mutool.exe" merge -o X:\some\all_tmp.pdf *.pdf merge顺序同"dir /B *.pdf"的输出,并非从1到291递增,不可接受。假设有: -------------------------------------------------------------------------- 1.pdf 2.pdf ... 291.pdf -------------------------------------------------------------------------- "dir /B *.pdf"时,并非数字递增顺序,比如出现了: -------------------------------------------------------------------------- 88.pdf 89.pdf 9.pdf 90.pdf 91.pdf -------------------------------------------------------------------------- 需要从1到291递增,9不应该出现在89与90之间。PowerShell可达目的: Get-ChildItem *.pdf | Sort-Object {[int]($_.BaseName -replace '\D+')} | ForEach-Object { $_.Name } Get-ChildItem *.pdf | Sort-Object {[int]($_.BaseName -replace '\D+')} | ForEach-Object { $_.FullName } 在PowerShell中执行如下命令,完成合并、压缩: -------------------------------------------------------------------------- Set-Location -Path X:\pdf\ $files = Get-ChildItem *.pdf | Sort-Object {[int]($_.BaseName -replace '\D+')} | ForEach-Object { $_.Name } & "X:\Green\MuPDF\mutool.exe" merge -o X:\some\all_tmp.pdf $files & "X:\Green\MuPDF\mutool.exe" clean -gggg -l -z -f -i -c -s X:\some\all_tmp.pdf X:\some\all.pdf -------------------------------------------------------------------------- 用PowerShell增加了复杂度,Windows版mutool不如其他GUI方案直观方便,只合并几 个PDF时还成,待合并PDF较多时,除非"*.pdf"的展开顺序符合预期,否则不推荐 Windows版mutool。 ☆ 其他讨论 From UID(7432902670) 提供一个垃圾测试结果,金山PDF合并完243MB,使用金山自带的压缩,选择低清晰度、 最小体积,压缩后220MB,而且软件还是收费的。 scz: 这是真垃圾啊,这算有效反馈,至少说明这是一款垃圾,可以避坑。 From UID(1727858283) & UID(6140947238) -------------------------------------------------------------------------- Stirling-PDF https://github.com/Frooodle/Stirling-PDF https://github.com/Stirling-Tools/Stirling-PDF/blob/main/LocalRunGuide.md https://github.com/Stirling-Tools/Stirling-PDF/releases -------------------------------------------------------------------------- Docker布署的本地方案,不适用于Windows用户。有exe、jar,exe只是jar的另一种 形态,本质都是jar包。直接执行exe时,假设PATH中有java解释器,找不到时就报错。 若是绿色java环境,可手动执行: java -jar Stirling-PDF.jar java -jar Stirling-PDF-with-login.jar 会侦听: http://localhost:8080 再用浏览器访问上述URL,通过WEB界面使用Stirling-PDF。但是,这没啥用,比如 "Compress PDF"会去找gs,实际是用gs完成PDF压缩,对Windows用户意义不大。有这 折腾劲儿,不如直接用Windows版gs。 ☆ 后记 本文只收录了用同一测试样本集实测过的、尚可一用的各种方案,其他方案未收录。 本文面向最终用户,不面向程序员,Python编程方案无优势。 各种方案合并压缩后的大小随软件版本而变化,纯图片PDF、矢量PDF的压缩效果不一 样,这些差异不特别强调,假设知道自己在说什么。 有全功能版Acrobat,就不必瞎折腾其他方案了。PDFsam这种属于反面例子。Linux用 gs即可,mutool压缩效果不如gs。Windows用户,gs、PDFPatcher、Smallpdf、 PDFXEdit随自己喜欢吧,gs正宗,PDFPatcher小巧,Smallpdf傻瓜化,PDFXEdit功能 强大。若有选择综合症,就用Smallpdf好了。我用PDFXEdit V10较多。