Print Grid to PDF

I have a DHTMLX Grid that I want to print as PDF. So far I have followed the instructions on [url][/url] and [url]Start DHTMLX Docs and I was able to successfully generate my Grid as PDF document.
However, I have failed to find solution to the following issues and will appreciate any help:
1- I want the PDF page to appear in landscape mode so that I could fit as much data as I could from my grid
2- I want the PDF to respect the column colors that I have on my Grid. The data of the Grid is appearing on the PDF document but the color of column that I have customized in my grid is not appearing on the PDF document. I have passed the extra parameter ‘color’ to the ‘toPDF’ method in the hope that it will generate the Grid with my column colors like the following example lessonPlanningGrid.toPDF('http://localhost:8080/XML2PDF/generate', 'color'); but that still did not display the colors of each column that i have on my Grid on the generated PDF document. any idea why?

many thanks in advance :wink:

To change page orientation open file gridPdfGenerator.php and modify code like here:

	private $orientation = 'L';
	private $columns = Array();

Try to use ‘full_color’ instead of ‘color’ to apply custom colors.

Thanks radyno for your reply.

I forgot to mention that I am using the Java version of the PDFGenerator.

Your suggestion around the Landscape parameter did give me a clue and i managed to change the Java class to print the page in a Landscape mode and worked fine.

However, the second issue is still outstanding. Sending the parameter ‘full_color’ instead of ‘color’ made things even worst. the page appeared black and white with no colors at all. when I send the parameter ‘color’ i do get the default sky_blu colors. but my grid columns have custom colors and these are not showing at all. I am now wondering if the Print PDF functionality respect grid custom colors instead of the default skin colors…

Any other suggestions?


I do not believe there is a ‘full_color’ option available yet for the Java version, so the “best” you can choose is ‘color’; you can verify this for yourself by tracing their code. I too was having issues generating a pdf with all of my grid information AND custom colors for my cells; however, what I found with both the Java and the hosted PDF services was the following:

  • The hosted service supported my custom colors; however, due to the fact that it is hosted on the dhtmlx site, we are not able to specify paper options and orientation. This becomes an issue when you have a large number of columns in that your data will be “scrunched” together with a lot of cell values being “…” as opposed to their appropriate values.

  • The most up-to-date Java service/.WAR allows you to customize font size, paper options and orientation (within allowing you to accommodate grids with relatively large number of cells; however, the footers still appear below the headers and custom colors fail to appear in the exported file. I was using a form that allows a user to select paper options, to specify custom sizes, and to choose orientation. Inside of, you can see where you can set default orientation, so I believe you should be able to abstract out paper size and orientation, but I am somewhat confused as to how to incorporate both those attributes into the toPDF() function’s parameters within dhtmlxgrid_export.js. This perhaps would be a useful feature for a future update.

  • My understanding is that there is currently a trade-off between having custom coloring and footers appearing correctly with the Hosted version, and having the complete dataset (accommodation of large num of cols by configuring to print in LANDSCAPE) with the Java version with your goal being to generate the “best” possible exported PDF.

  • I was using dhtmlxGrid v2.6 pro Build 110318 if that helps.

Best of luck.

Thanks PGoldman for your input. and i must agree with you that although the export to PDF functionality is useful and helpful, however, I feel that it is not complete and does not support scenarios that are slightly diverting from the standard way of using the DHTMLX Grid component (things like Orientation, Custom Colors, footers…etc). I hope that the DHTMLX guys are reading this post and will provide a fix for all the issues above and I am sure there are many more listen in this forum.

Having said that, I found that the only way to control the orientation of the page is by modifying the toPDF method inside the dhtmlxgrid_export.js file to pass extra parameter called ‘orientation’ to the function that will then get passed to the PDFGenerator servlet.

Below is my modification to the toPDF method

	mode = mode || "color";	
	var full_color = mode == "full_color";
	var grid = this;
	grid._asCDATA = true;
	if (typeof(target) === 'undefined') = " target=\"_blank\"";
	else = target;
	eXcell_ch.prototype.getContent = function(){
		return this.getValue();
	eXcell_ra.prototype.getContent = function(){
		return this.getValue();
	function xml_top(profile) {
		var spans = [];
		for (var i=1; i<grid.hdr.rows.length; i++){
			for (var j=0; j<grid._cCount; j++){
				var cell = grid.hdr.rows[i].childNodes[j];
				if (!spans[i][j])
				if (cell)
					spans[i][cell._cellIndexS]=[cell.colSpan, cell.rowSpan];
	    var xml = "<rows profile='"+profile+"'";
	       if (header)
	          xml+=" header='"+header+"'";
	       if (footer)
	          xml+=" footer='"+footer+"'";
		   if (orientation)
			  xml+=" orientation='"+orientation+"'";
	    for (var i=2; i < grid.hdr.rows.length; i++) {
                var empty_cols = 0;
                var row = grid.hdr.rows[i];
    	        var cxml="";
	    	for (var j=0; j < grid._cCount; j++) {
	    		if ((grid._srClmn && !grid._srClmn[j]) || (grid._hrrar[j])) {
	    		var s = spans[i][j];
	    		var rspan =  (( s[0] && s[0] > 1 ) ? ' colspan="'+s[0]+'" ' : "");
                        if (s[1] && s[1] > 1){
                             rspan+=' rowspan="'+s[1]+'" ';
                             empty_cols = -1;
                var val = "";
                for (var k=0; k<row.cells.length; k++){
					if (row.cells[k]._cellIndexS==j) {
						if (row.cells[k].getElementsByTagName("SELECT").length)
							val = _isIE?row.cells[k].innerText:row.cells[k].textContent;
							val=val.replace(/[ \n\r\t\xA0]+/," ");
	    		if (!val || val==" ") empty_cols++;
	    	if (empty_cols != grid._cCount)
	    return xml;

you can then call the method in the following way:

myGrid.toPDF('http://localhost:8080/XML2PDF/generate', 'color','landscape');

As you could see, i am passing the extra parameter called ‘orientation’ to the toPDF method and then adding extra if statement to check for existing of this parameter then it will create an XML attribute which will get passed to the class which already checks for this attribute and decide if the page should be in landscape or portrait mode.
no extra modification is required to any other java class. The outcome should be a page rendered in a landscape mode.

As for the other, more important, issue which is getting my grid custom colors displayed, i am still not sure how to modify the code or if I should way until DHTMLX people release a proper version of the export to PDF functionality.

Let’s hope for the best :wink:

Here is the last dev version of grid2pdf for Java. Try to update it.
Full_color mode is available now. … id-pdf.war

excellent. The new version works like a dream :smiley: great job.