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.
3 Answers
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")))) - 1I was wondering what the
modi/does in the function name, but that's your last name to indicate your personal functions?AstroFloyd– AstroFloyd2021-06-18 12:17:48 +00:00Commented Jun 18, 2021 at 12:17 - 1@AstroFloyd, it does nothing. That's a way of using pseudo namespaces in Lisp.sphakka– sphakka2024-10-03 17:03:38 +00:00Commented Oct 3, 2024 at 17:03
You can do C-u M-x ps-print-buffer to print current buffer to a PS file and then pipe it through ps2pdf.
- 2I often forget that I need the
C-uprefix argument.remcycles– remcycles2022-01-11 19:44:16 +00:00Commented Jan 11, 2022 at 19:44
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)
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".)

org-modeto LaTeX/PDF.org-latex-export-to-pdfC-u M-x ps-print-bufferand 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-facesis what it sounds like.C-c C-e is undefined