Convert Markdown format files to PDF files (mermaid/emoji/toc compatible)
Tags: markdown, pdf, emoji, javascript
Published: 2022-09-01 14:45:13

This article is a translation of [マークダウン形式のファイルをPDFファイルに変換する（mermaid・emoji・toc対応）](https://bmf-tech.com/posts/%e3%83%9e%e3%83%bc%e3%82%af%e3%83%80%e3%82%a6%e3%83%b3%e5%bd%a2%e5%bc%8f%e3%81%ae%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92PDF%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%ab%e5%a4%89%e6%8f%9b%e3%81%99%e3%82%8b%ef%bc%88mermaid%e3%83%bbemoji%e3%83%bbtoc%e5%af%be%e5%bf%9c%ef%bc%89).

# 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](https://github.com/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](https://github.com/simonhaenisch/md-to-pdf) library.

This library is also indebted to me for managing my resume.
cf. [Manage resumes on Github](https://bmf-tech.com/posts/Github%e3%81%a7%e3%83%ac%e3%82%b8%e3%83% a5%e3%83%a1%e3%82%92%e7%ae%a1%e7%90%86%e3%81%99%e3%82%8b%e3%82%88%e3%81%86% e3%81%ab%e3%81%97%e3%81%9f)

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](https://github.com/yzane/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](https://github.com/simonhaenisch/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](https://github.com/simonhaenisch/md-to-pdf) can extend the setting of [markedjs/marked](https://github.com/markedjs/marked) Therefore, it seems that all of them can be realized by customizing [md-to-pdf](https://github.com/simonhaenisch/md-to-pdf).

It seems that TOC is planned to be supported.
[Generate TOC (table of contents) #74](https://github.com/simonhaenisch/md-to-pdf/issues/74)

I could have used [md-to-pdf](https://github.com/simonhaenisch/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](https://github.com/mikewootc/md-to-pdf-ng).

This is a library that extends [md-to-pdf](https://github.com/simonhaenisch/md-to-pdf) to support mermaid notation. It can be used without problems.

Based on [md-to-pdf-ng](https://github.com/mikewootc/md-to-pdf-ng), emoji support is [node-emojify](https://github.com/jesselpalmer /node-emojify), TOC generation is realized using a library called [doctoc](https://github.com/thlorenz/doctoc).

# Implementation
Install the following with npm.

- [md-to-pdf-ng](https://github.com/mikewootc/md-to-pdf-ng)
- [node-emojify](https://github.com/jesselpalmer/node-emojify)
- [doctoc](https://github.com/thlorenz/doctoc)

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

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

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

const renderer = new marked.Renderer();

renderer.text = emojiify;

module. exports = {
marked_options: { renderer },
};
```

Define the following command in scripts of package.json.

```json
doctoc --notitle md/ && md-to-pdf md/*.md --config-file config.js && mv md/*.pdf pdf/
```

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.