export-data
The functionality is available in PRO Edition only
PRODescription
Triggers export of Gantt data to PDF, PNG, Excel, or MS Project XMLThe export-data action allows exporting Gantt data to PDF, PNG, Excel, or MS Project XML formats.
If url is provided, the action sends the current Gantt state to a ready-made server-side export service which generates files in PDF, PNG, XLSX, or MS Project XML (MSPX) formats.
If url is not provided (MS Project XML only), the action exports data using client-side logic.
Export mechanism allows overriding layout, sizing, styling, and Gantt configuration without affecting the live Gantt instance.
Usage
"export-data": ({
url?: string,
format?: "pdf" | "png" | "xlsx" | "mspx",
fileName?: string,
styles?: string,
ganttConfig?: Record<string, any>,
pdf?: {
fitSize?: boolean,
size?: "auto"
| "a0" | "a1" | "a2" | "a3" | "a4" | "letter" | "legal"
| {
width: number, // mm
height: number, // mm
},
landscape?: boolean,
styles?: string,
margins?: {
top?: number, // mm
bottom?:number, // mm
left?: number, // mm
right?: number, // mm
},
header?: string,
footer?: string,
scale?: number,
},
png?: {
fitSize?: boolean,
size?: "auto"
| "a0" | "a1" | "a2" | "a3" | "a4" | "letter" | "legal"
| {
width: number, // px
height: number, // px
},
landscape?: boolean,
styles?: string,
},
excel?: {
sheetNames?: string[],
visual?: boolean,
dateFormat?: string,
indent?: "native" | "spaces",
columns?: {
id: string,
header?: string,
type?: "string" | "number" | "date" | "progress",
width?: number,
}[],
},
}) => void;
Parameters
url- (optional) export service endpoint, recommendedhttps://export.svar.dev/gantt/{version}format- (optional)"pdf" | "png" | "xlsx" | "mspx". Default value is "pdf"fileName- (optional) name of the exported file, without extension. Default value is "gantt"styles- (optional) CSS string applied to the exported documentganttConfig- (optional) overrides Gantt configuration for export only
PDF-specific options (pdf)
fitSize- (optional) scale the chart down to fit the printable width of the page. Only scales down: if the chart already fits, nothing changes. Ignored whensizeis"auto"size- (optional) page size, can be:-
"auto"- the page is sized to match the chart -
one of the standard paper sizes:
Value Size "a0"841 x 1189 mm "a1"594 x 841 mm "a2"420 x 594 mm "a3"297 x 420 mm "a4"210 x 297 mm "letter"215.9 x 279.4 mm "legal"215.9 x 355.6 mm -
{ width: number, height: number }- custom size in millimeters
-
landscape- (optional) boolean, set totruefor landscape orientation. Applies to the standard paper sizes only; for a custom size, swapwidthandheightyourselfstyles- (optional) custom css applied in pdf exportmargins- (optional) page marginstop- (optional) top margin in millimetersbottom- (optional) bottom margin in millimetersleft- (optional) left margin in millimetersright- (optional) right margin in millimeters
header- (optional) text or HTML to display in page headerfooter- (optional) text or HTML to display in page footerscale- (optional) manual scaling factor for chart,0.1-2.0. Cannot be combined withfitSize
PNG-specific options (png)
fitSize- (optional) scale the chart down to fit the image, preserving the aspect ratio. Only scales down: if the chart already fits, nothing changes. Ignored whensizeis"auto"size- (optional) image size, can be:"auto"- the image is sized to match the chart- the same standard paper sizes as for PDF (
"a0"..."legal"), converted to pixels at 96 DPI -"a0"gives 3179 x 4494 px { width: number, height: number }- custom size in pixels
landscape- (optional) boolean, set totruefor landscape orientation. Applies to the standard paper sizes only; for a custom size, swapwidthandheightyourselfstyles- (optional) custom CSS applied in png export
Margins, headers, footers and scale are PDF-only and are ignored for PNG.
Excel-specific options (excel)
sheetNames- (optional) an array of sheet names for tasks and links, e.g.,["Tasks", "Links"]visual- (optional) boolean, set totrueto include a visual Gantt chart in excel, defaultfalsecolumns- (optional) columns configuration; each column contains the following properties:- id - (required) data field id
- header - (optional) header text
- type - (optional) "string" | "number" | "date" | "progress"
- width - (optional) column width
dateFormat- (optional) format for date fields, default"yyyy-mm-dd"indent- (optional) how the task hierarchy is rendered:"native"(default) uses Excel's own grouping,"spaces"indents the task name with spaces
MP Project specific options
Data is exported to MS Project without any specific options, but there are 2 ways to do it:
- if you provide the
urlpointing to an export service, data will be exported on the server side - if you do not provide the
url, data will be exported on the client side
api.exec("export-data", {
url, // skip for client-side export
format: "mpsx",
fileName: "gantt",
});
Example
import { useRef } from "react";
import { getData } from "../data";
import { Gantt, version } from "@svar/react-gantt";
import { Button } from "@svar-ui/react-core";
export default function ExportExample() {
const apiRef = useRef(null);
const data = getData();
const url = "https://export.svar.dev/gantt/" + version;
function handleClick() {
apiRef.current?.exec("export-data", {
url,
format: "pdf",
fileName: "gantt",
pdf: {
size: "a4",
landscape: true,
fitSize: true
}
});
}
return (
<>
<Button onClick={handleClick}> Export to PDF </Button>
<Gantt
ref={apiRef}
tasks={data.tasks}
links={data.links}
scales={data.scales}
/>
</>
);
}
Related articles: