pdflatex:使用 includepdfmerge(pdf-pages) 生成的 pdf 文件大小比 includegraphics 小得多

pdflatex:使用 includepdfmerge(pdf-pages) 生成的 pdf 文件大小比 includegraphics 小得多

我有一个 pdf 文件test.pdf。我正在使用两种不同的方法重新生成相同的 pdf includepdfmergeincludegraphics我发现使用生成的 pdfincludepdfmerge与原始 pdf 几乎相同,而使用生成的 pdfincludegraphics比原始文件大 3-4 倍。以下是 latex 代码,还附上了文件的链接。

方法 1:使用 includepdfmerge

输出文件名:test_includepdf.pdf

该脚本正在从 test.pdf 添加 5 页

\documentclass[version=3.21]{scrartcl}
\usepackage{pdfpages}
\usepackage[showframe]{geometry}
\usepackage[automark,headsepline=false,footsepline=false]{scrlayer-scrpage}
\pagestyle{empty}
\begin{document}
\newgeometry{layoutwidth = 432pt,layoutheight = 33.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}
\includepdfmerge[fitpaper,templatesize={432pt}{33.87999pt}]{test.pdf, 1}
\newgeometry{layoutwidth = 432pt,layoutheight = 33.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}
\includepdfmerge[fitpaper,templatesize={432pt}{33.87999pt}]{test.pdf, 2}
\newgeometry{layoutwidth = 432pt,layoutheight = 33.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}
\includepdfmerge[fitpaper,templatesize={432pt}{33.87999pt}]{test.pdf, 3}
\newgeometry{layoutwidth = 432pt,layoutheight = 33.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}
\includepdfmerge[fitpaper,templatesize={432pt}{33.87999pt}]{test.pdf, 4}
\newgeometry{layoutwidth = 432pt,layoutheight = 33.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}
\includepdfmerge[fitpaper,templatesize={432pt}{33.87999pt}]{test.pdf, 5}
\end{document}

方法 2:使用 includegraphics

输出文件名:test_includegraphics.pdf

该脚本正在从 test.pdf 添加 5 页

\documentclass[parskip=full,version=3.21]{scrartcl}
\usepackage{pdfpages}
\usepackage[showframe]{geometry}
\usepackage[automark,headsepline=false,footsepline=false]{scrlayer-scrpage}
\pagestyle{empty}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt}
\setlength{\baselineskip}{0pt}
\begin{document}
\KOMAoptions{paper=432pt:33.87999pt,DIV=calc}%
\recalctypearea%
\newgeometry{layoutwidth = 432pt,layoutheight = 33.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}%
\includegraphics[width=\textwidth,keepaspectratio,page=1]{test.pdf}%
\KOMAoptions{paper=432pt:33.87999pt,DIV=calc}%
\recalctypearea%
\newgeometry{layoutwidth = 432pt,layoutheight = 33.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}%
\includegraphics[width=\textwidth,keepaspectratio,page=2]{test.pdf}%
\KOMAoptions{paper=432pt:33.87999pt,DIV=calc}%
\recalctypearea%
\newgeometry{layoutwidth = 432pt,layoutheight = 33.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}%
\includegraphics[width=\textwidth,keepaspectratio,page=3]{test.pdf}%
\KOMAoptions{paper=432pt:33.87999pt,DIV=calc}%
\recalctypearea%
\newgeometry{layoutwidth = 432pt,layoutheight = 33.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}%
\includegraphics[width=\textwidth,keepaspectratio,page=4]{test.pdf}%
\KOMAoptions{paper=432pt:33.87999pt,DIV=calc}%
\recalctypearea%
\newgeometry{layoutwidth = 432pt,layoutheight = 33.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}%
\includegraphics[width=\textwidth,keepaspectratio,page=5]{test.pdf}%
\end{document}

所有文件均可在以下链接获取https://filebin.net/1498hwp3fxnngpcs 注意:以上链接将于 2020 年 9 月 20 日过期

我得到的文件大小为:

 test.pdf   19 kB
 test_includepdf.pdf    20 kB
 test_includegraphics.pdf   76 kB

正如我们所见,这test_includegrahicps.pdf明显大于test.pdftest_includepdf.pdf

为什么第二个包含 includegraphics 的脚本会产生较大的 pdf 大小。有什么方法可以控制大小吗?

因为如果我尝试在有页面的 pdf 上1000,那么使用includepdf文件大小是1MB,使用includegrahicps11MB

临时解决方法

includepdf目前,根据一些尝试和错误,我可以使用和的组合来减小文件大小includegraphics。这不是正确的解决方案,但在一定程度上解决了我的需求

我不知道我提出这个问题的原因。

但是因为我必须在 latex 项目中使用 includegraphics,同时我又不想要很大的文件大小,所以通过反复试验,我找到了以下方法,这将显著减少文件大小

在 pdf 中添加一个额外的第一页,该第一页将使用 调用\includepdfmerge,然后其余页面可以使用 调用includegraphics

所以我尝试了

