DHTMLX Docs & Samples Explorer

Export to PDF

Installation

The latest Export packages for installation can be found here:

To export data from dhtmlxGrid into a pdf document:

  1. Download and unpack the appropriate Export package to the root level of your web server to folder codebase (you don't need to install anything, just unpack).
  2. Call method toPDF(). The method takes as a parameter the path to the generate.php file resided in the Export package.

For example, if you add a button by clicking on which dhtmlxGrid will start exporting, then your code can look like this:

// DHTMLX Suite is used
<link rel="STYLESHEET" type="text/css" href="codebase/dhtmlx.css">
<script src="codebase/dhtmlx.js"></script>
<script src="codebase/dhtmlxgrid_export.js"></script>
 
<input type="button" value="Get as PDF" 
onclick="mygrid.toPDF('codebase/grid-pdf-php/generate.php');">
<div id="gridbox" style="width:399px;height:270px;"></div>
 
<script>
    mygrid = new dhtmlXGridObject('gridbox');
    ...
</script>

Necessary includes

On the grid's page you need to include one more extension

<script src="codebase/dhtmlxgrid_export.js"></script>

Printing triggering

To export grid's data to a PDF you need to add the button on the page, which will call the toPDF method. The parameter of toPDF method is the url of the script, which has been installed previously (./server/generate.php):

<input type="button" name="save" value="save" 
       onclick="grid.toPDF('path/to/folder/generate.php')">

Configuring service

Client side options

The only thing, which can be controlled on client side is color-map of image. You can choose between

  1. “color” - full color printing, used by default
  2. “gray” - print in shades of black and white
  3. “bw” - uses only black and white colors
  4. “custom” - can be used to enabled custom color map ( will require php coding, check below )
  grid.toPDF('path/to/folder/generate.php','gray');

Server side options

generate.php contains a code, similar to the next

$pdf = new gridPdfGenerator();
$pdf->printGrid(&$xml);

You can apply some custom configuration options , before executing printGrid

Size of elements

//page offsets
$pdf->minOffsetTop = 10;
$pdf->minOffsetBottom = 10;
$pdf->minOffsetLeft = 10;
$pdf->minOffsetRight = 10;
//element sizes
$pdf->headerHeight = 7;
$pdf->rowHeight = 5;
$pdf->minColumnWidth = 13;
$pdf->pageNumberHeight = 10;
//font settings
$pdf->fontSize = 8;
//image settings
$pdf->dpi = 96;
$pdf->footerImgHeight = 50;
$pdf->headerImgHeight = 50;

Color of elements ( “custom” colormap)

//main bg color of grid
$pdf->bgColor = 'D1E5FE';
//color of border lines
$pdf->lineColor = 'A4BED4';
//color of text in grid's header
$pdf->headerTextColor = '000000';
//color of alter-css - even row
$pdf->scaleOneColor = 'FFFFFF';
//color of alter-css - odd row
$pdf->scaleTwoColor = 'E3EFFF';
//color of text in the data part
$pdf->gridTextColor = '000000';
//color of the text on a page ( page number, etc. )
$pdf->pageTextColor = '000000';

Other setttings

//control processing of html tags
$pdf->strip_tags = false;

Header and Footer

It possible to define custom header and footer for each page.

  • create images with names as “header.png” and “footer.png”
  • copy those images to the same folder where generate.php resides
  • on client side , change code call as
    grid.toPDF(url, "color", true, true);

As result you will have content of “header.png” and “footer.png” as header and footer on all pages in the generated pdf file.

Error reporting

If output of PDF file is failed, there must be file named as “error_report_xxxx.xml”, please send this file with any bug-reports.

If output doesn't fail, but still has some problems, you can edit generate.php and change

    $debug = false;

as

    $debug = true;

As result, there will be a new file saved, with name as “debug_xxxxx.xml” - please send it with related error report.