21

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.

7
  • 2
    Look at exporting from org-mode to LaTeX/PDF.
    – PythonNut
    Commented Feb 18, 2015 at 19:02
  • 2
    specifically org-latex-export-to-pdf
    – casey
    Commented Feb 18, 2015 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
    Commented Feb 18, 2015 at 21:48
  • @PythonNut: C-c C-e is undefined
    – Tim
    Commented Feb 18, 2015 at 21:49
  • @casey: the command is not found
    – Tim
    Commented Feb 18, 2015 at 21:50

3 Answers 3

19

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-region-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
  • 1
    I was wondering what the modi/ does in the function name, but that's your last name to indicate your personal functions?
    – AstroFloyd
    Commented Jun 18, 2021 at 12:17
  • 1
    @AstroFloyd, it does nothing. That's a way of using pseudo namespaces in Lisp.
    – sphakka
    Commented Oct 3 at 17:03
6

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".)

6

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

1
  • 1
    I often forget that I need the C-u prefix argument.
    – remcycles
    Commented Jan 11, 2022 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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