pdflatex:使用 includepdfmerge(pdf-pages) 產生的 pdf 檔案大小比 includegraphics 小得多

pdflatex:使用 includepdfmerge(pdf-pages) 產生的 pdf 檔案大小比 includegraphics 小得多

我有一個 pdf 檔test.pdf。我正在使用兩種不同的方法重新生成相同的 pdfincludepdfmergeincludegraphics.我發現使用產生的pdfincludepdfmerge幾乎與原始pdf相同,而使用產生的pdf比includegraphics原始文件大3-4倍。下面是乳膠程式碼,還附上了文件的連結。

方法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。這不是正確的解決方案,但在某種程度上解決了我的需求

我無法弄清楚我的問題的原因。

但因為我必須在我的乳膠專案中使用 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}

在開始頁面和後面的頁面中新增了 \includepdfmerge 使用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}%

最終的 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使用時,字體被嵌入includegraphicsonce

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

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

我發現了一個類似的問題:

當包含使用獨立建立的圖形時,相同的字體嵌入兩次

答案之一說使用乳膠是不可能的。唯一的方法是稍後使用pdfsizeopt https://tex.stackexchange.com/a/83709/221200

也嘗試了建議的解決方案\pdfinclusioncopyfonts=0,但它不起作用,或者字體在 pdflatex 中嵌入多次

相關內容