# Export

> Export charts as SVG strings, PNG blobs or downloaded files, at any size and pixel ratio.

Every chart can be exported as a vector SVG or a raster PNG, either as data for your own code or as a file the user downloads. Exports are rendered from the chart's scene, not copied from the screen, so they can have a different size and resolution from the chart on the page.

## Download a file

```ts title="export-buttons.ts"
document.querySelector('#download-png')!.addEventListener('click', () => {
  void chart.download('png', 'revenue-by-region-q2-2026');
});

document.querySelector('#download-svg')!.addEventListener('click', () => {
  void chart.download('svg', 'revenue-by-region-q2-2026');
});
```

`download(format, fileName?)` saves the chart with the browser's normal download flow. The file name defaults to `chart`, and the extension (`.png` or `.svg`) is added unless the name already contains a dot. It returns a promise that resolves once the file has been handed to the browser.

## SVG

```ts
const svg: string = chart.toSVG();

await fetch('/api/reports/q2/charts/revenue', {
  method: 'PUT',
  headers: { 'content-type': 'image/svg+xml' },
  body: svg,
});
```

`toSVG(options?)` returns a self-contained SVG document as a string. It includes a `<title>` with the chart title and a `<desc>` with the same accessible summary that screen readers hear on the page, so the exported file is accessible too. All text and attribute values in the SVG are escaped. It returns an empty string when there is nothing to draw yet.

SVG is the best choice for print, for embedding in PDFs and for slides, because it scales without losing sharpness.

## PNG

```ts
const blob = await chart.toPNG({ width: 1600, height: 900, pixelRatio: 2 });

const form = new FormData();
form.append('attachment', blob, 'monthly-orders.png');
await fetch('/api/tickets/OPS-4821/attachments', { method: 'POST', body: form });
```

`toPNG(options?)` returns a `Promise<Blob>` of type `image/png`. By default it renders at the chart's current size and at twice the device pixel ratio (at least 2), so the image stays sharp on high-density displays and when zoomed in a document. It rejects when there is nothing to export yet, or when the browser cannot create or encode a canvas.

PNG suits chat messages, e-mail and any place that does not render SVG.

## Export options

Both `toSVG()` and `toPNG()` accept the same options:

| Option | Meaning | Default |
| --- | --- | --- |
| `width` | Export width in CSS pixels; the chart is laid out again for this width | The chart's current width |
| `height` | Export height in CSS pixels | The chart's current height |
| `pixelRatio` | PNG only: device pixels per CSS pixel | `max(2, devicePixelRatio)` |
| `background` | `false` leaves the background transparent | The theme background |

**`ExportOptions`** (interface from `@birchui/charts`, full reference: https://docs.birchui.com/api/charts.md)

```ts
interface ExportOptions
```

| Property | Type | Description |
| --- | --- | --- |
| `width?` | `number` |  |
| `height?` | `number` |  |
| `pixelRatio?` | `number` |  |
| `background?` | `boolean` |  |

An export at a different size is a fresh layout, not a scaled image: axis ticks, label rotation and decimation are recomputed for the export size. That makes `toPNG({ width: 2400, height: 1200 })` a good way to produce a detailed image of a long time series.

The export uses the chart's current state: its theme, hidden series, legend and zoom range. To export a light image from a dark page, update the theme, export, and restore it:

```ts
const previous = chart.getOptions().theme;
chart.update({ theme: 'light' });
const svg = chart.toSVG();
chart.update({ theme: previous });
```

## Exporting from a framework

The export methods live on the `ChartApi`, so read it from your component's ref (React, Solid), the exposed `api` (Vue, Angular), the `onReady` callback (Svelte action) or `element.api` (custom element), and call them as above. See [Using charts in a framework](https://docs.birchui.com/charts/overview.md#using-charts-in-a-framework).
