![pdflatex: includepdfmerge(pdf-pages) を使用して生成された pdf のファイルサイズは includegraphics よりもはるかに小さくなります](https://rvso.com/image/420773/pdflatex%3A%20includepdfmerge(pdf-pages)%20%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6%E7%94%9F%E6%88%90%E3%81%95%E3%82%8C%E3%81%9F%20pdf%20%E3%81%AE%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%B5%E3%82%A4%E3%82%BA%E3%81%AF%20includegraphics%20%E3%82%88%E3%82%8A%E3%82%82%E3%81%AF%E3%82%8B%E3%81%8B%E3%81%AB%E5%B0%8F%E3%81%95%E3%81%8F%E3%81%AA%E3%82%8A%E3%81%BE%E3%81%99.png)
PDF ファイルがありますtest.pdf
。 2 つの異なる方法を使用して同じ PDF を再生成していますincludepdfmerge
。を使用して生成された PDFは元の PDF とほぼ同じですが、 を使用して生成された PDF は元のファイルの 3 ~ 4 倍の大きさであることincludegraphics
がわかりました。 以下は LaTeX コードで、ファイルへのリンクも添付されています。includepdfmerge
includegraphics
方法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.pdf
。test_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です。
答え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
使用されている場合にのみ埋め込まれますincludegraphics
once
パート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に複数回埋め込まれます