23

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).
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286

4 Answers4

18

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
scoa
  • 19,359
  • 5
  • 65
  • 80
  • this doesn't seem to work - it converts it to plain text..when I try to open the result with a document viewer, I get: `File type plain text document (text/plain) is not supported` –  Apr 16 '20 at 20:28
  • I think this is what's needed `echo foo | pandoc -t latex | pdflatex > ./test.pdf` but I still get the `text/plain is not supported` –  Apr 16 '20 at 20:32
9

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

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
7

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

jdhao
  • 24,001
  • 18
  • 134
  • 273
  • Thank you for your answer. But I can't get math formula well with this method. It will not compile the latex command in the html. So how do you solve this problem. – Jia Ruipeng Oct 26 '18 at 08:13
  • which link are you trying to convert to pdf? Post the link. – jdhao Oct 26 '18 at 08:14
0

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.

James Wilson
  • 852
  • 13
  • 29