Image Export in TypeScript Pivot Table

The TypeScript 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 and inject the HtmlExport module into the Pivot Table. 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.

import { PivotView, IDataSet, HtmlExport, ImageExportProperties } from '@syncfusion/ej2-pivotview';
import { Button } from '@syncfusion/ej2-buttons';
import { pivotData } from './datasource.ts';

PivotView.Inject(HtmlExport);
let pivotTableObj: PivotView = new PivotView({
    dataSourceSettings: {
        dataSource: pivotData as IDataSet[],
        expandAll: false,
        drilledMembers: [{ name: 'Country', items: ['France'] }],
        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: []
    },
    allowImageExport: true,
    height: 320
});
pivotTableObj.appendTo('#PivotTable');

let exportBtn: Button = new Button({ isPrimary: true });
exportBtn.appendTo('#pdf');

document.getElementById('pdf').onclick = function () {
    let imageExportProperties: 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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    <div id='loader'>Loading....</div>
    <div id='container'>
        <input id="pdf" type="button" value="Image Export" name="Image Export" />
        <div id='PivotTable'></div>
        <br />
        <div id='PivotTable2'></div>
    </div>
</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'.