[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Formatting a header to print a buffer to PDF w/ line wrapping

From: Werner Heijstek
Subject: Formatting a header to print a buffer to PDF w/ line wrapping
Date: Tue, 20 Sep 2011 11:54:30 +0200


To be able to save a buffer to pdf, one can use the PsPrintPackage and
make a system call to (e.g.) ps2pdf14.

Some scripts exist to make this process somewhat more streamlined.
Examples are Mathias Dahl's ps2pdf.el
( or Peter Mao's PdfTools

However helpful, these packages all churn out a good looking PDF file
in which lines are not wrapped. Enabling longlines mode does little to
the resulting PS output as the soft linebreaks introduced are
"ignored". These linebreaks can hardened in a copy  of the current
buffer, off course, but then the resulting PS header has to be
re-created (to "simulate" what the header would have been for the
original buffer).

All this has been cleverly observed by Rupert Swarbrick (at
The three functions that he came up with work fine, except that the
resulting header is always "HeaderLinesLeft" (at least in my version
of Emacs - I am using GNU Emacs 23.2.1 (i386-redhat-linux-gnu, GTK+
Version 2.24.4) of 2011-05-23 on

These are the three functions, who can spot the mistake?

(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)
  (load "ps-print")
  (let ((tmp ps-left-header))
          (setq ps-left-header
                (list (lambda () name) 'ps-header-dirpart))
      (setf ps-left-header tmp))))

(defun print-to-pdf ()
  "Print the current file to /tmp/print.pdf"
  (let ((wbuf (generate-new-buffer "*Wrapped*"))
        (sbuf (current-buffer)))
      (set-buffer wbuf)
      (insert-buffer sbuf)
      (setq fill-column 95)
      (longlines-mode t)
      (message (buffer-name sbuf))
      (spool-buffer-given-name (buffer-name sbuf))
      (kill-buffer wbuf)
      (switch-to-buffer "*PostScript*")
      (write-file "/tmp/")
      (kill-buffer (current-buffer)))
    (call-process "ps2pdf14" nil nil nil
                  "/tmp/" "/tmp/print.pdf")
    (delete-file "/tmp/")
    (message "PDF saved to /tmp/print.pdf")))

NB. This is a multipost from
. So, if you know the answer, you can up your kudo's at Stackoverflow
by also posting it there.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]