13

How can we convert the current text buffer to a pdf file, preferably turning those headings started with * into bookmarks of the pdf file. For example, turning the Emacs integrated tutorial (shown by C-h t) into a bookmarked pdf file. Thanks.

  • 2
    Look at exporting from org-mode to LaTeX/PDF. – PythonNut Feb 18 '15 at 19:02
  • 2
    specifically org-latex-export-to-pdf – casey Feb 18 '15 at 21:27
  • 6
    Just for the sake of diversity: you can do C-u M-x ps-print-buffer and then convert the resulting PostScript file to PDF (if your files aren't Org files and you just want to have plain text kind of PDF). Similarly, ps-print-buffer-with-faces is what it sounds like. – wvxvw Feb 18 '15 at 21:48
  • @PythonNut: C-c C-e is undefined – Tim Feb 18 '15 at 21:49
  • @casey: the command is not found – Tim Feb 18 '15 at 21:50
12

You can put the below in your init.el and bind the function to a binding of your choice.

The function prints the file in current buffer as a PDF in the same folder by default.

Here the function requires the binary ps2pdf for converting .ps to .pdf. But you can replace that with any available pdf generator on your system.

(require 'ps-print)
(when (executable-find "ps2pdf")
  (defun modi/pdf-print-buffer-with-faces (&optional filename)
    "Print file in the current buffer as pdf, including font, color, and
underline information.  This command works only if you are using a window system,
so it has a way to determine color values.

C-u COMMAND prompts user where to save the Postscript file (which is then
converted to PDF at the same location."
    (interactive (list (if current-prefix-arg
                           (ps-print-preprint 4)
                         (concat (file-name-sans-extension (buffer-file-name))
                                 ".ps"))))
    (ps-print-with-faces (point-min) (point-max) filename)
    (shell-command (concat "ps2pdf " filename))
    (delete-file filename)
    (message "Deleted %s" filename)
    (message "Wrote %s" (concat (file-name-sans-extension filename) ".pdf"))))
2

This does not address the specific issue of bookmarks in the PDF, but does address the general problem of converting buffers to PDF.

If you want to get a PDF "screenshot" of the buffer as you actually see it -- not showing hidden text -- then a good alternative is to htmlize the buffer, and convert the result from HTML to PDF. You could use this, for example, to build a PDF version of your Org Agenda. (Note that the ps-print-based answer from Kaushal Modi would show hidden buffer contents.)

recipe

M-x htmlize-buffer RET, C-x C-w buf.html RET; then run on the command line:

pandoc --from=html --to=latex --variable geometry="landscape" -o buf.pdf buf.html

example (screenshot)

enter image description here

See also

There is an interesting discussion about "vector screenshots" here: Can I take vector (SVG) screenshots of Emacs? (what I have described could be seen as a very limited example of a "vector screenshot".)

2

You can do C-u M-x ps-print-buffer to print current buffer to a PS file and then pipe it through ps2pdf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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