APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Create syntax highlighted PDF and HTML files from C or perl

© October 2009 Girish Venkatachalam
2009/10/05 by Girish Venkatachalam

Girish Venkatachalam is a UNIX hacker with more than a decade of networking and crypto programming experience. His hobbies include yoga,cycling, cooking and he runs his own business. Details here:


People who have a necessity to publish source code for public view like myself and Tony will benefit by this article.

It is rather funny since I have published a lot of code in this very site without syntax highlighting. Perhaps things will change with this newfound wisdom.

Let me quickly cut to the chase. This article comes close on the heels of my Vim article and you shall see in a moment why.

Both my tips involve Vim and its genius is doing a perfect job of this. Let us see for ourselves. We are going to use a C file sqlite2.c for this purpose.

$ vim sqlite2.c
:hardcopy > %.ps

gets us the file sqlite2.c.ps which is a postscript file. If you open it with gv you find that it is neatly syntax highlighted. This will only happen if you have it enabled in your vimrc like this:

sy on
se nu
se textwidth=72
se spell spelllang=en_us
noremap  :se nospell

Now that is an interesting vimrc. But let us focus on the first line alone and get on with the article. The other way to do it is by opening the file in vim and then highlighting the syntax manually with the colon command ":sy on".

But we are not very happy with this for two reasons. It is in ps format which is fine for academic papers and not for Internet publishing. And the second problem with it is that it not suitable for batch conversions. Hackers are incredibly lazy individuals who want the computer to work for them. They don't want to sit and do the job.

What is the way out?

It turns out that there are two ways. One way is interesting. The other a bit boring but both work well.

$ vim -me -e -c ":hardcopy >%.ps" -c ":q" sqlite2.c

works but this is a better way.

$ vim -e -s sqlite2.c < hardcopy.vim

$ cat hardcopy.vim
:hardcopy > %.ps

It is obvious why we prefer the second approach. The first approach takes a bunch of vim commands. You are limited to 10 and it is clumsy since you start vim and kill it!

The second approach is filled with UNIX genius. It has redirection and it is infinitely scalable since your one line vim script can do a lot more than this job alone.

Please spend an hour studying the tutorial for writing vim scripts by typing this colon command ":help script"

Vim scripting has object oriented programming, dictionaries arrays and of course loops.

But can't we do this with the shell?

for file in "foo.c foo.perl bar.c script.sh"
	vim -e -s $file < process.vim

Good. Now there is one itch remaining. How to convert the resulting ps file into PDF?

$ ps2pdf14 sqlite2.c.ps
$ xpdf sqlite2.c.pdf

Happy? No? Then I have another trick for you.

Does looking at this PDF help?

There is a canned plugin that comes with Vim for html conversion. It is called TOhtml. Invoke it inside a vim session with ":TOhtml"

Now put two and two together and come up with your own way to convert C files into syntax highlighted html.

For those of you who hate white backgrounds there is a solution. You can set this in vim.

:se background=dark


running cheetah
Stop spam dead in its tracks!

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Create syntax highlighted PDF and HTML files from C or perl


Inexpensive and informative Apple related e-books:

Take Control of Automating Your Mac

Take Control of Upgrading to El Capitan

Take Control of Parallels Desktop 12

Sierra: A Take Control Crash Course

Take Control of OS X Server

More Articles by © Girish Venkatachalam

Mon Oct 5 14:09:17 2009: 7073   TonyLawrence

It's so obvious how much easier syntax highlighting can make reading, writing, avoiding silly typo's - and yet I've never taken advantage of that.

In fact, when I've happened to be working somewhere where it is turned on by default, I have actually shut it off!

Mon Oct 5 14:24:32 2009: 7074   TonyLawrence

Also: I used that :TOhtml many years back.

It's pretty awful html :-)

Mon Oct 5 14:31:43 2009: 7075   MikeHostetler

GNU Enscript is what I usually use for this task:

Tony: I think once you get with syntax highlighting, you will never go back. But, yes, it's hard to get use to at first. What do you think of colored ls listings?

Mon Oct 5 14:52:00 2009: 7076   TonyLawrence

Don't get me started on Linux colorizing. I hate it, hate it, HATE IT.

Seriously: I do not like it and get very, very annoyed by it.

Mon Oct 5 16:07:40 2009: 7077   TonyLawrence

And most of all, I hate Vim coloring: (link)


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

The history of the world teaches us that succession is dangerous and that the strong take what they want. It's not likely to be any different with Linux. (Tony Lawrence)

Linux posts

Troubleshooting posts

This post tagged:






Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode