Print / export to PDF

TreeGrid documentation

Print and export of grid data to PDF document.

The printing to PDF shares setting with standard printing, see Choosing items to print and Page size.

In TreeGrid print menu it is possible to choose which columns or rows to print and set various printing options.
See Configuration menu for basic settings or Print menu for special settings.

The printing must be done on server side by some third party tool that converts HTML to PDF.
By default (since 14.0) we use (headless) Chrome for the HTML to PDF conversion.
The examples for PDF export using Chrome are included in default trial package for various server scripts (ASP.NET, PHP, JSP).

If the Chrome solution does not support your required features, we provide also two sample tools, WKHtmlToPdf as GPL command line tool using WebKit for rendering and EVOHtmlToPdf Converter as commercial ASP.NET library using IE for rendering. There can be use also any other HTML to PDF conversion tool.
These two tools and examples with them are in extra package at http://www.treegrid.com/treegrid/Download/TreeGridEvalPDFExport.zip

The PDF export works in this way:
1) On client side in main TreeGrid application a user chooses what and how will be printed (in menu, see ExportPDFCol and page size printing options).
2) The client side TreeGrid uploads the chosen setting and other configuration to server (the url is defined by ExportPDF_Url).
3) The server side script creates HTML of the same TreeGrid according to received configuration by PrintTreeGrid function. It shows simple page that loads TreeGrid script and creates the grid by PrintTreeGrid() instead of <bdo> or TreeGrid(). It should use the same data as the client side TreeGrid.
4) The server side script converts the HTML to PDF by some automation tool, e.g. WKHtmlToPdf or EVOHtmlToPdf.
5) The server side script returns the created binary PDF file as response of the client's request (received in point 2).

Client side settings

Settings to set up the communication from client side to server side

new 6.7 <treegrid,bdo> string

ExportPDF_Url

Destination for page that receives the grid configuration and creates and returns the binary PDF data.
This server URL should return the exported data as PDF file in HTTP content-type="application/pdf".
This communication is not done by AJAX, but always by hidden form, so it can be used even if no ActiveX is supported.
Export cannot use Soap or Get communication.
To this URL is uploaded actual grid configuration and the server script should create and return the exported data by itself.
new 6.7 <treegrid,bdo> string

ExportPDF_Data

["Data"]
The name of form input in that will be uploaded exported data or configuration to Export_Url
new 6.7 <treegrid,bdo> string

ExportPDF_Type

["settings"]
Which configuration will be sent to server for export, case insensitive.
Settings
To server will be send configuration (sorting, grouping, filters) and columns (positions, visibility and widths) and print settings in XML.
The server must generate the requested report in PDF according to the settings and stored data and return it to client.
Expanded
Sends ids of all expanded rows in <Cfg Expanded/> attribute, comma separated.
Changes
Sends modified rows - rows with set attribute Added, Deleted, Moved or Changed (modified values). The rows are sent in <Changes> tag.
When set, the export does not save the changes to server before export, but includes them in the export request.

This attribute supports also all other settings like Cookie or Data defined for _Type, see Upload_Typey.

PDF Export supports also other Upload_ attributes: ExportPDF_Flags (see Upload_Flags), ExportPDF_Attrs (see Upload_Attrs), ExportPDF_Format (see Upload_Format) and ExportPDF_XML (see xxx_XML).

Printing options

Settings specific to PDF chosen on client side and used on server side
Other printing setting are described at Choosing items to print and Page size

new 6.7 chg 9.0 <Cfg> int

ExportPDFCols

[2]
If and which menu will be shown before doing export to PDF.
0 - no menu
1 - menu with columns to select which columns will be exported. Only columns with CanPrint>='1' are listed. (In TG prior 9.0 was tested CanExport instead of CanPrint.)
2 - (new 6.7) menu with column and also with options for export to PDF
See Configuration menu for basic settings or Print menu for special settings.
new 12.1 <Cfg> string

PDFFormat

["pdf"] Saved to cookies, avoid it by <Cfg PrintLap='1'/>
Format to export. TreeGrid provides server code and converters only for "pdf" format. Other formats must be handled by some custom converter. Case insensitive.
It is also file extension to save the report by, always lowercase.
new 12.1 <Cfg> string

PDFName

["Export"] Saved to cookies, avoid it by <Cfg PrintLap='1'/>
File name to save the exported PDF by. It is without extension, extension is set by PDFFormat.
new 6.7 chg 12.0 <Cfg> int

PDFFitPage

