Figure 1: Attribution: en:User:Petteri (via CC Attribution-Share Alike 3.0 Unported)
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
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:
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:
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:
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?
- Lpr Man Page: http://linuxcommand.org/man_pages/lpr1.html
- Configuring and Managing Printing in Oracle® Solaris 11.2: https://docs.oracle.com/cd/E36784_01/html/E36821/glldq.html
- Command-Line Printing and Options (CUPS.org): http://www.cups.org/documentation.php/options.html
- Printing Hard Copies (GNU Emacs Manual – Section 39): https://www.gnu.org/software/emacs/manual/html_node/emacs/Printing.html
- Postscript Hardcopy (GNU Emacs Manual – Section 39.1): https://www.gnu.org/software/emacs/manual/html_node/emacs/PostScript.html
- PostScript (Wikipedia): https://en.wikipedia.org/wiki/PostScript
- Creating PDF files with ps2pdf: https://www.linux.com/learn/tutorials/442405-creating-pdf-files-with-ps2pdf
- LaTex and PDF export (The Org Manual): http://orgmode.org/manual/LaTeX-and-PDF-export.html#LaTeX-and-PDF-export
- HTML export commands (The Org Manual): http://orgmode.org/manual/HTML-Export-commands.html#HTML-Export-commands
- A Chocolate Chip Cookie Recipe (Your mileage may vary…): http://allrecipes.com/recipe/10813/best-chocolate-chip-cookies/