\documentclass[parskip=full,version=3.21]{scrartcl}
\usepackage{pdfpages}
\usepackage[showframe]{geometry}
\usepackage[automark,headsepline=false,footsepline=false]{scrlayer-scrpage}
\usepackage{xcolor}
\color[RGB]{84,84,84}
\pagestyle{empty}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt}
\setlength{\baselineskip}{0pt}
\begin{document}
%%%%%%%% ADDITIONAL PAGE IN THE START CALLED USING includepdfmerge
\newgeometry{layoutwidth = 432pt,layoutheight = 33.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}
\includepdfmerge[fitpaper,templatesize={432pt}{33.87999pt}]{test.pdf, 1}
%%%%%%  LATER PAGES CALLED USING INCLUDEGRAPHICS
\KOMAoptions{paper=432pt:38.87999pt,DIV=calc}%
\recalctypearea%
\newgeometry{layoutwidth = 432pt,layoutheight = 38.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}%
\includegraphics[width=\textwidth,keepaspectratio,page=1]{test.pdf}%
\KOMAoptions{paper=432pt:38.87999pt,DIV=calc}%
\recalctypearea%
\newgeometry{layoutwidth = 432pt,layoutheight = 38.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}%
\includegraphics[width=\textwidth,keepaspectratio,page=2]{test.pdf}%
\KOMAoptions{paper=432pt:38.87999pt,DIV=calc}%
\recalctypearea%
\newgeometry{layoutwidth = 432pt,layoutheight = 38.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}%
\includegraphics[width=\textwidth,keepaspectratio,page=3]{test.pdf}%
\KOMAoptions{paper=432pt:38.87999pt,DIV=calc}%
\recalctypearea%
\newgeometry{layoutwidth = 432pt,layoutheight = 38.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}%
\includegraphics[width=\textwidth,keepaspectratio,page=4]{test.pdf}%
\KOMAoptions{paper=432pt:38.87999pt,DIV=calc}%
\recalctypearea%
\newgeometry{layoutwidth = 432pt,layoutheight = 38.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}%
\includegraphics[width=\textwidth,keepaspectratio,page=5]{test.pdf}%
\end{document}

变更内容如下:

\newgeometry{layoutwidth = 432pt,layoutheight = 33.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}
\includepdfmerge[fitpaper,templatesize={432pt}{33.87999pt}]{test.pdf, 1}

在开始页面和后续页面中添加 \includepdfmergeincludegraphics

\KOMAoptions{paper=432pt:38.87999pt,DIV=calc}%
\recalctypearea%
\newgeometry{layoutwidth = 432pt,layoutheight = 38.87999pt,left=0mm,right=0mm,top=0mm, bottom=0mm}%
\includegraphics[width=\textwidth,keepaspectratio,page=1]{test.pdf}%

最终的 PDF 文件开头有一个额外的页面

现在文件大小为20kb。以下链接是最终输出的pdf

https://filebin.net/5p2a32kdx80qmvni

答案1

似乎将 pdf 存储在临时框中一次就足以触发字体优化。只有使用 pdflatex 时才需要它,lualatex 默认会这样做。

\documentclass{article}
\usepackage{graphicx}
\pagestyle{empty}
\makeatletter
\setbox\@tempboxa=\hbox{\includegraphics{example-image-a4-numbered}}% font is included only once
\makeatother
\begin{document}

\includegraphics[width=\textwidth,keepaspectratio,page=1]{example-image-a4-numbered}%
\newpage
\includegraphics[width=\textwidth,keepaspectratio,page=2]{example-image-a4-numbered}%
\newpage
\includegraphics[width=\textwidth,keepaspectratio,page=3]{example-image-a4-numbered}%
\newpage
\includegraphics[width=\textwidth,keepaspectratio,page=4]{example-image-a4-numbered}%
\newpage
\includegraphics[width=\textwidth,keepaspectratio,page=5]{example-image-a4-numbered}%
\end{document}

答案2

回答第 1 部分:为什么仅使用 includegraphics 时文件大小会很大

这是由于多次嵌入字体

仅使用includegraphics

$ pdffonts test_includegraphics.pdf 
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
ABCDEE+ScaGoudy                      TrueType          WinAnsi          yes yes no       9  0
ABCDEE+ScaGoudy                      TrueType          WinAnsi          yes yes no      19  0
ABCDEE+ScaGoudy                      TrueType          WinAnsi          yes yes no      31  0
ABCDEE+ScaGoudy                      TrueType          WinAnsi          yes yes no      41  0
ABCDEE+ScaGoudy                      TrueType          WinAnsi          yes yes no      51  0

仅使用以下方式制作的文件includepdfmerge

$ pdffonts test_includepdf.pdf 
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
ABCDEE+ScaGoudy                      TrueType          WinAnsi          yes yes no      12  0

使用第一页制作的文件includepdfmerge,其余部分使用includegraphics

$ pdffonts test_mix.pdf       
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
ABCDEE+ScaGoudy                      TrueType          WinAnsi          yes yes no      15  0

正如所见,5 times当我仅使用includegraphics其他两种情况时,字体才会嵌入once

第 2 部分未找到答案:如何避免

But now the question is how can we avoid font getting embedded mutliple times when made using only includegraphics

我发现了类似的问题:

包含使用独立创建的图形时,相同的字体嵌入两次

其中一个答案说使用 latex 是不可能的。唯一的方法是稍后使用pdfsizeopt https://tex.stackexchange.com/a/83709/221200

还尝试了建议的解决方案\pdfinclusioncopyfonts=0,但它不起作用,或者字体在 pdflatex 中多次嵌入

相关内容