[0] Saved to cookies, avoid it by <Cfg PrintLap='1'/>
It can be set also in Print to PDF menu (when ExportPDFCols = 2) and is sent to server along with the request.
The options are defined in Text.xml, by default are: 0 = Real size, 1 = Width, 2 = Height (since 9.0), 3 = Single page.
Since 9.0 it is used by TreeGrid to calculate DPI and page size and apply page breaks.
Since 12.0 this attribute is used not only for PDF export, but also for normal printing to printer if set PrintZoomFit.
new 6.7 chg 9.0 <Cfg> int

PDFText

[1] Saved to cookies, avoid it by <Cfg PrintLap='1'/>
It can be set also in Print to PDF menu (when ExportPDFCols = 2) and is sent to server along with the request. It is not used by TreeGrid.
Since 9.0 it is hidden in menu by default, to show it set <Lang><Text PDFText='the text'/></Lang>
The options are defined in Text.xml, by default are: 0 = Image only, 1 = Selectable text, 2 = Selectable text with embedded fonts
new 6.7 deleted 9.0 <Cfg> int

PDFPageSize

[8] Saved to cookies, to not load it, set PrintLap='1'
Replaced by PrintPageSize.
new 6.7 deleted 9.0 <Cfg> int

PDFPageOrientation

[0] Saved to cookies, to not load it, set PrintLap='1'
Replaced by PrintPageOrientation.

Client side API

new 6.7 <Actions>

ExportPDF

Attached to event OnClickButtonExportPDF
Creates a request for report in PDF and sends it to server.
new 6.0 upd 7.0 API event bool

OnExportStart

(TGrid grid, bool pdf)
Called before the export report is generated.
(since 7.0) pdf is true for export to PDF.
Return true to cancel export.
new 12.1 API event bool

OnExportInit

(TGrid grid, string[Name] Cols, string[id] Rows, object source)
Called after the options are chosen by a user in menu and before the export is generated.
Cols is array of all columns to export, by column name. If the column is not listed, it is not exported.
Rows is array of rows to export, by row id. If the row is not listed, it is exported according to its CanExport value.
It is possible to modify these arrays.
Other settings like ExportFormat or ExportType are already read from configuration and can be modified in the grid attributes.
source is data source used, for PDF export it is grid.Source.ExportPDF.
Can be used for custom export or export to different formats.
Return true to cancel export.
chg 12.1 API event object

OnExport

(TGrid grid, object data, int type)
Called after the export or configuration is created and before it is saved to disk or sent to server.
data is the created export or configuration. It can be string or (since 12.1) Blob object.
(since 12.1) type is data type: 0 export configuration, 1 export pdf configuration, 2 direct data to save, 3 direct data to send to server.
Return true to suppress the export.
Or return new or updated data to send.
new 12.1 API method void

SaveExport

(string data, object source = null)
Saves exported data to file (source = null) or sends it to server to source like grid.Source.Export or grid.Source.ExportPDF.
chg 12.1 API method void

SendExport

(string data, string url, string name)
Sends the data to server as export or configuration.
Since 12.1 is does not call OnExport.

Server side API

API to generate the HTML for the HTML to PDF tool.

new 6.7 upd 14.1 global func. TGrid

PrintTreeGrid

