Registry / devops / md-to-pdf

md-to-pdf

JSON →
library0.1.0jsnpmunverified

md-to-pdf is a CLI tool for converting Markdown files to PDF using Marked (for HTML rendering) and Puppeteer (headless Chromium for PDF generation). Current stable version is 5.2.5 (last updated November 2023). It supports concurrent conversion of multiple files, watch mode, front-matter configuration, custom stylesheets/scripts, headers/footers, page breaks, syntax highlighting via highlight.js, and both CLI and programmatic API. Key differentiators include its hackability (~500 lines of TypeScript), stdio support, and optional HTML output. Note: v5.0.0 disabled JavaScript in front-matter by default to prevent RCE (security fix). Requires Node >=12.

npm install md-to-pdf
INSTALL
IMPORT
SIG · MD-TO-PDF
M
md-to-pdf
devopsjavascriptv0.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

convertMdToPdf
import { convertMdToPdf } from 'md-to-pdf'
const convertMdToPdf = require('md-to-pdf')
ESM import (package ships TypeScript types). The default export is not available; use named import. For CommonJS, use const { convertMdToPdf } = require('md-to-pdf').
convertMdToPdf (CommonJS)
const { convertMdToPdf } = require('md-to-pdf')
const mdToPdf = require('md-to-pdf'); mdToPdf.convertMdToPdf()
Direct destructuring is the correct pattern. The module does not export a default object.
Options types
import type { PdfOptions, MarkedOptions } from 'md-to-pdf'
import { PdfOptions } from 'md-to-pdf'
These are type-only exports. Use import type for TypeScript. Available since v5.0.

Demonstrates programmatic API: convert a markdown string to PDF with custom PDF options and CSS, then save to file.

import { convertMdToPdf } from 'md-to-pdf' // For CommonJS: const { convertMdToPdf } = require('md-to-pdf') async function main() { const pdf = await convertMdToPdf('# Hello, World!\n\nThis is a **markdown** string.', { pdf_options: { format: 'A4', margin: { top: '20mm', bottom: '20mm' } }, css: 'body { font-family: Helvetica, Arial, sans-serif; }' }) // pdf is a Buffer with the PDF content require('fs').writeFileSync('output.pdf', pdf) } main()
md-to-pdf --version
Debug
Known issues
breakingv5.0.0 disables JavaScript engine in front-matter by default to prevent remote code execution (RCE).
fix
To enable JS in front-matter, pass `--gray-matter-options 'null'` on CLI or set `gray_matter_options: undefined` in programmatic options.
affects: >=5.0.0
gotchaPuppeteer may not be bundled with the package; additional system dependencies may be required (e.g., libnss3, libnspr4 on Linux).
fix
Install Puppeteer's missing dependencies as per https://pptr.dev/troubleshooting#chrome-headless-doesnt-launch-on-unix
affects: All
deprecatedThe `body-classes` option (plural) is deprecated; use `body-class` (singular) instead.
fix
Use `--body-class` in CLI or `body_class` in programmatic options.
affects: >=5.0.0?
gotchaNode 14 compatibility was broken in v5.2.3 due to use of `.replaceAll`, but fixed in v5.2.4.
fix
Upgrade to v5.2.4 or later, or set `--md-file-encoding` for certain edge cases.
affects: 5.2.3
breakingv5.1.0 changed the code highlighting option from overwriting `renderer.code` to using the `highlight` option in marked.
fix
Pass `highlight` function in `marked_options` instead of custom renderer.
affects: >=5.1.0
Errors
Common errors & fixes
Error: Cannot find module 'puppeteer'
Puppeteer is a peer dependency and may not be installed automatically.
fix
Run `npm install puppeteer` in your project or install globally with `npm i -g puppeteer`.
Error: Failed to launch the browser process! spawn /path/to/node_modules/puppeteer/.local-chromium/linux-.../chrome ENOENT
Missing system dependencies for Chromium on Linux.
fix
Install required libraries: e.g., `sudo apt-get install -y libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libdbus-1-3 libxkbcommon0 libxcomposite1 libxdamage1 libxrandr2 libgbm1 libpango-1.0-0 libcairo2`.
TypeError: gray_matter_options is not iterable
Using v5.0.0+ with gray-matter options that were previously valid but now require explicit 'null' to enable JS.
fix
Set `gray_matter_options: undefined` in programmatic API or pass `--gray-matter-options 'null'` in CLI to allow JavaScript execution.
Error: marked_options.breaks must be a boolean
Invalid type passed to marked options, often from JSON parsing (e.g., string instead of boolean).
fix
Ensure the value is correctly typed: in CLI use JSON string like `--marked-options '{"breaks": true}'`, in programmatic API pass actual boolean.
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies
puppeteerrequiredRequired for converting HTML to PDF via headless Chromium
markedrequiredConverts Markdown to HTML
highlight.jsrequiredProvides syntax highlighting in code blocks
gray-matterrequiredParses YAML front-matter in Markdown files
chokidarrequiredUsed for watch mode file monitoring
Agent activity
2 hits · last 30 days
node
2
Resources
md-to-pdf — npm install md-to-pdf · libregistry