pdflatex: includepdfmerge(pdf-pages) を使用して生成された pdf のファイルサイズは includegraphics よりもはるかに小さくなります

pdflatex: includepdfmerge(pdf-pages) を使用して生成された pdf のファイルサイズは includegraphics よりもはるかに小さくなります

PDF ファイルがありますtest.pdf。 2 つの異なる方法を使用して同じ PDF を再生成していますincludepdfmerge。を使用して生成された PDFは元の PDF とほぼ同じですが、 を使用して生成された PDF は元のファイルの 3 ~ 4 倍の大きさであることincludegraphicsがわかりました。 以下は LaTeX コードで、ファイルへのリンクも添付されています。includepdfmergeincludegraphics

方法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 を使用した 2 番目のスクリプトで大きなサイズの PDF が生成される理由。サイズを制御する方法はありますか。

1000ページ付きのPDFを試す場合、includepdfファイルサイズは使用し1MB、は使用していincludegrahicpsます11MB

一時的な回避策

現時点では試行錯誤の結果、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}

先頭ページとそれ以降のページに\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ファイルには、冒頭に1ページ追加されています。

ファイルサイズは です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

ご覧のとおり、フォントは他の2つのケースでのみ5 times使用されている場合にのみ埋め込まれますincludegraphicsonce

パート2の回答が見つかりません: 回避方法

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

同様の質問を見つけました:

スタンドアロンで作成されたグラフィックを含めると、同じフォントが2回埋め込まれます

答えの1つは、ラテックスを使用するのは不可能だと言っています。唯一の方法は、後でそれを使用することです。pdfsizeopt https://tex.stackexchange.com/a/83709/221200

提案された解決策も試しました\pdfinclusioncopyfonts=0が、機能しないか、フォントがpdflatexに複数回埋め込まれます

関連情報