Chris Stryczynski
Chris Stryczynski

Reputation: 34041

How to convert HTML to PDF using pandoc?

Which libraries do I need? Which command line parameters do I need to pass?

I've installed wkhtml2pdf and I've tried to run:

pandoc reports/7/report.html -t pdf -o reports/7/report.pdf

Which reports an error of:

To create a pdf with pandoc, use the latex or beamer writer and specify
an output file with .pdf extension (pandoc -t latex -o filename.pdf).

Upvotes: 31

Views: 52365

Answers (4)

James Wilson
James Wilson

Reputation: 942

For me, running linux with pandoc 1.19.2.4, (and converting cv.md) I needed to use:

pandoc -t html5 -o cv.pdf cv.md

the 5 in html5 seems to be missing from a lot of things I have Googled.

Please note that you need wkhtmltopdf installed for this to work.

Upvotes: 3

jdhao
jdhao

Reputation: 28467

The following works for me:

pandoc --pdf-engine=xelatex https://www.python.org/dev/peps/pep-0008/ -o pep8.pdf

You need to have a LaTeX distribution installed such as TeX Live.

If you want to color the links, you should add linkcolors options. If the webpage contains CJK characters, you need to specify CJKmainfont options. An example is shown below:

pandoc --pdf-engine=xelatex -V colorlinks -V CJKmainfont="KaiTi" https://jdhao.github.io/2019/01/07/windows_tools_for_programmers/ -o programmer_tools.pdf

The font KaiTi supports Chinese characters. If you use other languages, you may use the mainfont option to specify a font which supports the language the webpage uses.

If the webpage contains svg images, you also need to install rsvg-convert to successfully convert the webpage to PDF files (see reference here).

Upvotes: 8

Dave Jarvis
Dave Jarvis

Reputation: 31211

From https://pandoc.org/MANUAL.html:

Alternatively, pandoc can use any of the following HTML/CSS-to-PDF-engines, to create a PDF:

To do this, specify an output file with a .pdf extension, as before, but add the --pdf-engine option or -t context, -t html, or -t ms to the command line (-t html defaults to --pdf-engine=wkhtmltopdf).

Upvotes: 13

scoa
scoa

Reputation: 19857

pdf is not a valid output format. latex and beamer (for latex slideshows) are.

To create a pdf, use -t latex and -o myoutput.pdf. You can omit the -t argument since a .pdf in -o defaults to latex. So you can use either:

pandoc reports/7/report.html -t latex -o reports/7/report.pdf

or:

pandoc reports/7/report.html -o reports/7/report.pdf

Upvotes: 19

Related Questions