All current data from the grid or chart can be exported in various formats. It is an easy and convenient way to save the results of your work. The exported file can be saved to the local file system or to your server.
The API call exportTo() is used for exporting and the API call print() is used for printing. To see different examples of exporting and printing, visit our Examples page.
The printing of the contents of the grid or chart via the OS print manager can be configured with the following options
object inside the print()
call:
options
(optional) – Object. Can contain the following print properties:
header
(from v2.211) (optional) – String. The header is set using the HTML format (tags, inline styles, img with base64 src), rendered in the browser, and added as an image to the printed PDF file. The following tokens can be used: ##CURRENT-DATE##
, ##PAGE-NUMBER##
. They will be replaced by the appropriate data.footer
(from v2.211) (optional) – String. The footer is set using the HTML format (tags, inline styles, img with base64 src), rendered in the browser, and added as an image to the printed PDF file. The following tokens can be used: ##CURRENT-DATE##
, ##PAGE-NUMBER##
. They will be replaced by appropriate data.Below is an example with header
and footer
parameters:
var options = {
header:"<div>##CURRENT-DATE##</div>",
footer:"<div>##PAGE-NUMBER##</div>"
}
flexmonster.print(options);
Check out how this sample works on JSFiddle.
To export to HTML set the first parameter of exportTo()
to "html"
. The second parameter is an optional object that can contain the following properties:
filename
(optional) – String. The name of the created file. Default name: "pivotgrid"
.
destinationType
(optional) – String. Defines where the component’s contents will be exported. The destination type can be the following: "file"
(default) – the component’s contents will be exported to a file to the local computer."server"
– the component’s contents will be exported to the server (a server-side script is required)."plain"
– the component’s contents will be exported as a string and returned with callbackHandler
. footer
(from v2.211) (optional) – String. The footer can be also set using the HTML format (tags, inline styles, img with base64 src). The following token can be used: ##CURRENT-DATE##
. It will be replaced by appropriate data.header
(from v2.211) (optional) – String. The header can be also set using the HTML format (tags, inline styles, img with base64 src). The following token can be used: ##CURRENT-DATE##
. It will be replaced by appropriate data.url
(optional) – String. To save the file to the server, provide the component with a path to the server-side script that can save this file.The third parameter of exportTo()
is the callbackHandler
– a function that is called when the data is ready to be exported. The callbackHandler
takes an object with the data
property containing the data to be exported. The callbackHandler
is an optional parameter.
Here is an example of how to save to a local HTML file:
var params = {
filename: 'flexmonster',
header:"##CURRENT-DATE##",
footer:"<div style='color:#df3800'>Table Footer</div>",
};
flexmonster.exportTo('html', params);
Try the example on JSFiddle.
Saving to server:
var params = {
destinationType: 'server',
url: 'your server-side script'
};
flexmonster.exportTo('html', params);
To export to CSV set the first parameter of exportTo()
to "csv"
. The second parameter is an optional object that can contain the following properties:
alwaysEnclose
(optional) – Boolean. Indicates whether to enclose all CSV fields in quotes. When set to true
, the fields are always enclosed in quotes. Otherwise, they will be enclosed only when necessary (e.g., if a field contains a comma: Bike, "$15,000", blue
). Default value: false
.fieldSeparator
(optional) – String. Defines the field separator to split each CSV row in the export file. Default separator: ,
.filename
(optional) – String. The name of the created file. Default name: "pivot"
.destinationType
(optional) – String. Defines where the component’s contents will be exported. The destination type can be the following:"file"
(default) – the component’s contents will be exported to a file to the local computer."server"
– the component’s contents will be exported to the server (a server-side script is required)."plain"
– the component’s contents will be exported as a string and returned with callbackHandler
. footer
(from v2.7.24) (optional) – String. There’s an option to apply the multirow footer in the following way: "Row1\nRow2\nRow3"
.header
(from v2.7.24 ) (optional) – String. There’s an option to apply the multirow header in the following way: "Row1\nRow2\nRow3"
. url
(optional) – String. To save the file to the server, provide the component with a path to the server-side script that can save this file. The third parameter of exportTo()
is the callbackHandler
– a function that is called when the data is ready to be exported. The callbackHandler
takes an object with the data
property containing the data to be exported. The callbackHandler
is an optional parameter.
Here is how to save a local CSV file and get the resulting data within the callbackHandler
:
var params = {
filename: 'flexmonster',
header:"First header row\nSecond header row",
footer:"Table Footer",
};
flexmonster.exportTo('csv', params, function(result) {
console.log(result.data)
});
Check out an example on JSFiddle.
Saving to server:
var params = {
destinationType: 'server',
url: 'your server-side script'
};
flexmonster.exportTo('csv', params);
To export to Excel set the first parameter of exportTo()
to "excel"
. The second parameter is an optional object that can contain the following properties:
filename
(optional) – String. The name of the created file. Default name: "pivot"
.destinationType
(optional) – String. Defines where the component’s contents will be exported. The destination type can be the following: "file"
(default) – the component’s contents will be exported to a file to the local computer."server"
– the component’s contents will be exported to the server (a server-side script is required). "plain"
– the component’s contents will be exported as a Uint8Array and returned with callbackHandler
.excelSheetName
(from v2.2) (optional) – String. Sets the sheet name.footer
(from v2.7.24) (optional) – String. There’s an option to apply the multirow footer in the following way: "Row1\nRow2\nRow3"
.header
(from v2.7.24) (optional) – String. There’s an option to apply the multirow header in the following way: "Row1\nRow2\nRow3"
. showFilters
(from v2.1) (optional) – Boolean. Indicates whether the filters info will be shown (true
) in the exported Excel file or not (false
). Default value: false
.url
(optional) – String. To save the file to the server, provide the component with a path to the server-side script that can save this file.useOlapFormattingInExcel
(from v2.2) (optional) – Boolean. Indicates how to export the grid cells to the Excel file if the formatting in the component is taken from an OLAP cube – as a formatted string (true
) or as numbers without any formatting (false
). In previous versions, this was not configurable and the cells were exported as formatted strings.useCustomizeCellForData
(optional) – Boolean. Specifies how cells modified by customizeCell
are exported: as formatted strings (true
) or as numbers without formatting (false
). Default value: true
.The third parameter of exportTo()
is the callbackHandler
– a function that is called when the data is ready to be exported. The callbackHandler
takes an object with the data
property containing the data to be exported. The callbackHandler
is an optional parameter.
This is how to save a report as a local Excel file:
var params = {
filename: 'flexmonster',
header:"First header row\nSecond header row",
footer:"Table Footer",
excelSheetName: 'Report',
showFilters: true,
useOlapFormattingInExcel: false
};
flexmonster.exportTo('excel', params);
Check out an example on JSFiddle.
Saving to the server:
var params = {
destinationType: 'server',
url: 'your server-side script'
};
flexmonster.exportTo('excel', params);
To export to an image set the first parameter of exportTo()
to "image"
. The second parameter is an optional object that can contain the following properties:
filename
(optional) – String. The name of the created file. Default name: "pivotgrid"
.destinationType
(optional) – String. Defines where the component’s contents will be exported. The destination type can be the following: "file"
(default) – the component’s contents will be exported to a file to the local computer."server"
– the component’s contents will be exported to the server (a server-side script is required)."plain"
– the component’s contents will be exported as an HTMLCanvasElement and returned with callbackHandler
. footer
(from v2.7.24) (optional) – String. The footer can be also set using the HTML format (tags, inline styles, img with base64 src). The following token can be used: ##CURRENT-DATE##
. It will be replaced by appropriate data.header
(from v2.7.24) (optional) – String. The header can be also set using the HTML format (tags, inline styles, img with base64 src). The following token can be used: ##CURRENT-DATE##
. It will be replaced by appropriate data. url
(optional) – String. To save the file to the server, provide the component with a path to the server-side script that can save this file.The third parameter of exportTo()
is the callbackHandler
– a function that is called when the data is ready to be exported. The callbackHandler
takes an object with the data
property containing the data to be exported. The callbackHandler
is an optional parameter.
Here is how to save the image as a local file:
var params = {
filename: 'flexmonster',
header:"##CURRENT-DATE##",
footer: "<div style='color:#df3800'>Table Footer</div>"
};
flexmonster.exportTo('image', params);
Try the example on JSFiddle.
Saving to server:
var params = {
destinationType: 'server',
url: 'your server-side script'
};
flexmonster.exportTo('image', params);
To export to PDF set the first parameter of exportTo()
to "pdf"
. The second parameter is an optional object that can contain the following properties:
filename
(optional) – String. The name of the created file. Default name: "pivot"
.destinationType
(optional) – String. Defines where the component’s contents will be exported. The destination type can be the following: "file"
(default) – the component’s contents will be exported to a file to the local computer."server"
– the component’s contents will be exported to the server (a server-side script is required)."plain"
– this destination type allows you to modify the generated PDF file. The component’s contents will be exported to a jsPDF object and returned with the callbackHandler
. jsPDF is a library that generates PDFs using client-side JavaScript. After exporting from Flexmonster, the jsPDF object can be modified using the jsPDF API and then saved. See an example. fontUrl
(from v2.7.7) (optional) – String. The URL to the TTF font file for saving PDF reports in Chinese, Arabic or any other language. Check out the list of ready-to-use Google Noto Fonts that you can use to support almost any language in the world. Only fonts in standard TTF format are supported.footer
(from v2.211) (optional) – String. The footer can be also set using the HTML format (tags, inline styles, img with base64 src), rendered in the browser, and added as an image to the exported PDF file. The following tokens can be used: ##CURRENT-DATE##
, ##PAGE-NUMBER##
. They will be replaced by appropriate data.header
(from v2.211) (optional) – String. The header can be also set using the HTML format (tags, inline styles, img with base64 src), rendered in the browser, and added as an image to the exported PDF file. The following tokens can be used: ##CURRENT-DATE##
, ##PAGE-NUMBER##
. They will be replaced by appropriate data.pageFormat
(optional) – String. Defines the page format for the PDF file. The following sizes are available: "A0"
, "A1"
, "A2"
, "A3"
, "A4"
, "A5"
. Default value: "A4"
.pageOrientation
(optional) – String. Defines the page orientation for the PDF file. Page orientation can be either "portrait"
or "landscape"
. Default value: "portrait"
. url
(optional) – String. To save the file to the server, provide the component with a path to the server-side script that can save this file.The third parameter of exportTo()
is the callbackHandler
– a function that is called when the data is ready to be exported. The callbackHandler
takes an object with the data
property containing the data to be exported. The callbackHandler
is an optional parameter.
Here is how to save the report as a local PDF file:
var params = {
filename: 'flexmonster',
header:"##CURRENT-DATE##",
footer:"<div>##PAGE-NUMBER##</div>",
pageOrientation: 'landscape'
};
flexmonster.exportTo('pdf', params);
Check out the example on JSFiddle.
Saving to server:
var params = {
destinationType: 'server',
url: 'your server-side script'
};
flexmonster.exportTo('pdf', params);
Modifying generated PDF and saving locally:
flexmonster.exportTo("pdf", { destinationType: "plain" }, function(res) {
var pdf = res.data;
pdf.addPage();
pdf.text('Hello world!', 10, 10);
pdf.save(res.filename);
});
Setting TTF font file:
flexmonster.exportTo('pdf', {
fontUrl: 'https://cdn.flexmonster.com/fonts/NotoSansCJKtc-Regular.ttf'
});
You can easily export the report without a browser using Puppeteer — a JavaScript library for working with headless browsers.
We prepared a sample GitHub project with Flexmonster and Puppeteer. It demonstrates how to export Flexmonster reports in headless browsers with Puppeteer. To run the sample project, follow these steps:
.zip
archive with the sample or clone it from GitHub with the following command:git clone https://github.com/flexmonster/pivot-puppeteer
cd pivot-puppeteer
package.json
: npm install
npm start
When the export is complete, find the saved files in the storage/
folder.
Now let’s have a look at the project files’ contents to understand how the sample project works:
The index.html file contains the pivot table subscribed to the ready
, reportcomplete
, and exportcomplete
events. These events will let us know when the report is ready to be exported and when the export is finished.
When the component triggers one of these events, the dispatchEvent() method triggers the same event for the browser window. This approach allows the browser to handle the component’s events in its scope.
You can specify other Flexmonster events similarly.
The pivot.js file runs the browser and performs the export. This section describes its key points.
In the sample project, we use ready
, reportcomplete
, and exportcomplete
events to export the report. You can add other Flexmonster events in a similar way – check it out.
The next important part of the project is where we set a report. It’s done using the setReport() function as soon as the component is initialized. You can replace the default report with an inline report or a link to it.
The exportTo()
function supports changing the file name or exporting the report to your server – just specify the corresponding export parameters. The structure of the parameters is the same as in the flexmonster.exportTo() API call.
For technical details on how the export is performed, see comments in the pivot.js
file.