(object Source, string tag = "", object param = null, object Page)
Creates and renders new grid by API according to settings in Source.
It renders inactive grid for printing purposes, especially when printing to PDF.
Use it only with GridE.js static script and not with GridED.js on demand script, because with GridED.js script is asynchronous.
It prints the grid synchronously, without scrollbars, loads also all pages and child pages in server paging, so it can be very slow for long grids.
For long grids and routines that use IE for exporting to PDF it can be useful to disable slow script message in IE by:
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Styles\MaxScriptStatements = 0xFFFFFFFF
Its attributes and other behavior is similar to TreeGrid method.
Source can be:
a) string with <treegrid> or <bdo> tag with all settings like it is included directly to HTML
Example: TreeGrid("<treegrid Data_Url='Grid2.xml'></treegrid>","Main");
b) string with XML to create the grid from. The grid will have only Data_Data source defined.
Example: TreeGrid("<Grid><Cols><C Name='A'/></Cols><Body><B><I A='xxx'/></B></Body></Grid>","Main");
c) object with parsed <treegrid> / <bdo> tag.
All multilevel attributes (data sources) containing '_' must be set as nested objects, split by the '_',
for example Data_Param_Test is accessed as Source.Data.Param.Test and set as {Data:{Param:{Test:'val'}}}
Eample: PrintTreeGrid({Layout:{Url:"Grid2Def.xml"},Data:{Url:"Grid2.xml"}},"Main");
tag is the main div tag to render grid into. The tag can be an id of the tag or the HTMLElement itself. The htm tag must exist in time of calling the function.
If the parameter is missing, the id of the tag must be set in input XML by attribute <Cfg Tag/>. See <treegrid> tag description.
param are (since 14.1) the attributes that will be set to the created grid.
It is useful to pass here some link to the parent object that creates TreeGrid, e.g. this.Grid = TreeGrid({Data:{Url:"Test.xml"}},"Main",{id:"TestGrid",Component:this});
The param can contain id attribute or the param can be a string to be used as the id attribute.
The id is the unique id of the grid, used to identify it, to access the grid object globally by Grids[id].
The id is also used in saving configuration to the cookies if not set CfgId attribute.
If the id is not set in/by param, the id of the grid can be set in the Source or input XML by attribute <Cfg id/> or it is autogenerated.
Page since 14.0 are parameters for printing every page:
Width - width of the printed page in millimeters, including margin.
Height - height of the printed page in millimeters, including margin.
MarginWidth - width of the left+right page margin in millimeters.
MarginHeight - height of the top+bottom page margin in millimeters.
Dpi - dpi (scale) ratio, 96 is 1 (default), higher value decreases page scale and vice versa.
FitPage - How to fit content to page 0 - none, 1 - to width, 2 - to height, 3 - single page. Set only for server paging, when client side cannot calculate dpi for fit
Header - html displayed in top margin on every page.
HeaderLink - url assigned to Header.
HeaderBackground - background color for the Header.
Footer - html displayed in bottom margin on every page. By default it displays white background to hide the default Chrome footer. In case of problems set Footer="" to ignore it at all.
FooterBackground - background color for the Footer.
FooterLink - url assigned to Footer.
Update - value of ChangesUpdate, default value is 0 for printing to PDF.
The function returns created TGrid object.
Since 14.1 if used GridED.js on demand script instead of GridE.js static script, the function downloads the GridE.js asynchronously and always returns before the TreeGrid is initialized and loaded - the returned grid object has set only its Source attribute.
It returns null for fatal error. It fails if the Source is null or cannot be used as one of the three options.
The Source is accessible by API by grid.Source attribute.
It calls all loading events like OnLoaded or OnReady.
(Since 7.0) It calls also OnRenderStart event, when all data are loaded, even for server paging and OnRenderFinish when the grid is fully rendered (even for paging).
API event string

OnGetHtmlValue

(TGrid grid, TRow row, string col, type val)
Called to get formatted cell value to display. val is unformatted cell value.
Returns formatted value. If returned value is not null, it is used instead of internal formatting by Format or Enum attributes.
Return null to use default formatting behavior.

Data sent from client to server

To server is sent just actual grid configuration - sorting, grouping, filters, search and column width, positions and visibility.
In "custom" parameter "ExportFormat" is sent "pdf" (to distinguish it from other export formats when sharing the export url)
In "custom" parameters PrintPageSize, PrintPageOrientation, PrintDPI, PrintMarginWidth, PrintMarginHeight, PDFFitPage and PDFText are sent chosen PDF options, see them in Page size and Printing options.
Since 14.0 in "custom" parameters PrintPageWidth and PrintPageHeight is sent the page size in millimeters, including margins.

It is possible to upload also other data from client like changes, set Export_Type="Settings,Changes".
The configuration is sent as standard TreeGrid XML in Data attribute.

XML structure of request for PDF export

Used as request sent to data source ExportPDF to get the exported grid when set server side export (ExportPDF_Type="Settings").
It contains session identification and grid configuration to sort, group, filter and search exported data on server.
It contains also columns settings - width, position and visibility.

<Grid>
<IO ... attributes with request settings ... /> (Session attribute)
<Cfg ... attributes with grid settings read from cookies ... />
(Sort, Group, ReSort, ReCalc, TimeZone attributes)
(SearchAction, SearchExpression, SearchType, SearchMethod, SearchDefs, SearchCols attributes)
(Focused, FocusedCol, FocusedPos attributes)
(PrintPageBreaks, PrintRows, PrintExpanded, PrintFiltered)
<Filter> (list of filters)
<I ... row and cell attributes ... /> (id attribute, cell values and cell Filter attributes)
... More tags <I> if more filters are in grid ...
</Filter>

<LeftCols>
<C Name='column name' Width='...' Visible='0/1' />
... more tags <C> for other columns ...
</LeftCols>
<Cols>
<C Name='column name' Width='...' Visible='0/1' />
... more tags <C> for other columns ...
</Cols>
<RightCols>
<C Name='column name' Width='...' Visible='0/1' />
... more tags <C> for other columns ...
</RightCols>

</Grid>