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.
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
(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"))))
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.)
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
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".)
You can do
C-u M-x ps-print-buffer to print current buffer to a PS file and then pipe it through