Get Free Trial
API reference
Table of contents

exportTo

exportTo(type: String, params: Object, callbackHandler: Function)

[starting from version: 1.4]

Exports grid or chart to CSV, HTML, PDF, Image or Excel format. The file can be saved to the local file system or to your server (you need to have a script on the server side).

To learn more about export, see this guide: Export and print.

Parameters

  • type – String. A type of export. There are such types available: "csv", "html", "pdf", "image" and "excel".
  • params optional – Object. It contains export parameters. The object can have 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). Only for CSV export. Default value: false.
    • filename optional – String. A default name of the resulting file.
    • destinationType optional – String. It defines where the component’s content will be exported. Destination type can be the following:
      • "file" (by default) – the component’s content will be exported to the file to the local computer.
      • "server" – the component’s content will be exported to the server (a server-side script is required).
      • "plain" – the component’s content will be returned with callbackHandler. Below are the types of the component’s content for each type of export:
        • for HTML export: String
        • for CSV export: String
        • for Excel export: Uint8Array
        • for image export: HTMLCanvasElement
        • for PDF export: jsPDF object. This destination type allows modification of the generated PDF file. jsPDF is a library which generates PDFs in client-side JavaScript. After export from Flexmonster, jsPDF object can be modified using jsPDF API and then saved. See the example.
    • excelSheetName (starting from v2.2) optional – String. To configure the sheet name when exporting to Excel file.
    • fieldSeparator optional – String. Defines specific fields separator to split CSV row in the export file (only for CSV export). Default value: ,.
    • 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.
    • pageFormat optional – String. It defines the page format for a PDF file. There are such types available: "A0", "A1", "A2", "A3", "A4", "A5". Default value: "A4".
    • pageOrientation optional – String. It defines the page orientation for a PDF file. Page orientation can be the following:
      • "portrait" (by default) – defines portrait page orientation for a PDF file.
      • "landscape" – defines landscape page orientation for a PDF file.
    • requestHeaders (starting from v2.6.13) optional – Object. It allows adding custom request headers when exporting the file to a server. The object consists of "key": "value" pairs, where "key" is a header name and "value" is its value.
    • showFilters (starting from v2.1) optional – Boolean. Excel only. Indicates whether the filters info will be shown (true) in exported Excel file or not (false). Default value: false.
    • url optional – String. A path to a server-side script that can save the file to the server. The url property is required when the destinationType property is "server".
    • useOlapFormattingInExcel (starting from v2.2) optional – Boolean. To configure how to export grid cells in Excel file if formatting is taken from OLAP cube – as a formatted string (true) or as numbers without formatting (false).
    • useCustomizeCellForData optional – Boolean. Excel only. Specifies how cells modified by customizeCell are exported: as formatted strings (true) or as numbers without formatting (false). Default value: true.
  • callbackHandler optional – Function. A JS function that is called when the data is ready to be exported. It has the following parameters:
    • result – Object. Describes the result of the export. If the export fails, the result will be null. The result object can have the following properties:
      • data – String | Uint8Array. The data to be exported. The data’s type depends on the type of the export: String for "csv" and "html", Uint8Array for "excel", "image", and "pdf".
        Note that the types are different for the "plain" destination type – learn more here.
      • filename – String. The name of the resulting file.
      • response optional – String. The server’s response. This property is defined in the result only when the destinationType is set to "server".
      • type – String. The export type.
    • error – Object. Describes the error with export. If the export is successful, the error will be null. Check out how to handle errors when exporting to a server.
      The error object can have the following properties:
      • message – String. The error message.
      • response optional – String. The server’s response. This property is defined in the error only when the destinationType is set to "server".
      • status optional – Number. The response status code. This property is defined in the error only when the destinationType is set to "server".

Examples

1) This example on JSFiddle demonstrates all types of export: CSV, HTML, PDF, Image and Excel.

2) Export to PDF, modify generated file and save locally:

flexmonster.exportTo("pdf", { destinationType: "plain" }, function(res) {
var pdf = res.data;
pdf.addPage();
pdf.text('Hello world!', 10, 10);
pdf.save(res.filename);
});

3) Export to CSV, save as a local file and add a callback handler:

flexmonster.exportTo('csv', {filename : 'flexmonster.csv'}, 
function(result) { console.log(result.data); }
);

4) Export to HTML and save as local file:

var params = { 
  filename : 'flexmonster.html'
};
flexmonster.exportTo('html', params);

5) Export to PDF file, change page orientation to landscape and save file to the server:

var params = { 
  filename : 'flexmonster.pdf',
 pageOrientation : 'landscape',
  destinationType : 'server',
  url : 'your server'
};
flexmonster.exportTo('pdf', params);

6) Export to Excel and save as local file:

flexmonster.exportTo('excel');

7) Export to PDF and set TTF font file:

flexmonster.exportTo('pdf', { 
fontUrl: 'https://cdn.flexmonster.com/fonts/NotoSansCJKtc-Regular.ttf'
});

See also

print
Export and print tutorial