How to generate a PDF from a Markdown file — using Node.js and JavaScript

  • Summary: Translating Markdown to PDF is an essential part of many report generation systems. PDF is a portable format that can enable users to share files on mediuns such as instant messaging and email systems.

  • Audience: Systems architects, JavaScript developers, Front-end developers, Back-end developers.

  • This meeting: f4c75be3-34d8-4591-aa42-dcf6a6b56160
  • Participants: Marcio S Galli
  • Text Language: en-US
  • Tags: NodeJS, PDF, Markdown.

Introduction

In this project, we will use JavaScript to generate a PDF file based on another file written in Markdown. The motivation for me is this: I usually write a document, using simple text, that covers an executive summary for most of my meetings. I want to have a PDF of it. Anyway, that is my case — your situation might be something else.

What matters is that I was using Markdown already. Markdown is the well-known lightweight markup language designed to be easily converted to HTML. Developers know that, but if you don’t know, now you know. Since Markdown can become HTML and HTML can become PDF, I had the idea to start wrapping my reports to PDF.

Let’s see a simple file in Markdown

Example of a Markdown text file - Photo by Bruna Araujo on Unsplash — screenshot adapted by Marcio

Let’s see the PDF generated from it

The following screenshot is here to help you visualize the whole flow more easily:

The screenshot of the PDF preview using Mac OS X

Example of the PDF sample printed

You may also download the PDF:

Introducing Markdown-pdf

The good news is that I didn’t even have to do these two steps, although I think it’s important to stress that because the library that I have used is doing behind the scenes. If you want to check, this library is markdown-pdf.

Although this library will help you do the conversion using the command line, the following sample will show how to do the conversion programmatically via JavaScript.

The sample code — in JavaScript using Node.js

The following is the main file in JavaScript. This sample is a server-side JavaScript that will render a PDF using the input file “./db/test.md”:

const path           = require('path');
const fs             = require('fs');
const markdownpdf    = require("markdown-pdf");
let inFile  = path.join(__dirname, 'db', 'test.md');
let outFile = path.join(__dirname, 'db', 'test.pdf');
 
fs.createReadStream(inFile)
  .pipe(markdownpdf())
  .pipe(fs.createWriteStream(outFile))

I have published the simple script here:

The following structure shows how this sample looks in the filesystem:

├─ README.md
├─ db
│  ├─ test.md
│  └─ test.pdf
├─ index.js
└─ package.json

The above sample case will let do a conversion of the provided sample file. To get it going, you will need to clone the project and be familiar with Node.js and npm (Node Package Manager).

Let’s look at the underlying libraries

To conclude this article, I wanted to tell you that I did a check on the file package.json from Markdown-pdf and was able get an overview of some of the key underlying library projects used by the author, which is:

  • “remarkable” — a project that can convert Markdown to HTML.

  • “phantomjs-prebuilt” — a project of a browser rendering engine that does the HTML rendering job behind the scenes. PhantomJS can also generate “the page” as PDF.

References

About

This article was published at JavaScript in Plain English. Thanks to the editors of Plain English for the help in terms of reviewing. Thanks to the author of Markdown-pdf, Alan Shaw.