Can someone help me with export to pdf

Hello all,

I’ve got the normal grid and I just want to implement the print to pdf function. However when I use the code below I get an alert error without any text appearing on the page with the grid. And this error on the page when I try to print anyway:
/dhtmlx/codebase/grid-pdf-php/generate.php at 13 : Undefined index: grid_xml

Here is my code for the connector:

<?php
	require_once("config.php");
	$res=mysql_connect($mysql_server,$mysql_user,$mysql_pass);
	mysql_select_db($mysql_db);

	require("../../dhtmlx/codebase/connector/grid_connector.php");
	require("../../dhtmlx/codebase/convert.php");
	
	$convert = new ConvertService("../../dhtmlx/codebase/grid-pdf-php/generate.php");
	$convert->pdf();
	
$gridConn = new GridConnector($res);
$gridConn->set_config(new GridConfiguration());
$sql = "SELECT * FROM customer_billing";
$gridConn->render_sql($sql,"id", "id, company_name, address, city, province, postal_code, phone_number, fax_number, web_address, longitude, latitude");

?>

And here is the code for the actual grid:

	<script src="../../dhtmlx/codebase/dhtmlx.js" type="text/javascript"></script>
    <!-- dhtmlx.css contains styles definitions for all included components -->
    <link rel="STYLESHEET" type="text/css" href="../../dhtmlx/codebase/dhtmlx.css">
    <script src="../../dhtmlx/codebase/connector/connector.js" type="text/javascript"></script>
    <script src="../../dhtmlx/codebase/ext/dhtmlxgrid_export.js" type="text/javascript"></script>
 
    <script type="text/javascript">
var layout,toolbar,custBillGrid;
dhtmlx.image_path = "../../dhtmlx/codebase/imgs/";
dhtmlxEvent(window,"load",function(){
    //layout
    layout = new dhtmlXLayoutObject("layout","1C");
    layout.cells("a").setText("Customer");
	layout.cells("a").setWidth(400);
	

toolbar = layout.attachToolbar();
toolbar.setIconsPath("../../icon/");
toolbar.loadXML("../../dhtmlx/codebase/xml/customer_billing_master.xml");
		
custBillGrid = layout.cells("a").attachGrid();
    //contactGrid.setImagePath("./codebase/imgs/");
    //contactGrid.setSkin("dhx_skyblue");
custBillGrid.setHeader("Account Number, Company Name, First Name, Last Name, Address, City, Province, Postal, Phone Number, Latitute, Longitude");
custBillGrid.attachHeader("#text_filter,#text_filter,#text_filter,#text_filter,#text_filter,#text_filter,#text_filter,#text_filter,#text_filter,#text_filter,#text_filter");
custBillGrid.setInitWidths("130,130,130,130,130,130,130,130,130,130,130");
custBillGrid.setColAlign("left,left,left,left,left,left,left,left,left,left,left");
custBillGrid.setColTypes("ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro");
custBillGrid.setColSorting("str,str,str,str,str,str,str,str,str,str,str");
custBillGrid.enableSmartRendering(true);
custBillGrid.init();
custBillGrid.load("customer_billing_master_connector.php");		

//toolbar
toolbar.addText("exportPDF", 1,'<a href="../../dhtmlx/codebase/grid-pdf-php/generate.php">Print to PDF</a>');
toolbar.addText("newCust", 2,'<a href="customer_billing_add.php"><img src="../../icon/savesmall16.png" width="18" height="18"/></a>');
toolbar.attachEvent("onclick",function(id){
    if(id=="delCust"){
       var rowId = custBillGrid.getSelectedRowId();
       if(rowId!=null){
          var selectedIndex = custBillGrid.getRowIndex(rowId)
          custBillGrid.deleteRow(rowId);
          if(selectedIndex!=(custBillGrid.getRowsNum()-1)){
             custBillGrid.selectRow(selectedIndex+1,true);
          }
          else{
             custBillGrid.selectRow(selectedIndex-1,true)
          }
        }
    }
});

var dpg = new dataProcessor("customer_billing_master_connector.php");
dpg.init(custBillGrid);

})
    </script>
<div id="layout" style="width:500px; height:500px; position:relative;"></div>
</div>

Hope someone can help me here. Thanks in advance

Hi,
try to use the follow export logic:

