Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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."
  (interactive)
  (ps-print-buffer "~/temp.ps")
  (shell-command "open /Applications/Preview.app ~/temp.ps"))

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

;; Setup the postscript settings
;;
(defun sprint-setup (use-headers use-line-numbers use-landscape)
  (interactive
   (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):  http://www.emacswiki.org/emacs/download/longlines.el

http://stackoverflow.com/questions/17077434/adding-a-file-exists-p-condition-inside-an-interactive-list-of-a-function

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
      (append
       '((Consolas
          (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)))
       ps-font-info-database))
(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 ()
  (interactive)
  "Make all the newlines in the buffer hard."
  (save-excursion
    (goto-char (point-min))
    (while (search-forward "\n" nil t)
      (backward-char)
      (put-text-property (point) (1+ (point)) 'hard t)
      (forward-char))))

(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: " "~/.0.data/" 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)))
    (jit-lock-fontify-now)
    (save-current-buffer
    (set-buffer wbuf)
    (insert-buffer sbuf)
    (setq fill-column 63)
    (require 'longlines)
    (longlines-mode t)
    (harden-newlines)
    (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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .