# pdflatex: filesize of pdf generated using includepdfmerge(pdf-pages) is much smaller that includegraphics

I have a pdf file test.pdf. I am regenerating the same pdf using two different methods includepdfmerge and includegraphics. I found that pdf generated using includepdfmerge is almost same as original pdf where as pdf generated using includegraphics is 3-4 times larger than the original file. The below is the latex code and also attached the link for the files.

METHOD1: using includepdfmerge

OUTPUT FILENAME: test_includepdf.pdf

The script is adding 5 pages from test.pdf

\documentclass[version=3.21]{scrartcl}
\usepackage{pdfpages}
\usepackage[showframe]{geometry}
\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}


METHOD2: using includegraphics

OUTPUT FILENAME: test_includegraphics.pdf

The script is adding 5 pages from test.pdf

\documentclass[parskip=full,version=3.21]{scrartcl}
\usepackage{pdfpages}
\usepackage[showframe]{geometry}
\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}


All the files are available at the link https://filebin.net/1498hwp3fxnngpcs Note:The above link will get expired by Sep-20-2020

The file sizes i got are:

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


As we see that test_includegrahicps.pdf is significantly larger than the test.pdf and test_includepdf.pdf

Why the second script with includegraphics produce large pdfs sizes. Is there any way i can control the size.

Because if i try on a pdf with 1000 pages, then using includepdf filesize is 1MB and using includegrahicps is 11MB

TEMPORARY WORKAROUND

CURRENTLY BASED ON SOME TRAIL AND ERROR, I COULD MAKE THE FILE SIZE SMALLER USING combination of includepdf and includegraphics. This is not the right solution but solves to some extent my need

I could not figure out the reason for my question.

But Because i have to use includegraphics in my latex project and at the same time i dont want huge file sizes, by doing trail and error i figured out the following way which will produce less significantly less file size

Add an additional first page in the pdf which will be called using \includepdfmerge and then the rest of the pages can be called using includegraphics

So i tried

\documentclass[parskip=full,version=3.21]{scrartcl}
\usepackage{pdfpages}
\usepackage[showframe]{geometry}
\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}


The following is the change:

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


Added \includepdfmerge in the beginning page and later pages 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}%


The final pdf file has one extra page in the beginning

NOw the file size is 20kb. The following link is the final output pdf

https://filebin.net/5p2a32kdx80qmvni

It seems to be enough to store the pdf once in temporary box to trigger the font optimization. It is needed only with pdflatex, lualatex does it by default.

\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}

• I never used lualatex. I will try this Sep 14 '20 at 18:13
• Works like a charm. Can i also add multiple pdfs. For gathering more fonts before Sep 14 '20 at 18:19
• Can you explain the syntax \setbox\@tempboxa=\hbox{} How this store the pdf in temporary box. I am new to this Sep 14 '20 at 18:44
• Recently i was doing lot of pdf manipulation. I found about pdflatex and learned about KOMA script, pdfpages, includegraphics which is what I play with for my task. After this answer 45MB pdf size is now 15.1MB significant reduction in size. (its 12,000 pages and avg page size of 6"x6"). So thank you again Sep 14 '20 at 18:50

ANSWER PART1: WHY THE FILE SIZE IF BIG when only using includegraphics

It is due to fonts embedded mutlitple times

FIle made using only 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  File made using only includepdfmerge $ pdffonts test_includepdf.pdf
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
ABCDEE+ScaGoudy                      TrueType          WinAnsi          yes yes no      12  0


FILE made using first page includepdfmerge and rest using includegraphics

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


As it can seen the font is embedded 5 times when i used only includegraphics where other two cases only once

NO ANSWER FOUND FOR PART2: HOW TO AVOID IT

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

I found a similar question:

Same font embedded twice when including graphics created with standalone

One of the answer says using latex its not possible. Only way is to do it later using pdfsizeopt https://tex.stackexchange.com/a/83709/221200

Also tried suggested solution \pdfinclusioncopyfonts=0, but it does not work, or fonts gets embedded mutliple times in pdflatex