Pdf export in EJ2 JavaScript Gantt Control
25 Mar 202524 minutes to read
PDF Export
PDF export allows exporting Gantt data to PDF document. You need to use the pdfExport
method for exporting. To enable PDF export in the Gantt, set the allowPdfExport
to true.
To export data to PDF document, inject the PdfExport
module in Gantt.
var clickHandler = function(args){
if (args.item.id === 'GanttExport_pdfexport') {
ganttChart.pdfExport();
}
};
var ganttChart = new ej.gantt.Gantt({
dataSource: GanttData,
height: '450px',
taskFields: {
id: 'TaskID',
name: 'TaskName',
startDate: 'StartDate',
duration: 'Duration',
progress: 'Progress',
child: 'subtasks'
},
allowPdfExport: true,
toolbar: ['PdfExport'],
toolbarClick: clickHandler
});
ganttChart.appendTo('#GanttExport');
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Gantt</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript Gantt Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.2.4/material.css" rel="stylesheet" type="text/css">
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="GanttExport"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Indicators in PDF exporting
The PDF export functionality allows users to export Gantt charts enriched with dynamic indicators and accompanying images.
These indicators, represented by images,can be effortlessly defined using the base64
encoding value in the data object of datasource.This data object field should be mapped to indiactor property of task fields
.
var clickHandler = function(args){
if (args.item.id === 'GanttExport_pdfexport') {
ganttChart.pdfExport();
}
};
var ganttChart = new ej.gantt.Gantt({
dataSource: GanttData,
height: '450px',
taskFields: {
id: 'TaskID',
name: 'TaskName',
startDate: 'StartDate',
duration: 'Duration',
progress: 'Progress',
child: 'subtasks',
indicators: 'Indicators'
},
allowPdfExport: true,
toolbar: ['PdfExport'],
toolbarClick: clickHandler
});
ganttChart.appendTo('#GanttExport');
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Gantt</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript Gantt Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.2.4/material.css" rel="stylesheet" type="text/css">
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="GanttExport"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Exporting Gantt data as a blob object
In Gantt, you can export the Gantt chart data as a blob object, which allows you to preview or modify the data before exporting it.
To export the Gantt chart data as a blob object, follow these steps:
step 1: pdfExport fourth argument set as true
.
step 2: Then , pdfExpComplete
return as blob object.
/**
* Exporting Blob data
*/
let excelExpComplete = (args) => {
//This event will be triggered when excel exporting.
args.promise.then((e) => {
//In this `then` function, we can get blob data through the arguments after promise resolved.
exportBlob(e.blobData);
});
};
let pdfExpComplete = (args) => {
//This event will be triggered when pdf exporting.
args.promise.then((e) => {
//In this `then` function, we can get blob data through the arguments after promise resolved.
exportBlob(e.blobData);
});
};
let exportBlob = (blob) => {
let a = document.createElement('a');
document.body.appendChild(a);
a.style.display = 'none';
let url = window.URL.createObjectURL(blob);
a.href = url;
a.download = 'Export';
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}
const clickHandler = (args) => {
if (args.item.id === 'GanttExport_pdfexport') {
gantt.pdfExport(null,null,null,true);
}
if (args.item.id === 'GanttExport_excelexport') {
gantt.excelExport(null, null, null, true);
}
};
var gantt = new ej.gantt.Gantt({
dataSource: GanttData,
height: '450px',
taskFields: {
id: 'TaskID',
name: 'TaskName',
startDate: 'StartDate',
duration: 'Duration',
progress: 'Progress',
child: 'subtasks'
},
allowPdfExport: true,
allowExcelExport: true,
excelExportComplete: excelExpComplete,
pdfExportComplete: pdfExpComplete,
toolbar: ['PdfExport','ExcelExport'],
toolbarClick: clickHandler
});
gantt.appendTo('#GanttExport');
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Gantt</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript Gantt Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.2.4/material.css" rel="stylesheet" type="text/css">
<script src="http://cdn.syncfusion.com/ej2/20.4.48/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="GanttExport"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Single page exporting in gantt
In Gantt, we have provided support to export the Gantt component where each rows are auto-fit to the PDF document page width by setting isFitToWidth
as true in fitToWidthSettings
of PdfExportProperties
.
Also, we can customize the chart width and grid width in exported file using chartWidth
and gridWidth
by defining it as percentage in string.
var clickHandler = function(args){
if (args.item.id === 'GanttExport_pdfexport') {
var exportProperties = {
fitToWidthSettings: {
isFitToWidth: true,
}
};
ganttChart.pdfExport(exportProperties);
}
};
var ganttChart = new ej.gantt.Gantt({
dataSource: GanttData,
dateFormat: 'MMM dd, y',
taskFields: {
id: 'TaskID',
name: 'TaskName',
startDate: 'StartDate',
endDate: 'EndDate',
duration: 'Duration',
progress: 'Progress',
dependency: 'Predecessor',
child: 'subtasks',
},
columns: [
{ field: 'TaskID', width: 80 },
{ field: 'TaskName', width: 250 },
{ field: 'StartDate' },
{ field: 'EndDate' },
{ field: 'Duration' },
{ field: 'Predecessor' },
{ field: 'resources' },
{ field: 'Progress' }
],
splitterSettings: {
columnIndex: 2
},
allowPdfExport: true,
toolbar: ['PdfExport'],
toolbarClick: clickHandler,
allowSelection: true,
gridLines: 'Both',
height: '450px',
treeColumnIndex: 1,
resourceFields: {
id: 'resourceId',
name: 'resourceName'
},
highlightWeekends: true,
timelineSettings: {
topTier: {
unit: 'Week',
format: 'MMM dd, y',
},
bottomTier: {
unit: 'Day',
},
},
labelSettings: {
leftLabel: 'TaskName',
rightLabel: 'resources'
},
projectStartDate: new Date('03/25/2019'),
projectEndDate: new Date('07/28/2019')
});
ganttChart.appendTo('#GanttExport');
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Gantt</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript Gantt Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.2.4/material.css" rel="stylesheet" type="text/css">
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="GanttExport"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Multiple gantt exporting in EJ2 JavaScript Gantt control
PDF export provides an option for exporting multiple Gantt to same file. In this exported document, each Gantt will be exported to a new page of the document in same file.
var firstGantt = new ej.gantt.Gantt({
dataSource: [GanttData[0]],
taskFields: {
id: 'TaskID',
name: 'TaskName',
startDate: 'StartDate',
duration: 'Duration',
progress: 'Progress',
child: 'subtasks'
},
treeColumnIndex: 1,
allowExcelExport: true,
projectStartDate: new Date('03/31/2019'),
projectEndDate: new Date('04/14/2019'),
height:280,
toolbar: ['ExcelExport']
});
firstGantt.appendTo('#GanttExport1');
var secondGantt = new ej.gantt.Gantt({
dataSource: [GanttData[1]],
taskFields: {
id: 'TaskID',
name: 'TaskName',
startDate: 'StartDate',
duration: 'Duration',
progress: 'Progress',
child: 'subtasks'
},
treeColumnIndex: 1,
height:250,
allowExcelExport: true
});
secondGantt.appendTo('#GanttExport2');
firstGantt.toolbarClick = function(args) {
if (args.item.id === 'GanttExport1_pdfexport') {
var appendExcelExportProperties = {
multipleExport: { type: 'AppendToSheet', blankRows: 2 }
};
var firstGanttExport= firstGantt.pdfExport({}, true);
firstGanttPdfExport.then(function(pdfData){
secondGantt.pdfExport({}, false, pdfData);
});
}
}
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Gantt</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript Gantt Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.2.4/material.css" rel="stylesheet" type="text/css">
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="GanttExport1"></div>
<div id="GanttExport2" style="margin-top:10px"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Applying Themes in PDF Export
PDF export provides an option to include theme for the exported PDF document. To apply theme in exported PDF, define the theme in pdfExportProperties.
The available themes are:
- Material
- Fabric
- Bootstrap
- Bootstrap 4
var clickHandler = function(args){
if (args.item.id === 'GanttExport_pdfexport') {
var exportProperties = {
theme:"Fabric"
};
ganttChart.pdfExport(exportProperties);
}
};
var ganttChart = new ej.gantt.Gantt({
dataSource: GanttData,
height: '450px',
taskFields: {
id: 'TaskID',
name: 'TaskName',
startDate: 'StartDate',
duration: 'Duration',
progress: 'Progress',
child: 'subtasks'
},
allowPdfExport: true,
toolbar: ['PdfExport'],
toolbarClick: clickHandler
});
ganttChart.appendTo('#GanttExport');
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Gantt</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript Gantt Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.2.4/material.css" rel="stylesheet" type="text/css">
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="GanttExport"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>