unified

Project: inokawa/remark-pdf

Package: remark-pdf@0.0.12

  1. remark plugin to compile markdown to pdf.
  1. remark 196
  2. unist 117
  3. markdown 114
  4. mdast 68

remark-pdf

npm npm check demo

remark plugin to compile markdown to pdf.

Supported mdast nodes

Currently, some of the default styles may not be nice. If you have some feature requests or improvements, please create a issue or PR.

Demo

https://inokawa.github.io/remark-pdf/

Install

npm install remark-pdf

Usage

Browser

import { unified } from "unified";
import markdown from "remark-parse";
import pdf from "remark-pdf";
import { saveAs } from "file-saver";

const processor = unified().use(markdown).use(pdf);

const text = "# hello world";

(async () => {
  const doc = await processor.process(text);
  const uint8Array = doc.value;
  saveAs(
    new Blob([uint8Array as BlobPart], { type: "application/pdf" }),
    "example.pdf",
  );
})();

Node.js

import { unified } from "unified";
import markdown from "remark-parse";
import pdf from "remark-pdf";
import * as fs from "fs";

const processor = unified().use(markdown).use(pdf);

const text = "# hello world";

(async () => {
  const doc = await processor.process(text);
  const uint8Array = doc.value;
  fs.writeFileSync("example.pdf", uint8Array);
})();

CLI

See remark-cli for usage.

npm install remark-cli
npx remark input.md --use remark-pdf --output output.pdf

To embed local images, use remark-embed-images with image option.

npx remark input.md --use remark-embed-images --use remark-pdf=image:true --output output.pdf

Custom fonts

Use custom fonts by providing a fonts option. Use the fonts by name in your styles configurations; the font file will be autoselected based on the chosen bold and italic style specifications.

Note that variable-width fonts are supported, but the path to the same font file must be supplied for all four font variant styles.

import { unified } from "unified";
import markdown from "remark-parse";
import pdf from "remark-pdf";
import * as fs from "fs";

const processor = unified()
  .use(markdown)
  .use(pdf, {
    fonts: [
      {
        name: "National Park",
        normal: fs.readFileSync("/path/to/fonts/nationalpark-variablevf.ttf"),
        bold: fs.readFileSync("/path/to/fonts/nationalpark-variablevf.ttf"),
        italic: fs.readFileSync("/path/to/fonts/nationalpark-variablevf.ttf"),
        bolditalic: fs.readFileSync(
          "/path/to/fonts/nationalpark-variablevf.ttf",
        ),
      },
      {
        name: "Merriweather Sans",
        normal: fs.readFileSync("/path/to/fonts/merriweathersans-light.ttf"),
        bold: fs.readFileSync("/path/to/fonts/merriweathersans-bold.ttf"),
        italic: fs.readFileSync("/path/to/fonts/merriweathersans-italic.ttf"),
        bolditalic: fs.readFileSync(
          "/path/to/fonts/merriweathersans-bolditalic.ttf",
        ),
      },
    ],
    styles: {
      default: { font: "Merriweather Sans", italic: true },
      head1: {
        bold: true,
        font: "National Park",
        fontSize: 24,
      },
    },
  });

const text = `
# Header in National Park bold

Body text in Merriweather Sans Italic
`;

(async () => {
  const doc = await processor.process(text);
  const uint8Array = doc.value;
  fs.writeFileSync("example.pdf", uint8Array);
})();

Image

Set image option to embed png, jpg and svg images.

const processor = unified().use(markdown).use(pdf, { image: true });

Images are fetched with fetch API by default. You can customize it with load option, which also receives the processed file to resolve relative urls.

import * as fs from "node:fs/promises";
import { pathToFileURL } from "node:url";

const processor = unified()
  .use(markdown)
  .use(pdf, {
    image: {
      load: (url, file) => fs.readFile(new URL(url, pathToFileURL(file.path))),
    },
  });

To embed other images such as gif and webp, also set toPng option to convert them to png. On browser, you can use browserToPng, which converts images that the browser can show.

import pdf, { browserToPng } from "remark-pdf";

const processor = unified()
  .use(markdown)
  .use(pdf, { image: { toPng: browserToPng } });

On other environments like Node.js, implement it by yourself.

import sharp from "sharp";

const processor = unified()
  .use(markdown)
  .use(pdf, {
    image: {
      toPng: ({ buffer }) => sharp(buffer).png().toBuffer(),
    },
  });

Images that are not embedded are replaced with their alt text.

Plugins

Math

Render LaTeX with MathJax.

import { unified } from "unified";
import markdown from "remark-parse";
import math from "remark-math";
import pdf from "remark-pdf";
import { latexPlugin } from "remark-pdf/plugins/latex";

const processor = unified()
  .use(markdown)
  .use(math)
  .use(pdf, { plugins: [latexPlugin()] });

Code

Syntax highlight

Syntax highlighting with shiki.

import { unified } from "unified";
import markdown from "remark-parse";
import pdf from "remark-pdf";
import { shikiPlugin } from "remark-pdf/plugins/shiki";

const processor = unified()
  .use(markdown)
  .use(pdf, { plugins: [shikiPlugin({ theme: "dark-plus" })] });

Mermaid

Render Mermaid in code blocks with mermaid language, with beautiful-mermaid. Flowcharts, state diagrams, sequence diagrams, class diagrams, ER diagrams and XY charts are supported.

import { unified } from "unified";
import markdown from "remark-parse";
import pdf from "remark-pdf";
import { mermaidPlugin } from "remark-pdf/plugins/mermaid";

const processor = unified()
  .use(markdown)
  .use(pdf, { plugins: [mermaidPlugin()] });

Documentation

Contribute

All contributions are welcome. If you find a problem, feel free to create an issue or a PR.

Making a Pull Request

  1. Fork this repo.
  2. Run npm install.
  3. Commit your fix.
  4. Add tests to cover the fix.
  5. Make a PR and confirm all the CI checks passed.