Turning Markdown into PDFs using Puppeteer
Tags: puppeteer, node, javascript, markdown
Published: 2019-11-26 02:55:37

---
title: "Turning Markdown into PDFs using Puppeteer"
published: true
description: "How to take all of your Markdown and generate good looking PDFs from it."
tags: [Puppeteer, Nodejs, Javascript, Markdown]
---

Puppeteer is useful in many other contexts besides doing automated things on the web.  It's particularly useful for generating PDFs.  I want to highlight an NPM module / command line utility for turning Markdown into PDFs, [Markdown to PDF](https://www.npmjs.com/package/md-to-pdf).  It leverages Puppeteer and Google Chrome.

## What is Markdown?

Backing up a bit, [Markdown](https://daringfireball.net/projects/markdown/) is a light plain text markup language and text-to-HTML conversion tool.  It's really useful for writers, in particularly, for developers.  

> Markdown is a text-to-HTML conversion tool for web writers. Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML).

I love it because I take a plenty of notes in text files when attending talks, taking notes for meetings, for research, and more.  It has a light and easy to learn formatting syntax that allows writers to easily make their writing look well.  The thing I found most useful is the there's formatting for including code snippets, with syntax highlighting.  

```javascript
let message = 'Built in syntax highlighting is great';
```

Markdown is pretty popular in the development community because of it's feature set and is a natively format for documentation on popular software repository hosting services, like GitHub and Bitbucket.  For example, GitHub will appropriately render Markdown for files hosted on it's platform, like the readme file for [Get-Me-The-Gif](https://github.com/evanhalley/get-me-the-gif/blob/master/README.md).

I love Markdown so much, that I use it as the content format for my website, [EvanHalley.dev](https://evanhalley.dev).  I write all of my articles in Markdown, and using [Hugo](https://gohugo.io/), convert all of my markdown to HTML that is then uploaded to the web. Dev.to also uses Markdown as their content source of choice.  

## Installing

```bash
npm install --global md-to-pdf
```

## Using Markdown to PDF

Markdown to PDF allows you to easily turn markdown files in PDFs.  This is immediately useful if I wanted to send some developer documentation to a product manager via email, but want my Markdown to render correctly.

### Usage on the Command Line

I want to convert the following Markdown to a PDF.  I've saved it as `shopping.md`.  The `md` extension denotes a markdown file.

```Markdown
# This is a markdown file

## Shopping List
* Macaroni noodles
* Sweet potatoes
* Pie crust
* Turkey
* Stuffing
* Collard greens

> "Don't forget the butter"
```

I can use Markdown to PDF to convert this to a PDF.

```bash
md-to-pdf shopping.md shopping.pdf
```

A screenshot of the generated PDF:

![Screenshot of the generated PDF](https://thepracticaldev.s3.amazonaws.com/i/6xrv28waqonihzxoke75.png)

## Usage in a Nodejs App

```Javascript
const markdownToPdf = require('md-to-pdf');
 
(async () => {
    let pdf = await markdownToPdf('shopping.md', { dest: 'shopping.pdf' });
    console.log(pdf.filename);
})();
```

The outcome of the the use is the same, however, it's very easily integrated into larger JavaScript applications.  The example that immediately comes to mind is generating PDFs of all the documentation in a GitHub repository that can be distributed or uploaded elsewhere.

Thanks for reading!  Reach out to me on [Twitter](https://twitter.com/EvanHalley) if you have any questions.  

If you like this article, please subscribe to my newsletter, [The PuppetHero Digest](https://puppethero.com), where I share articles just like this with you, sent directly to your email inbox!