Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been using emacs on the mac for a couple of years now, but have been using other apps to print. I finally started digging into emacs printing. I decided to set up postscript printing. I print to a file and then use Preview to do the actual printing. Here's my setup. Anyone else have something better?


;; Postscript printing
;; defaults
(setq ps-font-size 9)
(setq ps-header-font-size 8)
(setq ps-header-title-font-size 8)
;;(setq ps-header-lines 1) ;; only use the filename and page number

(setq ps-header-offset 18) ;; margin between header and text in 72nds of an inch
(setq ps-left-margin 36)   ;; left margin in 72nds
(setq ps-right-margin 36)  ;; right margin in 72nds

;; Simple print buffer command
(defun sprint-buffer ()
  "Output a postscript file using the defaults."
  (ps-print-buffer "~/")
  (shell-command "open /Applications/ ~/"))

;; Simple print region command
(defun sprint-region ()
  "Output a postscript file using the defaults."
  (ps-print-region (mark) (point) "~/")
  (shell-command "open /Applications/ ~/"))

;; Setup the postscript settings
(defun sprint-setup (use-headers use-line-numbers use-landscape)
   (list (y-or-n-p "Headers?")
         (y-or-n-p "Line numbers?")
         (y-or-n-p "Landscape?")))
  (setq ps-print-headers use-headers)
  (setq ps-line-number use-line-numbers)
  (setq ps-landscape-mode use-landscape))
share|improve this question

You'll need the older code from longlines.el (the current version of visual-line-mode doesn't work in lieu of longlines-mode):

I am using courier because it handles all capital letters better than times.

I have a yes/no in there as a workaround to the ns-read-file-name bug, but the Emacs team just fixed that bug a couple of weeks ago in the Trunk. So if you build an Emacs Trunk developer edition --with-ns, then you don't need the yes/no any more.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; PRINT TO PDF ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'ps-print)

;; Add Consolas
(setq ps-font-info-database
          (fonts (normal      . "Consolas")
                 (bold        . "Consolas-Bold")
                 (italic      . "Consolas-Italic")
                 (bold-italic . "Consolas-Bold-Italic"))
          (size           . 13.0)
          (line-height    . 14.5)
          (space-width    . 6.15)
          (avg-char-width . 6.05)))
(setq ps-font-family 'Courier) ;; no difference between Courier and Courier Bold
(setq ps-font-size 13)
;; Print in color
;; (setq-default ps-print-color-p t)
;; Page layout: Header [file-name     2011-12-05]
;;              Footer [                     n/m]
(setq ps-left-margin 60)
(setq ps-right-margin 0) ;; fill-column in print-to-pdf trumps this
;; Header
(setq ps-top-margin 0)
(setq ps-header-offset 0)
(setq ps-header-line-pad 2.6)
(setq ps-header-lines 1)
(setq ps-header-font-size 13)
(setq ps-header-title-font-size 13)
(setq ps-header-font-family 'Consolas)
(setq ps-left-header nil)
(setq ps-right-header nil)
(setq ps-print-header-frame nil)
;; Footer
(setq ps-bottom-margin 0)
(setq ps-footer-lines 1)
(setq ps-footer-font-size 13)
(setq ps-footer-font-family 'Consolas)
(setq ps-print-footer t)
(setq ps-left-footer nil)
(setq ps-right-footer (list "/pagenumberstring load"))
(setq ps-footer-offset 1.0)
(setq ps-footer-line-pad 2.5)
(setq ps-print-footer-frame nil)

(defun harden-newlines ()
  "Make all the newlines in the buffer hard."
    (goto-char (point-min))
    (while (search-forward "\n" nil t)
      (put-text-property (point) (1+ (point)) 'hard t)

(defun spool-buffer-given-name (name)
  (ps-spool-buffer-with-faces) )

(defun print-to-pdf (pdf-file-name)
  "Print the current file to the given file."
  (interactive (list (ns-read-file-name "Write PDF file: " "~/" nil ".pdf")))
  (when (or (not (file-exists-p pdf-file-name))
            (yes-or-no-p (format "%s  File already exists.  Do you want to overwrite it?" pdf-file-name)))
    (let (
        (ps-file-name (concat (file-name-sans-extension pdf-file-name) ".ps"))
        (wbuf (generate-new-buffer "*Wrapped*"))
        (sbuf (current-buffer)))
    (set-buffer wbuf)
    (insert-buffer sbuf)
    (setq fill-column 63)
    (require 'longlines)
    (longlines-mode t)
    (message (buffer-name sbuf))
    (spool-buffer-given-name (buffer-name sbuf))
    (kill-buffer wbuf)
    (switch-to-buffer "*PostScript*")
    (write-file ps-file-name)
    (kill-buffer (current-buffer)))
    (call-process "/usr/local/bin/ps2pdf14" nil nil nil ps-file-name pdf-file-name)
    (delete-file ps-file-name)
    (message "PDF saved to %s" pdf-file-name))))
share|improve this answer

In emacs 24 I'm using the htmlize-buffer command. This generates an .html file (with all the correct syntax highlighting) which I can then print or convert to a .pdf.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.