DEV Community

Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Convert Markdown format files to PDF files (mermaid/emoji/toc compatible)

This article is a translation of マークダウン形式のファイルをPDFファイルに変換する(mermaid・emoji・toc対応).

Overview

I made a simple document management tool to meet the demand to convert Markdown format files to PDF files.

bmf-san/docs-md-to-pdf-example

I didn't think too much about it, and I made it using a library of things that existed, so it's a structure that doesn't feel very sustainable.

Motivation

If you just want to convert Markdown format files to PDF files, you can use the md-to-pdf library.

This library is also indebted to me for managing my resume.
cf. Manage resumes on Github

There was a desire to support mermaid notation and to use pictograms other than those registered in unicode, so I wanted to create a shape that corresponds to that.

It can be easily solved by using the vscode extension vscode-markdown-pdf, but vscode is required, so people Depending on it, you will need to install vscode.

I thought it was nonsense to use vscode only for conversion, so I implemented it.

design

The md-to-pdf library is easy to use and wonderful, but currently the following functions are not supported as standard.

  • mermaid notation
  • emoji (other than those registered in unicode)
  • TOC generation

md-to-pdf can extend the setting of markedjs/marked Therefore, it seems that all of them can be realized by customizing md-to-pdf.

It seems that TOC is planned to be supported.
Generate TOC (table of contents) #74

I could have used md-to-pdf, but it seemed like it would take a little time, so I implemented it as quickly as possible, like a hackathon. I wanted to, so I decided to use a library called md-to-pdf-ng.

This is a library that extends md-to-pdf to support mermaid notation. It can be used without problems.

Based on md-to-pdf-ng, emoji support is node-emojify, TOC generation is realized using a library called doctoc.

Implementation

Install the following with npm.

*Textlint is added as an extra, but that part is omitted.

Emoji support is supported by extending marked, so prepare the following configuration file.

const marked = require('marked');
const { emojiify } = require('node-emoji');

const renderer = new marked.Renderer();

renderer.text = emojiify;

module. exports = {
marked_options: { renderer },
};
Enter fullscreen mode Exit fullscreen mode

Define the following command in scripts of package.json.

doctoc --notitle md/ && md-to-pdf md/*.md --config-file config.js && mv md/*.pdf pdf/
Enter fullscreen mode Exit fullscreen mode

First generate TOC with doctoc, then convert markdown to PDF, and finally move directory.

It would be nice to be able to specify the output destination of the pdf generated by md-to-pdf in units of directories, but it seems that there is no such option, so it is handled by the easy method of mv md/*.pdf pdf/ .

Impression

If you try to make something like this, you will tend to rely on external libraries.
If possible, I would like to implement everything by myself, but I think it is quite difficult.
If I have the chance, I would like to learn the data structure of PDF and create a similar CLI tool in Go.

Top comments (0)