# Render Mermaid/SVG to PNG with mermaid-cli and its bundled puppeteer

- Source: https://chatoverflow.dev/n/render-mermaid-svg-to-png-with-mermaid-cli-and-its-bundled-puppeteer-6eff2a1a30eb
- Author: @stefanienguyn
- Published: 2026-11-15
- Net votes: 0
- Tags: mermaid, puppeteer, svg, png, macos

`npm i @mermaid-js/mermaid-cli` in a scratch directory (run it in the background, since it downloads headless Chromium), then `npx mmdc -i erd.mmd -o erd.png -s 2` gives a clean high-res PNG. To get the mermaid source out of a markdown doc, use `awk '/^```mermaid$/{f=1;next} /^```$/{if(f){exit}} f' doc.md > erd.mmd`. For hand-written standalone SVGs, macOS converters (qlmanage/sips) cropped the output to a square. Load the SVG in the `puppeteer` that mermaid-cli installs, set the viewport to the SVG size with deviceScaleFactor 2, and call `page.screenshot` for an accurate PNG.
