Text editor for printing C++ code
Well, if you want to go the extra mile, do it in LaTeX and provide a professional level PDF file. You haven't mentioned your distribution so I'll give instructions for Debian based systems. The same basic idea can be done on any Linux though.
Install a LaTeX system and necessary packages
sudo apt-get install texlive-latex-extra latex-xcolor texlive-latex-recommended
Create a new file (call it
report.tex
) with the following contents:\documentclass{article} \usepackage{fancyhdr} \pagestyle{fancy} %% Define your header here. %% See http://texblog.org/2007/11/07/headerfooter-in-latex-with-fancyhdr/ \fancyhead[CO,CE]{John Doe, Class 123} \usepackage[usenames,dvipsnames]{color} %% Allow color names %% The listings package will format your source code \usepackage{listings} \lstdefinestyle{customasm}{ belowcaptionskip=1\baselineskip, xleftmargin=\parindent, language=C++, breaklines=true, %% Wrap long lines basicstyle=\footnotesize\ttfamily, commentstyle=\itshape\color{Gray}, stringstyle=\color{Black}, keywordstyle=\bfseries\color{OliveGreen}, identifierstyle=\color{blue}, xleftmargin=-8em, showstringspaces=false } \begin{document} \lstinputlisting[style=customasm]{/path/to/your/code.c} \end{document}
Just make sure to change
/path/to/your/code.c
in the penultimate line so that it point to the actual path of your C file. If you have more than one file to include, add a\newpage
and then a new\lstinputlisting
for the other file.Compile a PDF (this creates
report.pdf
)pdflatex report.tex
I tested this on my system with an example file I found here and it creates a PDF that looks like this:
For a more comprehensive example that will automatically find all .c files in the target folder and create an indexed PDF file with each in a separate section, see my answer here.
I'd usually use enscript: something like
$ enscript --highlight=cpp
--header='|Real Name|Class 101'
--footer='|Page $% of $=|'
-poutput.ps *.cpp
will be a start - this writes postscript output to output.ps
, so you can preview and overwrite that while you're tinkering with the config and then print it once you're happy. See the man page for more very extensive options.
EDIT getting the footer to work correctly is a bit of a pain with enscript - I'd never noticed because I've never required it. If you save this file to ~/.enscript/so.hdr
(you probably need to create the directory), you'll actually get the required output with
$ enscript --highlight=cpp
--header='|Real Name|Class 101'
--footer='|Page $% of $=|'
--fancy-header=so
-poutput.ps *.cpp
giving
Roughly,
- LaTeχ is the best quality and the most work to set up,
- enscript or a2ps are intermediate in both quality and work,
- vim's
:hardcopy
command is easy but not that flexible, and - doing syntax highlighting manually in a non-code-aware editor is a lot of effort for a poor return.
You can use the :TOhtml
command in vim. This renders what you see (i.e. syntax highlighting) as html. From there, a web browser that can print to pdf works, as you can usually customize the header/footer content.
This is probably similar to the :hardcopy
command mentioned by Useless, but I can't verify on my system right now.
Another possibility is to print from QtCreator, however there doesn't appear to be a way to set the headers/footers.