Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
syntax_highlight <source_file> | lpr

I have about 20 Python scripts that I wish to print as PDF. I wish to print them the way GEdit would allow me to, with:

  • Syntax Highlighting
  • Line Numbering
  • Page Headers

Is there any way I can achieve this through the terminal or in any other way which allows for batch printing?

One way is to write a small script to wrap it within alltt or verbatim and compile it with LaTex. But this seems like an overkill.

I have read the question on SuperUser.SE but all the solutions print the code as if it were a story book; no syntax highlighting or any of the above mentioned requirements. Another similar question on SuperUser.SE uses Mac OSX Automator.

share|improve this question

marked as duplicate by Gilles, Ulrich Dangel, Renan, jasonwryan, warl0ck Sep 7 '12 at 5:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Given your requirements, a simple wrapper that replaces FILENAMEHERE with $1 in a nicely prepared template LaTeX file doesn't seem like overkill to me; especially given the special packages for source code (see these answers at tex.stackexchange.com). – sr_ Apr 17 '12 at 14:59
up vote 2 down vote accepted

You can use a2ps. With the -o OUTPUTFILE.ps option, it produces a postscript file, which then can be converted to pdf using e.g. ps2pdf (from the ghostscript package). Line numbering can be enabled with --line-numbers=1. I do not know if the syntax highlighter of a2ps supports recent python standards.

If you prefer the LaTeX solutions, I would recommend the listings package.

share|improve this answer
a2ps is a very interesting package! – user14517 Apr 17 '12 at 19:49