Export of javascript grid to PDF with help of pdfmake

Export to PDF has been implemented to cover most possible use case scenarios with help of configuration encompassing both declarative options and imperative methods.

API

Most of the export to PDF configuration is based on parameters passed to exportData(...) method, and values of column.skipExport, column.exportRender, rowData.pq_hidden, rowData.pq_selected properties.

Header

Header of grid is exported by default. It can be skipped by passing header: false parameter to exportData method.

There is an eachCellHead callback parameter to exportData method to alter properties (value, link, format, comment, color, bgColor, underline, italic, bold, font, fontSize, wrap, border ) of every exported header cell.

There is an eachRowHead callback parameter to exportData method to alter properties ( css ) of every exported header row.

Columns

Columns with property skipExport set to true are excluded from exported data. This property can be applied to normal as well as grouped columns.

Hidden columns are visible in the exported PDF document by default, they can be removed by passing skipHiddenColumns: true to exportData method.

Rows

Only selected rows or rows with property pq_rowselect: true can be exported by passing selection: 'row' parameter to exportData method.

Hidden rows are visible in the exported PDF document by default, they can be removed by passing skipHiddenRows: true to exportData method.

There is an eachRow callback parameter to exportData method to alter properties ( css ) of every exported row.

Filtering

Only filtered rows are exported by default. Unfiltered rows are also exported when filterModel.hideRows: true unless skipHiddenRows: true is passed to exportData method.

Cells

Cells data rowData[ dataIndx ] along with formatting and static styles are exported by default. To include the rendered values and styles returned by column.render callback ( if any ) for all columns, pass render: true to exportData method.

To include the rendered values of few columns only, don't pass render: true to exportData method, rather set column.exportRender to true for those columns. Vice versa to exclude the rendered or formatted values of few columns only, pass render: true to exportData method and set column.exportRender to false for those columns.

ui.Export is true inside the column.render callback to help render the cells differently in exported PDF document than in grid

There is an eachCell callback parameter to exportData method to alter properties ( text, link, css, border (array i.e., [true, false, true, true]), borderColor (array i.e., ['red', 'green', 'brown', 'yellow' ]), style (used by pdfmake to apply common style to cells with default value of 'body'), alignment, rowSpan, colSpan or any other property from pdfmake ) of every exported cell.

Hyperlinks

Hyperlinks can be defined in the grid by any of the following:

  • setting rowData.pq_cellprop[ dataIndx ].link property to url
  • HYPERLINK spreadsheet formula
  • returning hyperlink html e.g., <a href="https://foo.com..">Bar</a> in column.render callback.

If hyperlinks are created in cells with the last method, then it's required to use column.exportRender to true or pass render: true to exportData method to export the hyperlinks.

Styling of hyperlinks

Hyperlinks inherit the style of their parent cells. linkStyle string parameter passed to exportData method is used to apply styles to hyperlinks if cells have no corresponding styles. Note that styles passed through linkStyle parameter have lower priority than cell styles.

Top level flow diagram of PDF export process

grid.exportData(...) first converts grid data to js intermediate object and calls pq.exportPdf() internally to finally return document definition object. Document definition object is then included in document definition ( dd ) of whole page and exported to PDF with pdfMake.createPdf(dd).open() method.

Document Definition is the javascript object representation of exported data which is used by pdfMake before final step of export to PDF.

Any customization which is not convenient through above configuration options can be done by mutating either the js intermediate object or the document definition object. It opens up many possibilities like combining multiple grids, exporting nested grids, orientation, size, margins of page, height of rows, add, remove, update new cells, rows, columns. apply borders, formatting, styles to any cell, etc

Third party libraries

Export to PDF functionality of ParamQuery grid is dependent upon following third party library:

In order to get familiar with pdfmake document definition, please check: