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.
-
2Look at [exporting from `org-mode` to LaTeX/PDF](http://orgmode.org/manual/LaTeX-and-PDF-export.html). – PythonNut Feb 18 '15 at 19:02
-
2specifically `org-latex-export-to-pdf` – casey Feb 18 '15 at 21:27
-
6Just 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
-
@Tim try doing `M-x` `org-mode`. – PythonNut Feb 18 '15 at 21:55
-
@PythonNut: Thanks. your link says `C-c C-e l p`, but when I hit `l`, it says `Need a file name to be able to export` – Tim Feb 18 '15 at 22:15
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-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"))))

- 25,203
- 3
- 74
- 179
-
I was wondering what the `modi/` does in the function name, but that's your last name to indicate your personal functions? – AstroFloyd Jun 18 '21 at 12:17
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".)

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

- 409
- 3
- 12