Home > emacs > eMarch 2 : create a pdf with highlighted code source

eMarch 2 : create a pdf with highlighted code source

SSemacsA very nice feature of emacs is syntax highlighting for a wide variety of language. Back in the old time, I used to print my a lot of my source code in order to analyse it in details, as the AIX server I was working on had no access to any color-printer, I had to print my buffer to a postscript file first, (convert it to pdf) and last transfer it to my main computer. But one, day I found a nice small piece of lisp which allow me simplify the process.

Thus I just added this in my  .emacs file

(defun print-to-pdf ()
 (interactive)
 (ps-spool-buffer-with-faces)
 (switch-to-buffer "*PostScript*")
 (write-file "tmp.ps")
 (kill-buffer "tmp.ps")
 (setq cmd (concat "ps2pdf14 tmp.ps " (buffer-name) ".pdf"))
 (shell-command cmd)
 (shell-command "rm tmp.ps")
 (message (concat "File printed in : "(buffer-name) ".pdf"))
 )

Then, from any buffer, typing M-x print-to-pdf,  create nice and colorful pdf of my source code.

This script used to be a neat way to keep a pretty printed version of my work, fortunately printing to pdf became much easier with later version emacs, making the previous code a bit useless. But here comes the way I recycled this script. As I very often have to document my script and source code (who doesn’t ???), I took the habit to add as an appendix to my manuals the source code of the main scripts or software used. To do so, I produce a syntax highlighted pdf that I include via the \includepdf{} \LaTeX command. Still, an annoying point is that scripts are updated rather often, so we have to think about an easy way to recreate the pdf.

In my .emacs, I thus modify my previous function into :

(defun print-to-pdf-batch ()
 (interactive)
 (ps-spool-buffer-with-faces)
 (switch-to-buffer "*PostScript*")
 (write-file "tmp.ps")
 (kill-buffer "tmp.ps")
 (setq cmd (concat "ps2pdf14 tmp.ps " (buffer-name) ".pdf"))
 (shell-command cmd)
 (shell-command "rm tmp.ps")
 (kill-emacs t)
 )

The changes are that we now avoid message output and ask to kill emacs at the end of the execution. For a series of sources code , you just have to run e.g.

for File in *.sh ; do emacs $File --eval "(print-to-pdf-batch)" ; done

This trick works, I imagine a better version would be to use the “-batch” argument of emacs, and eventually a lisp file (thus you should not change your .emacs). Unfortunately, I could not achieve to obtain color in the output (probably due to X issues ? any hints/solutions wold be appreciated). If you have no X server the -nw options also works but the colors used are to my opinion not so nice.

Advertisements
Categories: emacs
  1. No comments yet.
  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: