TILiE (Today I’m Learning in Emacs): Printing & Exporting and Publishing in Org-mode

GNU Emacs with GTK toolkit

Figure 1: Attribution: en:User:Petteri (via CC Attribution-Share Alike 3.0 Unported)

Link to original image | Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-SA 3.0)

I’m still feeling quite sluggish, really since yesterday, hence my earlier post. Still, I want to write something today because I learned a couple neat things in Emacs around printing. I’m also trying to understand how the Exporting and Publishing functions work in Org-mode.

Printing from Emacs and Lpr

I started to dig into this in the usual way: I tried to do something in Emacs and it didn’t work initially because it wasn’t configured. I noticed that, in the message buffer, Emacs was trying to unsuccessfully call:


and I was getting an error that the default printer wasn’t set. This led me to looking at the man page for lpr which is terse to say the least. However, the man page can at least give you a sense of what can be done. It looked like you could specify a default printer by setting an environment variable called either PRINTER or LPDEST. I managed to to find a bit more information on the Oracle site and started out by running the following command from my Bash shell:

$ export PRINTER=printer-name

where printer-name is the name of my printer (an HP Officejet in my case). This got (single-sided) printing going on Emacs but only within that Bash shell session. In other words, it worked when I called Emacs from that Bash shell:

emacs /file/ &

but not from another Bash shell or if I opened Emacs another way. Now, I want this printer to be the default used by lpr (and Emacs) no matter how I use this print command. How to do that? I figured out that you can check your default printer via:

lpstat -d

and I determined that I didn’t have one set. I did more digging and after a few false starts I found the Command-Line Printing and Options page on CUPS.org. Here I finally found the way to set a global default printer:

lpoptions -d /printer/

where printer is the name of the printer you normally use. This information was actually on the Oracle page too but I missed it initially. 😦 Now, when I run lpstat -d I get:

system default destination: HP-HP-Officejet-Pro-8620

and I can print from Emacs no matter how I start it. Hurray!

Now, because I can never settle for half measures, I wanted to set up my default printing to be duplex to save paper. Back to the Command-Line Printing and Options page on CUPS.org and they actually specifically outline what I wanted to do:

lpoptions -p /LaserJet/duplex/ -o sides=two-sided-long-edge

where LaserJet/duplex is your printer instance. I don’t quite understand what they mean by instances or why you would want them but it worked quite well when I replaced LaserJet/duplex with just my printer name (and no ‘/<instance>’ after it). I assume you might set up an instance of a printer if you want a certain set of options to be used in certain cases only. However, I’m good with all printing going through lpr to be duplex. Success!

Next, I started playing around with printing from Emacs now that I actually was getting output. Earlier, I discovered Printing Hard Copies in Section 39 of the GNU Emacs Manual. This allowed me to start with some basic black-and-white printing. However, with a bit of searching, I was able to find the Postscript Hardcopy in Section 39.1 of the GNU Emacs Manual. Now here are some interesting options! The Emacs command:

M-x ps-print-buffer-with-faces

lets me print my current buffer but with colours and other formatting features. In addition, if I want a to create a PostScript file (a .ps file) from the buffer I do the following:

M-x ps-spool-buffer-with-faces


C-u M-x ps-despool

which then prompts me for a file name and then saves the file. Perfect! 🙂

Converting the file to the more commonly-used (?) PDF format is a matter of running the Linux command:

ps2pdf your_file.ps converted_file.pdf

You can also skip the PDF file name and ps2pdf defaults to the same file name as used in the .ps file. You can see a useful article about ps2pdf here which will give you more detail if you want.

In conclusion, I now have a working solution to print from Emacs either to a printer or to a file. W00t!

Still To Do: Exporting and Publishing in Org-mode

Unfortunately, I haven’t had too much success around the exporting an org note to PDF yet. I seem to run afoul of some sort of LaTex issue that prevents me converting the file to PDF. This will require more research to see what’s not working. (TODO)

I have managed to export to HTML so that is something. I still haven’t figured out a clean way to convert an HTML page to PDF, short of using the “print to file” option in a browser which sometimes looks funny. I guess I have more work to do here. (TODO)

Whew! Is Everyone’s Brain Mush Now?

I guess that’s enough plodding along on this topic for now. Thanks for reading the whole thing! I’d get you a cookie but it would be virtual anyway.

Chocolate Chip Cookie!

Figure 2: Attribution: brainloc on sxc.hu (Bob Smith) (via CC Attribution 2.5 Generic)

Link to original image | Attribution 2.5 Generic (CC BY 2.5)

Useful Links


  1. I am using Pandoc to convert org-mode text into LaTeX and PDF. It works fairly well for my simple stuff.

    1. Where can I find more information about Pandoc?

  2. […] also fixed export and publishing to PDF issue I mentioned in my last post. Once again, the Internet came to my rescue and provided me with a good answer. I found an old post […]

  3. […] also fixed export and publishing to PDF issue I mentioned in my last post. Once again, the Internet came to my rescue and provided me with a good answer. I found an old post […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: