Export Mermaid Diagrams to PNG Without a Screenshot
Published
Decide when a Mermaid diagram is enough, how to keep labels readable on a social image, and which failures a screenshot will hide.
Steps
- Write the relationship in one sentence. If you cannot say what the arrows mean, a diagram will not say it either. Start with that sentence in the heading.
- Keep the fence small enough to read at column width. A handful of nodes with short labels beats a map of the whole system. Name the language mermaid on the opening fence.
- Match the theme to the card. Use a light template for a light diagram and a dark template for a dark one. Check that labels still differ when you ignore color.
- Export and keep the source. Download PNG, open it at the size the feed will use, and paste the fence next to the image so the next edit does not start from pixels.
A diagram is a sentence with arrows
Mermaid is a text format for a small set of pictures: a flowchart, a sequence, a state diagram, a class sketch. You write the relationships in a fenced block, and a renderer turns that text into SVG. That is useful when the picture is the relationships and you want the source to stay in the same file as the paragraph around it. It is a poor fit when you need a freehand layout, a precise brand illustration, or a chart whose data should stay in a table people can sort.
Exporting that SVG to PNG is a publishing decision, not a technical trophy. The PNG is easy to drop into a chat, a slide, or a feed that will not render Mermaid. The PNG is also frozen. The next correction belongs in the fence, followed by a new export. If you only keep the PNG, you will end up tracing pixels the next time a label changes. This page is about that trade. A live editor, a docs toolchain, and this site's preview fail the same way.
Here is a fence that is already a complete idea. It does not need a tool to be useful. Anyone who can read the text can see that a note becomes either one tall image or a set of pages:
flowchart TD
note[Markdown note]
note --> tall{Taller than one canvas?}
tall -->|No| one[Export one image]
tall -->|Yes| mode{Need separate cards?}
mode -->|Yes| pages[Pages, up to five]
mode -->|No| long[One long image]
If that block is the thing you want people to look at, the heading above it should say the point in words: "A long note is either one tall image or a short set of cards." The diagram repeats the choice. It does not replace the sentence. Readers who cannot see the picture, and readers who only received a cropped thumbnail, still need the sentence. Alt text for text images applies to diagrams too. Describe the decision, not the fact that it is a flowchart.
When the fence is enough
Use Mermaid when three things are true. The structure is a graph a person could sketch on a napkin. The labels are short enough to read after the image is scaled down. And you are willing to keep the fence next to the picture. A request path, a release decision, or the order of a handshake fits. A network of forty services with real hostnames does not. At feed width those labels become gray dots, and the PNG hides the fact that the source was precise.
Prefer a list or a table when the order is linear or the values are what matter. "Try 1×, then 2×, and stop if the browser refuses" is a list. A diagram of that list adds arrows and no information. A comparison of PNG and JPEG is a table. Turning a table into an image is the better guide for that job. Reach for Mermaid when a reader would otherwise have to reconstruct "who calls whom" from prose.
Sequence diagrams earn their place when time order is the whole point and the participants are few. Keep the messages as short as the labels on a flowchart. A sequence with six participants and paragraph-length messages is a script. Publish the script as text and draw only the turn that is confusing.
Theme, contrast, and social images
Mermaid themes are color systems. A dark theme on a light card, or a light theme on a dark card, produces a diagram-shaped sticker that fights the template. Match them. In this editor, a dark template (Developer, Dark Code, Launch Card) draws the diagram with a dark theme, and a light template draws it with a neutral theme. If you render elsewhere, pick the theme on purpose and look at the result on the same background the PNG will use.
Contrast fails in predictable places. Gray edge labels on a pale node. A red node and a green node as the only way to tell failure from success. Syntax colors that were fine in the docs and disappear on a phone in sunlight. Say the state in the label: "payment failed," not a red box with no words. The type and contrast guide is written for headings, and the same test works here. View the PNG at the width of the destination. If you need the legend to understand it, the labels are not done.
Social images add a second problem: the renderer likes to use HTML inside the SVG for labels, and some capture paths turn that HTML into blank boxes. Prefer SVG text. This editor asks Mermaid for SVG labels rather than HTML labels so the words survive the export. If you are capturing a browser screenshot instead, zoom to 100%, wait until the diagram has finished drawing, and inspect the file rather than the screen. A screenshot of a loading spinner is a common way to publish an empty box with a filename that says "architecture."
Thick borders and short labels survive JPEG better than thin gray arrows do, but a diagram is still line art. Start with PNG. JPEG will ring around the arrows and smear small type. WebP is reasonable only where you control the decoder. The format guide is the longer version of that choice.
Pitfalls that show up only after export
A fence that does not parse should fail in public. The preview in this editor prints the parser message on the card, and that message is what gets exported if you ignore it. That is better than a blank region you notice after the post is up. Read the first line of the error. It usually names the token it did not expect. A missing arrow, a quote that never closed, or the word mermaid forgotten on the opening fence are the ordinary causes. Fix the text. Do not cover the error with a rectangle in an image editor.
Pagination is the next trap. A normal code block can be cut by line and still be code. A diagram cannot. Cutting a flowchart after the third node leaves invalid syntax on both pages and a picture of half a thought. This editor keeps a mermaid fence together on one page. If the diagram is taller than the canvas, the page clips and the preview warns you. Believe the warning. Move the diagram to its own card, choose a taller preset such as Story / Reel only if a full-screen frame is actually where it will go, or switch to long-image mode when the picture is allowed to be tall and scrolled. Do not shrink the font until the arrows fit and the labels become unreadably small. Where to split a long note is about the cards around the diagram. The diagram itself wants one frame.
Width fails the same way. Shorten a label that runs past the card and put the long name in the paragraph under the image.
Wait for the drawing. An export that races the renderer saves a loading line or an empty SVG. This editor waits for the diagram, for fonts, and for images before it captures. A cross-origin image elsewhere on the card can still block the whole export. The diagram source itself does not need a network call.
A sequence worth keeping as text
This one is small enough to publish beside a paragraph about export order. The picture says the same thing as the list under it. Use whichever your reader can use. Keep both if the channel is a feed.
sequenceDiagram
participant Author
participant Browser
Author->>Browser: Paste mermaid fence
Browser->>Browser: Draw SVG locally
Browser-->>Author: PNG download
The list form, for the caption or the alt text: you paste a mermaid fence, the browser draws the SVG locally, and the download is a PNG. No step in that list uploads the note. If your toolchain renders on a server, say so in the docs of that toolchain. Do not describe this browser path as a hosted renderer.
Check the file, then stop
Open the PNG at the destination width. Read every label without zooming. Look at the arrow that carries the condition, because those labels are the first to vanish. Confirm the background matches the card rather than a white strip on a dark template. Then put the fence in the repository or under the image in the post. The next edit should be a text edit.
A diagram that only works as a full-screen SVG in the docs should stay an SVG in the docs. Export a PNG when the channel needs pixels. Export a smaller diagram, not a shrunken copy of the large one, when the channel is a feed. The code image guide covers ordinary fences. A mermaid fence is that problem plus a layout: the words and the arrows both have to survive the same crop.
Frequently asked questions
- Does the diagram get sent to a server to be drawn?
- No. A mermaid fence is parsed and drawn in your browser, then included in the same local export as the rest of the card. The Markdown is not uploaded to render it.
- What happens if the diagram is invalid?
- The preview shows the parser error on the card instead of an empty box. Fix the fence and export again. A blank region in the download usually means the preview was still rendering, or the error was ignored.
- Will a diagram split across carousel pages?
- The editor keeps one mermaid fence on a single page instead of cutting it the way it cuts a long code listing. A diagram taller than the canvas can still be clipped. Use a taller preset, fewer nodes, or long-image mode, and read the clip warning before you export.
- Can someone copy the diagram from the PNG?
- No. Put the mermaid source in the post, the repository, or the docs. The image is the reading view. The fence is the editable one.