Leedberg.com

The online home for Greg Leedberg, since 1995.

Friday, November 18, 2005

How-To: Printing in Emacs Under Windows

For those who don't know, Emacs is one of the most popular text editors available for Unix-based operating systems. Emacs, and its competitor vi, have both been around almost as long as Unix itself, and most users of Unix can barely get through a day without using one of them. They are both excellent, in their own ways.

There is also a surpisingly good port of Emacs for Windows operating systems, sometimes called NTEmacs. Emacs is one of the first programs I install in a new Windows installation -- the regular Windows Notepad editor just doesn't cut it for people used to working in Linux.

But, I always had one problem with Emacs under Windows -- printing. I could not for the life of me figure out how to print text files from Emacs. With a little research, I was able to print black-and-white documents, but one of the nice things about Emacs is the syntax highlighting for programming languages. I really wanted to be able to print source code files, with colored syntax highlighting in tact.

After a lot of research and experimentation, I now am able to do this, and will document the steps here.

First, download and install Emacs for Windows, if you don't already have it. It comes in a tarball, so you'll need a decompression program such as WinZip in order to open it.

Now, open up a file you want to print. If you don't know how to use Emacs, I'm not going to cover that here. Emacs has an excellent tutorial that comes with it; the tutorial should be your first reference for the basics of Emacs.

Once you have a file open, press Alt-X. This allows you to enter a command directly into Emacs.

The command we want to enter is:

ps-spool-buffer-with-faces

so type that, and press enter. What this does is create a new buffer in Emacs, which contains the Postscript version of the current buffer. Postscript is a language used by printers, which actually allows you to represent anything that can be printed, including text, colored text, and graphics. If you click on "Buffers" in Emacs, there should now be a Postscript buffer. Click on that, and you'll see the Postscript data that would be used to represent the document you wanted to print. This includes all formatting and color as well!

Now what you need to do is save this Postscript buffer. You can name it whatever you want, but give it a .ps file extension. For example, source.ps.

Now, you have a Postscript file. What you need now are two programs, Ghostscript and GSView. Ghostscript is a system for manipulating Postscript files, and GSView is a graphical program which uses Ghostscript in order to view Postscript files. Download and install them both, if you don't already have them.

Once installed, you can use GSView to open up the .ps file you created. It should open up and look just like it did in Emacs! Success!

Almost.

It may seem that you can now use GSView to print this, but in my experience this doesn't seem to always be the case. I always found that if there was color in the .ps file, GSView would print it in black and white. But, this is fixable!

With the file open in GSView, click on File, Convert to PDF. This will allow you to create a PDF version of this file. PDF is similar in idea to Postscript, but was developed exclusively by Adobe.

Once you have a PDF file, open it using Adobe Acrobat Reader. If you don't already have it, download and install it. It's a useful program that you should get if you don't already have it!

Once opened in Acrobat, it can now be printed, and should retain all color and formatting of the original.

Not a straightforward process, but once you've done it, it's not too bad. I hope it helps other people!