Similar to export to Excel export to PDF is another useful feature of jqGrid.
When summary rows (footer formula) are used, the grid creates a formula and the summary fields are displayed at end of the file.

When this method is used within jqGrid and jqGridEdit classes they should be called separatley in order to perform the export.
When used in jqGridRender class everthing is done automatically when the export to pdf is enabled.

Also when used separatley and you want to display diffrent header, width and hide some fields a colmodel array should be configured and passed as parameter to the method. If none is set in this case only column names are used

The array has the following structure and properties


Array(
[0]=>Array("label"=>"Some label", "width"=>100, "hidden"=>true, "name"=>"colname"),
[1]=>Array(...),
...
);
Where

  • label is the header displayed for this field
  • width is the width in pixels
  • hidden (boolean) if set does not export this column
  • name is the name from column model

If set the length of this array should be equal to the number of fields in the SQL command used.


This method uses a TCP PDF library www.tcpdf.org. The library is a part from the download package and usually you will need to do nothing (in sence to include some files) in order to perform export to PDF.
The PDF class is placed in the pdf subdir of the package. If the pdf subdirectory is not suitable for you needs, you can change it place. In this case the "path_to_pdf_class" should be changed according to the new place.
This can be done using the setPdfOptions method (see below).

By default the download package contain only one UTF font - freeserif (except the standart non UTF).
This font should be set using setPdfOptions(..) method. The names connected with this are font_name_main and font_name_data plus font_monospaced
A dejavupack can be downloaded from here: DejavuPack
To have more fonts please download the font pack from TCPPDF site and set the appropriate name in the setPdfOptions as described above


Related Methods

exportToPdf
setPdfOptions
renderGrid

Related variable(s)

PDF
gSQLMaxRows
ExportCommand


As can be seen we have one important private array variable $PDF, where all the export options are stored.
Direct the values of this array can not be changed. This ia done with the method stPdfOptions whch paramater is array.

Example:
We can change the path to pdf class this way.

$grid = new jqGridRender($conn) // or $grid = new jqGridQuery($conn); ... $grid->setPdfOptions(array("path_to_pdf_class"=>"mynewdir/tcpdf.php"));

Below we list all the possible options and the related values to it.

$PDF = array(
"page_orientation" => "P",
"unit"=>"mm",
"page_format"=>"A4",
"creator"=>"jqGrid",
"author"=>"jqGrid",
"title"=>"jqGrid PDF",
"subject"=>"Subject",
"keywords"=>"table, grid",
"margin_left"=>15,
"margin_top"=>7,
"margin_right"=>15,
"margin_bottom"=>25,
"margin_header"=>5,
"margin_footer"=>10,
"font_name_main"=>"helvetica",
"font_size_main"=>10,
"header_logo"=>"",
"header_logo_width"=>0,
"header_title"=>"",
"header_string"=>"",
"header"=>false,
"footer"=>true,
"font_monospaced"=>"courier",
"font_name_data"=>"helvetica",
"font_size_data"=>8,
"margin_footer"=>10,
"image_scale_ratio"=>1.25,
"grid_head_color"=>"#dfeffc",
"grid_head_text_color"=>"#2e6e9e",
"grid_draw_color"=>"#5c9ccc",
"grid_header_height"=>6,
"grid_row_color"=>"#ffffff",
"grid_row_text_color"=>"#000000",
"grid_row_height"=>5,
"grid_alternate_rows"=>false,
"path_to_pdf_class"=>"tcpdf/tcpdf.php"
);

