{"id":18539,"library":"md-to-pdf","title":"md-to-pdf","description":"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.","status":"active","version":"5.2.5","language":"javascript","source_language":"en","source_url":"https://github.com/simonhaenisch/md-to-pdf","tags":["javascript","markdown-to-pdf","cli","pdf-generation","documentation","assignments","homework","markdown","pdf","typescript"],"install":[{"cmd":"npm install md-to-pdf","lang":"bash","label":"npm"},{"cmd":"yarn add md-to-pdf","lang":"bash","label":"yarn"},{"cmd":"pnpm add md-to-pdf","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for converting HTML to PDF via headless Chromium","package":"puppeteer","optional":false},{"reason":"Converts Markdown to HTML","package":"marked","optional":false},{"reason":"Provides syntax highlighting in code blocks","package":"highlight.js","optional":false},{"reason":"Parses YAML front-matter in Markdown files","package":"gray-matter","optional":false},{"reason":"Used for watch mode file monitoring","package":"chokidar","optional":false}],"imports":[{"note":"ESM import (package ships TypeScript types). The default export is not available; use named import. For CommonJS, use const { convertMdToPdf } = require('md-to-pdf').","wrong":"const convertMdToPdf = require('md-to-pdf')","symbol":"convertMdToPdf","correct":"import { convertMdToPdf } from 'md-to-pdf'"},{"note":"Direct destructuring is the correct pattern. The module does not export a default object.","wrong":"const mdToPdf = require('md-to-pdf'); mdToPdf.convertMdToPdf()","symbol":"convertMdToPdf (CommonJS)","correct":"const { convertMdToPdf } = require('md-to-pdf')"},{"note":"These are type-only exports. Use import type for TypeScript. Available since v5.0.","wrong":"import { PdfOptions } from 'md-to-pdf'","symbol":"Options types","correct":"import type { PdfOptions, MarkedOptions } from 'md-to-pdf'"}],"quickstart":{"code":"import { convertMdToPdf } from 'md-to-pdf'\n// For CommonJS: const { convertMdToPdf } = require('md-to-pdf')\n\nasync function main() {\n  const pdf = await convertMdToPdf('# Hello, World!\\n\\nThis is a **markdown** string.', {\n    pdf_options: { format: 'A4', margin: { top: '20mm', bottom: '20mm' } },\n    css: 'body { font-family: Helvetica, Arial, sans-serif; }'\n  })\n  // pdf is a Buffer with the PDF content\n  require('fs').writeFileSync('output.pdf', pdf)\n}\nmain()","lang":"typescript","description":"Demonstrates programmatic API: convert a markdown string to PDF with custom PDF options and CSS, then save to file."},"warnings":[{"fix":"To enable JS in front-matter, pass `--gray-matter-options 'null'` on CLI or set `gray_matter_options: undefined` in programmatic options.","message":"v5.0.0 disables JavaScript engine in front-matter by default to prevent remote code execution (RCE).","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Install Puppeteer's missing dependencies as per https://pptr.dev/troubleshooting#chrome-headless-doesnt-launch-on-unix","message":"Puppeteer may not be bundled with the package; additional system dependencies may be required (e.g., libnss3, libnspr4 on Linux).","severity":"gotcha","affected_versions":"All"},{"fix":"Use `--body-class` in CLI or `body_class` in programmatic options.","message":"The `body-classes` option (plural) is deprecated; use `body-class` (singular) instead.","severity":"deprecated","affected_versions":">=5.0.0?"},{"fix":"Upgrade to v5.2.4 or later, or set `--md-file-encoding` for certain edge cases.","message":"Node 14 compatibility was broken in v5.2.3 due to use of `.replaceAll`, but fixed in v5.2.4.","severity":"gotcha","affected_versions":"5.2.3"},{"fix":"Pass `highlight` function in `marked_options` instead of custom renderer.","message":"v5.1.0 changed the code highlighting option from overwriting `renderer.code` to using the `highlight` option in marked.","severity":"breaking","affected_versions":">=5.1.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run `npm install puppeteer` in your project or install globally with `npm i -g puppeteer`.","cause":"Puppeteer is a peer dependency and may not be installed automatically.","error":"Error: Cannot find module 'puppeteer'"},{"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`.","cause":"Missing system dependencies for Chromium on Linux.","error":"Error: Failed to launch the browser process! spawn /path/to/node_modules/puppeteer/.local-chromium/linux-.../chrome ENOENT"},{"fix":"Set `gray_matter_options: undefined` in programmatic API or pass `--gray-matter-options 'null'` in CLI to allow JavaScript execution.","cause":"Using v5.0.0+ with gray-matter options that were previously valid but now require explicit 'null' to enable JS.","error":"TypeError: gray_matter_options is not iterable"},{"fix":"Ensure the value is correctly typed: in CLI use JSON string like `--marked-options '{\"breaks\": true}'`, in programmatic API pass actual boolean.","cause":"Invalid type passed to marked options, often from JSON parsing (e.g., string instead of boolean).","error":"Error: marked_options.breaks must be a boolean"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}