<?php
require("./config.php");
require("../../dhtmlx/codebase/connector/grid_connector.php");
require_once '../../dhtmlx/codebase/grid-pdf-php/gridPdfGenerator.php';
require_once '../../dhtmlx/codebase/grid-pdf-php/tcpdf/tcpdf.php';
require_once '../../dhtmlx/codebase/grid-pdf-php/gridPdfWrapper.php';

$res = mysql_connect($mysql_server, $mysql_user, $mysql_pass);
mysql_select_db($mysql_db, $res);

$grid = new GridConnector($res);
$grid->set_config(new GridConfiguration());

$grid->event->attach("beforeOutput", "export");
$fields = "item_nm,item_cd";
$grid->render_table("grid50", "item_id", $fields);

function export($grid, $out) {
	$xml = simplexml_load_string($out->__toString());
	$pdf = new gridPdfGenerator();
	$pdf->setOutputName('mygrid.pdf');
	$pdf->printGrid($xml);

	die();
}

?>

Sorry, I tried your code, still does the same thing.

Code below just has my info in it, did I miss something? And do I need to reference the xml somehow in the grid js?

[code]<?php
require (“config.php”);
require ("…/…/dhtmlx/codebase/connector/grid_connector.php");
require_once ("…/…/dhtmlx/codebase/grid-pdf-php/gridPdfGenerator.php");
require_once ("…/…/dhtmlx/codebase/grid-pdf-php/tcpdf/tcpdf.php");
require_once ("…/…/dhtmlx/codebase/grid-pdf-php/gridPdfWrapper.php");

$res=mysql_connect($mysql_server,$mysql_user,$mysql_pass);
mysql_select_db($mysql_db);

$grid = new GridConnector($res);
$grid->set_config(new GridConfiguration());

$grid->event->attach(“beforeOutput”, “export”);
$fields = “id, company_name, address, city, province, postal_code, phone_number, Fax_number, web_address, longitude, latitude”;
$grid->render_table(“customer_billing”, “id”, $fields);

function export($grid, $out) {
$xml = simplexml_load_string($out->_toString());
$pdf = new gridPdfGenerator();
$pdf->setOutputName(‘mygrid.pdf’);
$pdf->printGrid($xml);

die();
}

?>
[/code]

Hi, I understood that you try to implement export without client side - directly from server side.
Script which I provided does exactly the same.
Do you still have the follow error?

/dhtmlx/codebase/grid-pdf-php/generate.php at 13 : Undefined index: grid_xml

To call export you have to open php file with provided code.
If you copied code from here it should generate such error.

Hi Radyno,

Thanks for the reply.

Yes I still get the same error.

Which provided code are you speaking of? Could you provide a working example?

Thanks

Hi,
sample is in attachment.
You have to unpack archive to your web-server, configure database connection (file config.php) and connector (file export.php) and open it in browser.
direct_export.zip (1.02 MB)

Perfect, thanks for the example, and thank you for helping me with this.

Got it working now.

Hey thanks for your help,

One last question though, when the data is exported to PDF, it exports the entire table. How can I export filtered data. Say I only want to export one customer data, couldn’t find an answer in the forum.

thanks

Hi,
you may configure using connector a set of data which will be rendered from database.
For example you may use special request to select not all data. For example:

$fields = "id, company_name, address, city, province, postal_code, phone_number, Fax_number, web_address, longitude, latitude";
$grid->render_sql("SELECT * FROM customer_billing WHERE company_name='some customer name'", "id", $fields);

Also connector has API to filter data using FilterInterface. Here is link which describe it:
docs.dhtmlx.com/doku.php?id=dhtm … el_filters

So you may use one of this ways.

Hey,

Sorry I should have reworded that, I meant more along the lines of the demo on this page:
dhtmlx.com/docs/products/dht … x.shtml?mn

The client can filter & sort the data before it is rendered as a pdf or excel file. How would one go about doing this?

Hi,
it’s a classic grid export usage, i.e.:
data is loaded into grid. When user press “Export” button the follow code is called:

mygrid.toPDF('http://url_to_grid-pdf-php/generate.php');

This method takes data from grid and sends it to export script.

Version which I provided earlier takes data from database and send it directly to export script (without loading it into dhtmlxGrid component).

Here is export sample.
export_sample.zip (1.04 MB)

Thank you so much for all your help this week.

It had been a long time since I’d worked with any js/php coding before this project, you have been very kind and helpful, thank you.

Owen