Monday, January 28, 2013

Export source code listing to PDF

On Linux, enscript is a tool that can be used to generate source code listing to PDF. For example, I have a C++ source code file named area.cpp and I want to export the source code to PDF format with full syntax highlighting and line numbers.

The following command by enscript can be used:
enscript -Ecpp --color -C -o area.ps area.cpp

Here is the explanation of the above parameters.
-Ecpp: turn on syntax highlighting for C++ style.
--color: export with color fonts.
-o area.ps: save to a PostScript file named area.ps.
-C: include line numbers

To generate PDF, we can simply call
ps2pdf area.ps

Some other interesting options that you can play with:
-G, --fancy-header: add a nice header to each page.
--continuous-page-numbers: no reset of page numbers for each source file.