Image Export in JavaScript Pivot Table
The JavaScript Pivot Table supports exporting the current visible pivot composition into image formats: PNG, JPEG and SVG. To enable image export, set the allowImageExport property to true. The HtmlExport module is included in the Syncfusion script bundle used by the sample. Once enabled, call the exportToImage method programmatically, or use the image options in the built-in Toolbar export menu.
In the following example, an external button is used to start the image export. When the button is clicked, the exportToImage method is called with the PNG format and ImageExportProperties so that the Pivot Table is saved as an image.
var pivotTableObj = new ej.pivotview.PivotView({
dataSourceSettings: {
dataSource: pivotData,
expandAll: false,
columns: [{ name: 'Year', caption: 'Production Year' }, { name: 'Quarter' }],
values: [{ name: 'Sold', caption: 'Units Sold' }, { name: 'Amount', caption: 'Sold Amount' }],
rows: [{ name: 'Country' }, { name: 'Products' }],
formatSettings: [{ name: 'Amount', format: 'C0' }],
filters: []
},
height: 320,
allowImageExport: true
});
pivotTableObj.appendTo('#PivotTable');
var exportBtn = new ej.buttons.Button({ isPrimary: true });
exportBtn.appendTo('#pdf');
document.getElementById('pdf').onclick = function () {
var imageExportProperties = { fileName: 'PivotTable.png', backgroundColor: '#ffffff', scale: 2 };
pivotTableObj.exportToImage('PNG', imageExportProperties);
};<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Pivot Grid</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript Pivot Grid Control">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css" rel="stylesheet" />
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<input id="pdf" type="button" value="Image Export" name="Image Export">
<div id="PivotTable"></div>
<br>
<div id="PivotTable2"></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>exportToImage method
The exportToImage(format, properties, isBlob) method returns a Promise that resolves to the exported image. It accepts the following parameters:
| Parameter | Type | Description |
|---|---|---|
format |
'PNG' \| 'JPEG' \| 'SVG' |
Defines the image file format. |
properties |
ImageExportProperties |
Optional. An object that customizes the export. |
isBlob |
boolean |
Optional. When set to true, the method returns a Promise resolving to the image Blob or data string instead of downloading it directly. |
The ImageExportProperties object supports the following options:
| Property | Type | Description |
|---|---|---|
fileName |
string |
Specifies the output image file name, for example 'PivotTable.png'. |
quality |
number |
A number between 0 and 1 that specifies the image quality. Applicable for the JPEG format. |
scale |
number |
A scaling factor for high-resolution rendering. |
backgroundColor |
string |
Defines the background color of the exported image, for example '#ffffff'. |