Skip to main content

SVG

Exports the Paper as SVG. Learn more about the SVG plugin.

Functions

toSVG()

toSVG(
paper: dia.Paper,
callback: (svg: string, error?: Error) => void,
opt?: SVGExportOptions
): void;

Converts the content of the paper to an SVG string, and calls a callback with the SVG string as the first parameter. The second parameter of the callback is an optional error in case something happened during processing.

openAsSVG()

openAsSVG(paper: dia.Paper, opt?: SVGExportOptions): void;

Opens a new window with SVG representing the content of the paper.

Configuration

The toSVG(paper, callback[, opt]) method takes an options object opt with the following properties:

preserveDimensions

By default, the resulting SVG document has a set width and height of 100%. If you'd like to have the dimensions set to the actual content width and height, set preserveDimensions to true. An object with width and height properties can also be used here if you need to define the export size explicitly.

area

An area which should be displayed in the resulting raster image. The value is an object with x, y, width, and height properties, describing the origin and size of a rectangular area on the paper in local coordinates. It defaults to the paper content bounding box - paper.getContentBBox() transformed into local coordinates.

// Export selected elements only and add a padding
const padding = 20;
toSVG(paper, callback, {
area: graph.getCellsBBox(selection.collection.toArray()).inflate(padding)
});

convertImagesToDataUris

Converts all contained images into Data URI format. It defaults to false.

useComputedStyles

When set to true, all the styles from external stylesheets are copied to the resulting SVG export. Note this requires a lot of computations, and it might significantly affect the export time.

/* External stylesheet.css */
.joint-link .connection { fill: none }
.joint-element rect { stroke: red }

When set to false, no styles from external stylesheets are copied to the resulting SVG export. Using this option requires you to make sure all the elements and links styling is inlined.

// Store SVG Attributes right on the DOM elements
link.attr('.connection', { fill: 'none' });
element.attr('rect', { stroke: 'red' });
paper.toSVG(paper, callback, { useComputedStyles: false });

stylesheet

A stylesheet(as a string) can be provided and appended to the resulting SVG export.

paper.toSVG(callback, {
stylesheet: [
'.joint-link .connection { fill: none }',
'.joint-element rect { stroke: red }'
].join('')
});

It defaults to true.

beforeSerialize

type BeforeSerialize = (doc: SVGSVGElement, paper: dia.Paper) => void | SVGElement;

A function called before the XML serialization. It may be used to modify the exported SVG before it is converted to a string. The function can also return a new SVGDocument.

toSVG(paper, callback, {
beforeSerialize: function(doc) {
// remove all links from the exported XML
Array.from(doc.querySelectorAll('.joint-link')).forEach(function(node) {
node.parentNode.removeChild(node);
});
}
});

fillFormControls

Fill in the values of all form controls (input, select, textarea), so that they are displayed in the export (form controls can be part of the export if they are inserted via foreignObject). It defaults to true.

grid

When set to true, the grid is rendered in the export. It defaults to false.

Types

SVGExportOptions

interface SVGExportOptions {
preserveDimensions?: boolean;
area?: dia.BBox;
convertImagesToDataUris?: boolean;
useComputedStyles?: boolean;
stylesheet?: string;
grid?: boolean;
beforeSerialize?: BeforeSerialize;
fillFormControls?: boolean;
}