Turning Markdown into PDFs using Puppeteer
Evan
Posted on November 26, 2019
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. It leverages Puppeteer and Google Chrome.
What is Markdown?
Backing up a bit, 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.
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.
I love Markdown so much, that I use it as the content format for my website, EvanHalley.dev. I write all of my articles in Markdown, and using Hugo, 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
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.
# 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.
md-to-pdf shopping.md shopping.pdf
A screenshot of the generated PDF:
Usage in a Nodejs App
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 if you have any questions.
If you like this article, please subscribe to my newsletter, The PuppetHero Digest, where I share articles just like this with you, sent directly to your email inbox!
Posted on November 26, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.