help-gnu-emacs
[Top][All Lists]
Advanced

[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

Hi,

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
(http://www.emacswiki.org/emacs/ps2pdf.el) or Peter Mao's PdfTools
(http://www.emacswiki.org/emacs/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
http://stackoverflow.com/questions/7362625/word-wrap-for-emacs-print-buffer-to-pdf).
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 x86-05.phx2.fedoraproject.org)

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

(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)
  (load "ps-print")
  (let ((tmp ps-left-header))
    (unwind-protect
        (progn
          (setq ps-left-header
                (list (lambda () name) 'ps-header-dirpart))
          (ps-spool-buffer-with-faces))
      (setf ps-left-header tmp))))

(defun print-to-pdf ()
  "Print the current file to /tmp/print.pdf"
  (interactive)
  (let ((wbuf (generate-new-buffer "*Wrapped*"))
        (sbuf (current-buffer)))
    (jit-lock-fontify-now)
    (save-current-buffer
      (set-buffer wbuf)
      (insert-buffer sbuf)
      (setq fill-column 95)
      (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 "/tmp/print.ps")
      (kill-buffer (current-buffer)))
    (call-process "ps2pdf14" nil nil nil
                  "/tmp/print.ps" "/tmp/print.pdf")
    (delete-file "/tmp/print.ps")
    (message "PDF saved to /tmp/print.pdf")))

NB. This is a multipost from
http://stackoverflow.com/questions/7442100/formatting-a-header-in-an-emacs-function-to-print-a-buffer-to-pdf-w-line-wrappin
. So, if you know the answer, you can up your kudo's at Stackoverflow
by also posting it there.

Cheers,

Werner



reply via email to

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