unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Formatting a header to print a buffer to PDF w/ line wrapping
@ 2011-09-20  9:54 Werner Heijstek
  0 siblings, 0 replies; only message in thread
From: Werner Heijstek @ 2011-09-20  9:54 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2011-09-20  9:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-09-20  9:54 Formatting a header to print a buffer to PDF w/ line wrapping Werner Heijstek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).