Converting Markdown to PDF

by Christof Jans | 2021-02-21

Recently, I had to convert a large number of Markdown files to PDF. Obviously, I wanted to automate this.

After doing some research I settled on the following approach:

  1. Convert Markdown to HTML using powershell.
  2. Convert HTML to PDF using headless chrome.

Converting Markdown to HTML

Apparently, powershell now has a ConvertFrom-Markdown commandlet (yay!). This returns an object which contains the abstract syntax tree but also, the Html property.

Converting Markdown to HTML is as simple as

(ConvertFrom-Markdown | out-file output.html

Converting HTML to PDF

Headless Chrome is a way to run chrome without a user interface. The command to print HTML to PDF is:

& chrome --headless --disable-gpu --print-to-pdf-no-header --print-to-pdf=E:\output.pdf  E:\input.html

That is a mouthful so let's go over it step by step

Warning : I've noticed that headless chrome only works with absolute paths, not relative paths. You can use Resolve-Path to transform a relative path into an absolute path.

The final script is this:


$markdownpath = (Resolve-Path $markdownpath).Path
$htmlpath = "$markdownpath.html"
$pdfpath = "$markdownpath.pdf"

(ConvertFrom-Markdown $markdownpath).Html | out-file $htmlpath

& "$($Env:ProgramFiles) (x86)\Google\Chrome\Application\chrome.exe"  --headless --disable-gpu --print-to-pdf-no-header --print-to-pdf=$pdfpath  $htmlpath

Hope this helps.