Search code examples
markdownwkhtmltopdf

How to convert Markdown to PDF without writing an intermediate HTML file?


The goal is to take a Markdown file and output a PDF. Using the CLI tools from marked and wkhtmltopdf, I have:

marked file.md -o file.html && wkhtmltopdf git.html file.pdf && start file.pdf

This works but it leaves a useless HTML file to clean up. I would like to avoid this read/write step altogether. Is there a way to pipe the output from marked to wkhtmltopdf directly?

Is there a better tool to use? I'm open to JavaScript, C#, and command-line based tools.


Solution

  • marked outputs to standard out by default, and wkhtmltopdf can read from stdin if you specify - as the file to read, so you can pipe one into the other:

    marked file.md | wkhtmltopdf - file.pdf