Export Markdown to PDF with Windows and Smartdown

Exporting markdown to PDF can be tricky, especially under Windows. In order to make it easy for everybody, I decided to provide a solution, directly integrated into Smartdown. It does not need to install Pandoc or another converter.
Because Smartdown has also been made for “power users”, it also provides advanced way to customize your markdown to PDF export.

A premium markdown to PDF export

Currently Smartdown supports:

  • Custom cover, header and footer
  • Styling of PDF export
  • Table of contents
  • Automatically generated outline
  • Working links
  • Selectable text
  • Page size and orientation

General process

The Markdown is first converted into HTML. The CSS stylesheet of the preview template is used to style the output.
HTML is then converted to PDF and cover, table of contents, header and footer are added as configured in the export feature.

Defining header, footer and cover

  • Cover, Header and footer are defined as independent HTML files. You only have to inform “core” html, HTML, HEAD, BODY, … markups will be automatically added by Smartdown.
  • Smartdown will extract and apply to these parts, the CSS extracted from the preview template. Only CSS declared inside the HTML template is currently supported
  • Smartdown will then substitute the “template fields” such as page number, title,…

Cover

  • A cover will be added to the PDF document only if the cover field in PDF export options is not empty.
  • Html elements of class author and title will get their inner html replaced by the values defined in the metadata of the document. (Also applicable to footer and header)

Header and footers

You can use the following “template fields” in both header and footer. In order to use them, you need to specify an HTML element having one of the class. The content of this element will be substituted by the right value.

  • title
  • author
  • page
  • topage
  • document
  • section
  • subsection
  • subsubsection

Example: <span class="page"></span>

Table of contents

Smartdown can also generate a table of contents for your PDF from your markdown titles structure, if you check this option in the export screen.

How to use the preview template

Using CSS Print media

The same preview template can be also used to export to PDF format. However you may want to have a different output depending if you are previewing or exporting.
For example, I often used a light gray background for preview and a white background in the PDF.

You can achieve this by specifying print media specific CSS.

Styling cover, header and footer

If you use the default parameters, you can style the following class:

  • .header can style the header section of the PDF.
  • .footer can style the footer section of the PDF. For both header and footer, you can also add sub elements having their own style
  • .cover h1 which is used to style the document title (extracted from meta-data in the cover)
  • .cover h2 which is used to style the document author (extracted from meta-data in the cover)