a2ps — printing files from linux shell 

Linux shell is a powerful tool, since it is easy and convenient to perform many tasks directly from it. An example of this is sending files to the printer. While there are many ways of achieving this, f.e. to send content as is you could use the lpr or kprinter commands. In this post however, I will concentrate on the use of a small but very useful GNU utility called a2ps. The a2ps program is used to pre-format files for printing on a PostScript printer. This means you can parse different types of files into PostScript (PS) format supported by your printer. This is not all however, a2ps recognises many ASCII based formats (such as TEX files, Matlab/Octive files, C/C++ source code, etc.) and can perform highlighting and styling to make your source code printouts easier to read. a2ps supports many options, you can browse the a2ps man page to get familiar with them.

man a2ps  # ©2007 dsplabs.com.au

OK, lets start by going over a simple example. We will print the following file: resume.tex, which is a resume source code written in LATEX2e. To send this file to your default printer type the following at the shell prompt.

a2ps resume.tex  # ©2007 dsplabs.com.au

Now, depending on the Linux distribution you use, the a2ps command could be aliased to alter its default behaviour. I for one have the following alias in my .bashrc file.

alias a2ps='a2ps -v --line-numbers=1 --font-size=9 --copies=1 --sides=2 ' \
'--landscape --chars-per-line=120 --truncate-lines=1 ' \
'--highlight-level=normal -E --media=A4 -2 --printer=printer'

In the above alias the verbose mode is enabled, printout lines are set to be numbered, the font size is set to 9 point, a single copy gets printed, twosided printing is enabled (i.e. full-duplex compatible mode is selected), landscape mode is set, lines longer than 120 characters are truncated, normal level of source code highlighting is used, pretty printing is enabled, A4 page format is set, two pages per sheet are selected and finally the output is sent to the printer called printer. After running a2ps resume.tex command the following verbose information is printed to standard output.

[resume.tex (TeX): 2 pages on 1 sheet]
request id is printer-1148 (1 file(s))
[Total: 2 pages on 1 sheet] sent to the printer `printer'

This means that the file resume.tex was identified by a2ps as a TEX source code and two pages are sent on a single sheet to the printer. Here is what the printer output should look like provided that you use the same a2ps settings.

resume.tex: a2ps — format files for printing on a PostScript printer

The a2ps resume.tex command has sent the resume.tex to my printer. The electronic copy shown above however, was obtained by first specifying output PS file (resume.ps) to the a2ps command in the following way.

a2ps -o resume.ps resume.tex  # ©2007 dsplabs.com.au

Note that the verbose output has changed to reflect change in destination from printer to file.

[resume.tex (TeX): 2 pages on 1 sheet]
[Total: 2 pages on 1 sheet] saved into the file `resume.ps'

The PS file, resume.ps, can then be converted to a PNG file, resume.png, using ImageMagick as follows.

convert -density 300x300 -resize 670x1000 resume.ps resume.png

Enjoy!


Did you find the above information useful and interesting? If so, please support this site by using the blog directory links at the bottom of this page. Thanks for your support!

If you have any Linux related problems or questions then please feel free to post them on our Linux Forums: http://linux.dsplabs.com.au/forums.




VPS Hosting Referral Code DZZCC3

Add me to Technorati Favorites Vote for me on Blog Skinny Vote for me on Blog Catalog

Leave a Reply