您观察到的一些大小优化可能来自Ghostscript清理未使用对象,其最近获得的字体优化改进(您是否使用最新版本的GS?!)以及可能发生的图像重新/下采样。
如果用于PDF-> PDF转换,则Ghostscript基本上会按以下方式操作:
1.读取带有所有对象的输入文件并将它们转换为其内部格式,以进行图形页面表示。
2.对内部格式中的页面内容执行在命令行上要求的操作。
3.写出一个全新的PDF。
这意味着对于大多数PDF-> PDF操作,您将拥有不同的PDF对象排序和编号,甚至对象的内部代码也可能已更改(即使您的眼睛没有发现输入和输出PDF之间的任何差异)。
默认情况下,Ghostscript还将压缩任何在原始文件中未经压缩的对象流(但这是无损压缩)。
现在,对于你非常简单的命令行,不包含任何操作要求,Ghostscript会默认使用
-dPDFSETTINGS=/default
参数,并相应地进行操作。请注意,保留HTML标签。
那么,
/default
PDFSETTINGS是什么?你有两个选项来找出答案:
Read the manual. The large table in middle of this section gives an overview. You can see that this one -dPDFSETTINGS=/default
in itself is just a shorthand for the several dozen other more specific settings which it represents. The link to the documentation given is for current HEAD of the development code and your actually used version may be different of course.
Query (your own) Ghostscript for the detailed meaning of this setting. My answers to question 'Querying Ghostscript for the default options/settings of an output device...' and question 'What are PostScript dictionaries, and how can they be accessed (via Ghostscript)?' do elaborate a bit more on this. In short, to query Ghostscript for the details of its /default
PDFSETTINGS, run this command:
gs \
-q \
-dNODISPLAY \
-c ".distillersettings /default get {exch ==only ( ) print ===} forall quit"
You should get a result very similar to this:
/Optimize false
/DoThumbnails false
/PreserveEPSInfo true
/ColorConversionStrategy /LeaveColorUnchanged
/DownsampleMonoImages false
/EmbedAllFonts true
/CannotEmbedFontPolicy /Warning
/PreserveOPIComments true
/GrayACSImageDict << /HSamples [2 1 1 2] /VSamples [2 1 1 2] /QFactor 0.9 /Blend 1 >>
/DownsampleColorImages false
/PreserveOverprintSettings true
/CreateJobTicket false
/AutoRotatePages /PageByPage
/NeverEmbed [/Courier /Courier-Bold /Courier-Oblique /Courier-BoldOblique /Helvetica /Helvetica-Bold /Helvetica-Oblique /Helvetica-BoldOblique /Times-Roman /Times-Bold /Times-Italic /Times-BoldItalic /Symbol /ZapfDingbats]
/ColorACSImageDict << /HSamples [2 1 1 2] /VSamples [2 1 1 2] /QFactor 0.9 /Blend 1 >>
/DownsampleGrayImages false
/UCRandBGInfo /Preserve
The only point that stands out from these: you may want to change /AutoRotagePages
from /PageByPage
to /None
. On the commandline you would put it as -dAutoRotatePages=/None
.
To give you a complete list of parameters which would specifically tell Ghostscript to employ as much of a passthrough mode as it possibly can to the input PDF by adding these parameters:
-dAntiAliasColorImage=false \
-dAntiAliasGrayImage=false \
-dAntiAliasMonoImage=false \
-dAutoFilterColorImages=false \
-dAutoFilterGrayImages=false \
-dDownsampleColorImages=false \
-dDownsampleGrayImages=false \
-dDownsampleMonoImages=false \
-dColorConversionStrategy=/LeaveColorUnchanged \
-dConvertCMYKImagesToRGB=false \
-dConvertImagesToIndexed=false \
-dUCRandBGInfo=/Preserve \
-dPreserveHalftoneInfo=true \
-dPreserveOPIComments=true \
-dPreserveOverprintSettings=true \
所以您可以尝试这个命令:
gs \
-o output.pdf \
-sDEVICE=pdfwrite \
-dAntiAliasColorImage=false \
-dAntiAliasGrayImage=false \
-dAntiAliasMonoImage=false \
-dAutoFilterColorImages=false \
-dAutoFilterGrayImages=false \
-dDownsampleColorImages=false \
-dDownsampleGrayImages=false \
-dDownsampleMonoImages=false \
-dColorConversionStrategy=/LeaveColorUnchanged \
-dConvertCMYKImagesToRGB=false \
-dConvertImagesToIndexed=false \
-dUCRandBGInfo=/Preserve \
-dPreserveHalftoneInfo=true \
-dPreserveOPIComments=true \
-dPreserveOverprintSettings=true \
input1.pdf \
input2.pdf
最后,正如Chris Haas已经暗示的那样:如果您特别不想要Ghostscript默认应用的任何优化,您也可以使用pdftk
。 pdftk
无法执行这些操作,但相对操作简单,速度较快(但生成的文件大小可能比Ghostscript更大)。
pdftk
在 CentOS/RHEL 7 上不可用。 - a codergs
后丢失页面并且文件大小增加了10倍的情况。而pdftk
只是简单地进行合并,没有做任何修改,同时速度也更快。 - admirabilis