# Source Code using listings produce very large PDF

Taking a 2284 line source file (language=C) and embedding into a LaTeX document using \usepackage{listings} produces a PDF of around 150KB. The original source file is 78KB.

By itself this is acceptable, but I'm building a complete project document with hundreds of source files. My final document is now around 22MB of source listings!

Previously I've used GNU Enscript to produce postscript output from source. Using the same source file as above, Enscript produced a postscript file of 211KB, and then ps2pdf produced a PDF of 50KB; about a third the size of 'listings' package.

Below are my 'listings' settings. I'm using pdflatex and MiKTeX 2.9. Is there anything I can change to help reduce the file size? Why the disparity between Enscript and 'listings'?

\documentclass{article}

\usepackage{listings}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{
language=C,
basicstyle=\footnotesize\ttfamily,
keywordstyle=\color{blue},
stringstyle=\color{mauve},
numbers=left
}

\begin{document}
\lstinputlisting{my_main.c}
\end{document}


Thanks, Chris

====

Edit: To add difference between postscript outputs.

Please see the postscript outputs below. I created a short 'hello world' C file which I ran through both Enscript and LaTeX (via listings) to produce some postscript output for comparison.

The Enscript output appears more concise, whilst the LaTeX (listings) is rather fragmented, and I can only conclude is what is greatly increasing my final PDF sizes. Does this postscript output from 'listings' look correct?

C code:

#include <stdio.h>

int main(int)
{
/* This is a comment, can you see me in LaTeX? */
printf("Hello World!\n");
return 1;
}


Enscript postscript output:

<snip>
/Courier-Oblique 8 8 SUF
0.133333 0.545098 0.133333 setrgbcolor
19.4 732 M
(/* This is a comment, can you see me in LaTeX? */) s
/F-gs-font 8 8 SF
0 setgray
5 723 M
(   printf$$) s /Courier-Bold 8 8 SUF 0 setgray 53 723 M ("Hello World!\\n") s /F-gs-font 8 8 SF 0 setgray 129.8 723 M ($$;) s
5 714 M
(   ) s


LaTeX listings postscript output:

TeXcolorrgb Black 0 0.6 0 TeXcolorrgb 216 w Fb(/)5
b(*)p Black 0 0.6 0 TeXcolorrgb Black 0 0.6 0 TeXcolorrgb
53 w(T)h(h)g(i)g(s)p Black 0 0.6 0 TeXcolorrgb Black
0 0.6 0 TeXcolorrgb 53 w(i)f(s)p Black 0 0.6 0 TeXcolorrgb
Black 0 0.6 0 TeXcolorrgb 51 w(a)p Black 0 0.6 0 TeXcolorrgb
Black 0 0.6 0 TeXcolorrgb 50 w(c)g(o)g(m)g(m)g(e)g(n)g(t)p
Black 0 0.6 0 TeXcolorrgb 21 w(,)p Black 0 0.6 0 TeXcolorrgb
Black 0 0.6 0 TeXcolorrgb 50 w(c)g(a)g(n)p Black 0 0.6 0
TeXcolorrgb Black 0 0.6 0 TeXcolorrgb 55 w(y)g(o)g(u)p
Black 0 0.6 0 TeXcolorrgb Black 0 0.6 0 TeXcolorrgb 54
w(s)g(e)g(e)p Black 0 0.6 0 TeXcolorrgb Black 0 0.6 0
TeXcolorrgb 54 w(m)g(e)p Black 0 0.6 0 TeXcolorrgb Black
0 0.6 0 TeXcolorrgb 52 w(i)g(n)p Black 0 0.6 0 TeXcolorrgb
Black 0 0.6 0 TeXcolorrgb 53 w(L)h(a)g(T)g(e)g(X)p Black
0 0.6 0 TeXcolorrgb 11 w(?)p Black 0 0.6 0 TeXcolorrgb
Black 0 0.6 0 TeXcolorrgb 51 w(*)f(/)p Black 396 917
a Fc(6)217 b Fb(p)6 b(r)g(i)g(n)g(t)g(f)11 b($$)p 0.58 0 0.82 TeXcolorrgb 8 w(")p Black 0.58 0 0.82 TeXcolorrgb 10 w(H)6 b(e)g(l)g(l)g(o)p Black 0.58 0 0.82 TeXcolorrgb 10 w( )p Black 0.58 0 0.82 TeXcolorrgb 10 w(W)g(o)g(r)g(l)g(d)p Black 0.58 0 0.82 TeXcolorrgb 11 w(!)f(\$$p Black 0.58 0 0.82
TeXcolorrgb 9 w(n)p Black 0.58 0 0.82 TeXcolorrgb 7 w(")p
Black 9 w(\))g(;)

-
There are some additional color changes in listings but beside this I don't see any reason why listings should add more to the file size than normal text of the same length. In small files there can substantial (relative to the file size) differences due to font embedding but in larger files this shouldn't matter much. Are you sure sure 22MB are from the listings and not from e.g. a graphic or logo? – Ulrike Fischer Nov 21 '12 at 12:39
@UlrikeFischer My apologies. 22MB included graphics and logos. Removing these, so just source code, and the PDF is 16.6MB. Likewise, a PDF of just graphics and logos (no source) is 4MB. – chris Nov 21 '12 at 13:14
@UlrikeFischer I'm including 302 .h and .c files, which total just over 2MB themselves. I've been experimenting with Enscript (then ps2pdf) and \includepdf, but would prefer to use 'listings' to keep my workflow shorter. – chris Nov 21 '12 at 13:22
Well I tried with a larger c-file which I included about 30 times: around 2MB C-code gave about 1180 pages and 3,8MB. The source of your problem lies imho somewhere else. – Ulrike Fischer Nov 21 '12 at 14:15
@UlrikeFischer I've edited my original question to show what I think is the problem. The postscript output by LaTeX (via listings) is very fragmented. – chris Nov 21 '12 at 14:51

This morning I stumbled across this thread: How to create small PDF files for the Internet

Using the following settings pretty much halved my final PDF size:

\pdfminorversion=5
\pdfobjcompresslevel=3
\pdfcompresslevel=9


Not the answer I originally set out for, but works for me.

-