Option Description Values Default
page_orientation Determines the orientation of the page. Can be Portrait or Landscape P - Portrait; L - Landscape P (Portrait)
unit Set the measure of the export. Note that if you change this value all the related options which contain measures (except pixels) should be changed in the appropriate way mm - Millimeters, cm- Centimeters, in - Inchs, pt-Point mm (Millimetters)
page_format Set various format of the page. A, B, C, E, G (from 0 - 12), USLETTER, LETTER and a lot of otther formats A4
creator Set the creator of the pdf document Any string jqGrid
author Set the author of the document Any string jqGrid
title Set the title of the document. This is the title when see the properties of the document. Any String jqGrid PDF
subject Set the subject of the document Any string Subject
keywords Set the kyewords for the document Any string table, grid
margin_left set the left margin of the page in measurment defined in unit option Number 15
margin_top set the top margin of the page in measurment defined in unit option Number 7
margin_right set the right margin of the page in measurment defined in unit option Number 15
margin_bottom set the bottom margin of the page in measurment defined in unit option Number 25
margin_header set the header margin of the page in measurment defined in unit option Number 25
margin_footer set the footer margin of the page in measurment defined in unit option Number 25
font_name_main Defines the main font. This font is used in the headers. Any valid font name (as set in the font subdirectory) helvetica
font_size_main Defines the size of the font_name_main font. This font is used in the headers. Number 10
header_logo If set defines the image which will be placed at the top left corner of the page. The image should be placed in the image directory of the pdf directory Name of the image Empty string
header_logo_width Defines the width od the header image Number 0
header_title Defines the the title which will be printed at top of the page near th image logo if defined Any string Empty string
header Enables or disables the printing of the heder related data. Important: By default this option is disabled. If you want to print some header data it is a good idea to increase the margin_top option true or false false
footer Enables or disables the printing of the footer related data. true or false true
font_monospaced Defines the default monospaced font Any valid font name courier
font_name_data Defines the font for the printing of the table data Any valid font name helvetica
font_size_data Defines the size default monospaced font Number 8
margin_footer Defines the margin of the footer in measurment defined in unit option Number 10
grid_head_color Defines the table header color background Color defined as html value #dfeffc
grid_head_text_color Defines the table header text color Color defined as html value #2e6e9e
grid_draw_color Defines the table border color Color defined as html value #5c9ccc
grid_header_height Defines the height of the table header data in measurmen defined in unit Color defined as html value 6
grid_row_color Defines the table row color background Color defined as html value #ffffff
grid_row_text_color Defines the table row text color Color defined as html value #000000
grid_row_height Defines the height of the table row data in measurmen defined in unit Color defined as html value 5
grid_alternate_rows Enable or disable zebra striping in the export true or false false
path_to_pdf_class Set the path and filename to the tcpdf class. String tcpdf/tcpdf.php

Example:
This example will be the same as the Export to Excel Example, but this time we will export the data in PDF file
Let suppose that when we export to PDF we want to show additionally the ShipAddress and ShipCity and perform a summary on the field Freight. We will use again our example.
For this purpose we will first create a custom button in the navigator and use the build in method for this purpose excelExport with tag set to pdf. The method is avialble since version 3.6.3 of jqGrid Java Script lib. When used the method passes additinally a variable oper={tag} to identify that we want a export to a different formats.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP jqGrid Class Example</title> <link rel="stylesheet" type="text/css" media="screen" href="themes/redmond/jquery-ui-1.7.1.custom.css" /> <link rel="stylesheet" type="text/css" media="screen" href="themes/ui.jqgrid.css" /> <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script> <script src="js/i18n/grid.locale-en.js" type="text/javascript"></script> <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script> <script type="text/javascript"> jQuery(document).ready(function(){ .... // Craeate the grid manually jQuery("#grid").jqGrid({ "colModel":[ {"name":"OrderID","index":"OrderID","label":"ID","width":60, "key":true}, {"name":"OrderDate","index":"OrderDate"}, {"name":"CustomerID","index":"CustomerID"}, {"name":"Freight","index":"Freight"}, {"name":"ShipName","index":"ShipName"} ], "url":"querygrid.php", "datatype":"json", "jsonReader":{repeatitems:false}, "pager":"#pager" }); // Set navigator with search enabled. jQuery("#grid").jqGrid('navGrid','#pager',{add:false,edit:false,del:false}); // add custom button to export the data to excel jQuery("#grid").jqGrid('navButtonAdd','#pager',{ caption:"", onClickButton : function () { jQuery("#grid").jqGrid('excelExport',{tag:"pdf","url":"querygrid.php"}); } }); ...... }); </script> </head> <body> ...... <table id="grid"></table> <div id="pager"></div> ....... </body> </html>

In PHP code we will use ExportCommand to perform the export.

<?php require_once 'jq-config.php'; // include the jqGrid Class require_once "php/jqGrid.php"; // include the PDO driver class require_once "php/jqGridPdo.php"; // Connection to the server $conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD); // Create the jqGrid instance $grid = new jqGrid($conn); // Write the SQL Query $grid->SelectCommand = 'SELECT OrderID, OrderDate, CustomerID, Freight, ShipName FROM orders'; // we want to export additinal data when excel $grid->ExportCommand = 'SELECT OrderID, OrderDate, CustomerID, Freight, ShipName, ShipAddress, ShipCity FROM orders'; $grid->dataType = "json"; // now we should check whenever a export is lunched $export = $_POST['oper']; if($export == 'pdf') // let set summary field $grid->exportToPdf(array('Freight'=>'Freight')); else $grid->queryGrid(); ?>