본문으로 건너뛰기

Exports

On the CLI, you may export .d2 into

SVG

SVG is the default export format on the CLI. If you don't specify an output, the export file will be the input name as an SVG file.

For example, d2 in.d2 will produce a file named in.svg.

The resulting SVG has CSS injected into it. This, along with the use of HTML <foreignObject>s used to make Markdown work, means that the SVG is meant to be viewed in a web context. For example, opening it up in your browser, embedding it onto a webpage. It may not look right without a web context, like in Inkscape or Adobe Illustrator.

On the CLI, if you pass in -

  • for the input, it reads D2 from stdin
  • for the output, it writes SVG to stdout
Technical details on SVG exports This information might be useful if you're

planning on doing post-processing on the SVG exports.

Element IDs: All shape elements get CSS classes with base64-encoded IDs for safe CSS targeting. For example, a shape with ID my-shape gets class bXktc2hhcGU (base64 encoding of "my-shape").

Unique identifiers: Each diagram gets a deterministic hash prefix (e.g., d2-1234567890) used for clip paths, gradients, and other SVG elements to prevent conflicts when multiple diagrams are on the same page.

PNG

PNG exports work by Playwright spinning up a headless browser, putting the SVG onto it, and taking a screenshot. The first invocation of Playwright will download its dependencies, if they don't already exist on the machine.

정보

If you get a message like err: failed to launch Chromium, you can try installing Playwright dependencies outside of D2 on your machine. For example:

See #744 for more.

PDF

PDF exports are the result of taking PNG exports and placing them on PDF pages, along with headers and fonts. As such, dependencies needed for PNG exports are also needed for PDF exports.

PDF is more interactive than PNG, but less interactive than SVG.

For example, animate keyword won't show up in PDF exports like they would in SVG.

But links can still be clickable in PDFs.

linked PDF example in D2

PPTX

Similar to PDF exports. This export format is useful for giving presentations when used with composition (e.g. diagram with multiple Layers, Scenarios, Steps).

GIF

This export format is useful for giving presentations when used with short compositions. For example, show two Scenarios, show a couple of steps. Something that the audience can digest in a loop that lasts a couple of seconds without needing to flip through it manually.

Stdout

D2 accepts - in place of the input and/or output arguments. SVG is used as the format for Stdout output.

For example, this writes a D2 script of x -> y and outputs it to a file example.svg.