Mar 01 2010

Printing code and making it look pretty

Category: .code,.opensourceAmit Bahree @ 10:51 pm

If you are on Linux and want to print some code and also make it look pretty then check out a2ps (Any to postscript filter). Of course if you can avoid printing in the first place and saving paper and trees and make it greener that is ideal – however there are times that is not possible. I tried printing from CDT, but the printing options from CDT just looks plain ugly and big fonts and can spread over 10 pages for a simple code file (spanning 293 lines). Sure I can tweak the font in CDT, but that is the only option available – enter a2ps. It seems to have more options, but I have not had a chance to play with those.

For example if I wanted a C++ code file called MOOSSniffer.cpp and “print it” out as PDF then use the command shown below. Here “-E” is the option to make the code look pretty and the “-P pdf” is the option for printing to PDF. Next comes the source file (you can also provide multiple files such as *.cpp) and finally the -o option is for the output filename. Of course you will need to install a2ps, which you can do via System -> Admin -> Synaptic Package Manager

a2ps -E -P pdf MOOSSniffer.cpp -o MOOSSniffer.pdf

Now, for some reason the resulting PDF could not be opened in Acrobat Reader, but on my Ubuntu machine, I could open it using the “Document Viewer” and print it using that. And in case you were curious, the pretty page option came to 3 pages instead of the original 10.

Also no trees were harmed in the making of this post – my printouts were all to PDF and not real paper – but in the end I did print out the 3 page version. :)

Other similar posts you might be interested to check out:
  • April 18, 2010 -- invalid use of incomplete type ‘blah’ (1)
    When you try and compile some code and you get an error along the lines of invalid use of an incomplete type 'whatever type' then in most cases it means you need to include the header file where that type is displayed. For example I had the following events in my header file: [sourcecode lang="cpp" toolbar="false"] protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); [/sourcecode] When when I tried to comp...
  • April 5, 2010 -- Finding an element in a list (0)
    Often you need to search through an array or list to find a specific element and of course you need this search to be as fast and efficient as possible. One of the best ways to do this is using a binary predicate function. A binary function is a function object (which are also called Functors) and is any object which can be called as if it was a function. Depending on your language and platform of choice, Function objects are also known as callback functions, function pointers and delegates (...
  • April 4, 2010 -- Deleting folder on Linux (3)
    If you are a newbie to Ubuntu like me (or any other Unix distro) and you tried deleting a folder which is not empty contains files or subdirectories then you get the annoying error "Directory not empty". To delete such a folder (from a terminal) use the rm -rf command. For example to delete a folder called amitbahree run the following: rm -rf amitbahree/...

Tags: ,

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes