How to change formatting of export file in dhtmlxgrid? - php

I am using dhtmlxgrid for displaying data for my reports. I have embedded two export buttons i.e. pdf and excel. But there are some number fields which are left aligned in exported file. I want to change the alignment of the number fields to the right as well as I want to set the width of the columns(if possible).

You can change column types to number based ones ( edn or ron ), it will change align of data in those columns in case of export to Excel

I had to do a lot of debugging and a little customization to get column alignment and widths to work how I wanted them do. I have this working in production, but not sure if it will fit your case. This is with dhtmlxgrid v4.1 and I'm using the Java back-end Excel writer classes.
On the front end
You should debug through dhtmlxgrid_export.js in this function:
dhtmlXGridObject.prototype._serialiseExportConfig
Make sure your alignments are correct in this array:
this.cellAlign[i]
If they aren't, you probably need to explicitly set them when you create your grid (but before you load it):
grid.setColAlign(myColAligns.join('|'));
For widths, the same applies:
grid.setInitWidths(myColWidths.join('|'));
On the back-end
In ExcelWriter.java, debug through
headerPrint(ExcelXmlParser parser)
and make sure the your alignments are coming through here:
String al = cells[j].getAlign();
The col widths are also set in this same function. I did a little customization because it didn't seem like the sizes were comparable:
sheet.setColumnView(j, (int)(widths[j]/6.53)); // I replaced "*scale/sumWidth" with /6.53, which is a strange num, but it works! ;

Related

How to implement calculating table cell width like they actually do in html - manually?

I am stuck with this issue. I am using spipu/html2pdf for generating pdf files. So I have my total width of the page known. But the pdf I am generating will have dynamic tables (meaning I am not aware of how many column / what the size of each column would be while generating) so I want to implement the standard html like behaviour.
ie.
If I put width of the table 100% the table should take 100% of the available space and the table cells should be distributed sensibly. How can I do that?
I have tried flex but spipu/html2pdf does'nt have support for it (and it make sense considering the static nature of pdf).
Can anybody help me with a good way to make this possible?
literally I want to implement how the css display:table; and display:table-cell; is implementd - but manually. because spipu/html2pdf requires you to specify the width in td not on tr or table
Update
A little background - The thing is I am presenting a rich text box to the user for adding extra details. which result in half of the pdf's contents known, but the other half completly unknown. As for the security, I am only allowing a subset of tags like h1 - h6, p, ul, li and table and some of their respective attributes. here is where the issue. The rich text box behave exactly like i want (because its html is rendered in browser).
The only issue is when the user specify the width on the table instead of table-cell and so I want to implement the table's width calculating mechanism so that the report have the same styling. If the user provides a fixed width to table-cell. This is perfectly fine. But if the user provides a 100% width (or any % value) on the table the browser renders this correctly. But the html2pdf just calculate the width from the column.
How I am doing things now:
There is no heavy processing involved here anywhere just some for compatibility issues. The html is sanitized before storing it in the database.
while generating the report,
I just put the contents like
$data = preg_replace('some unkown tag to html2pdf', 'replaced with compatible tags and proper stying', $users_data);
$html2Pdf->writeHtml($data)
If I want the tables data I can do
$data = preg_match('/.*?<table(.*?)>(.*?)<\/table>.*?/', $match)
// so $match have the tables attributes and elments inside
What do I do from here?

Is it possible to set the width of one cell alone without affecting other cells in phpExcel?

Is it possible to set the width of one cell alone without affecting other cells?
I am generating a pdf file using phpexcel.
I need to set the width of cell A1 to 40.
Tried this
$objPHPExcel->getActiveSheet()
->getColumnDimension('A')
->setWidth(40);
It does affect the entire column .
No you can't, in exactly the same way that you can't in MS Excel, Open/Libre Office Calc, or in Gnumeric.... PHPExcel tries to replicate the functionality of these spreadsheet packages, not the tables in Word.
You can, perhaps, merge cells to achieve what you want
it seems you cannot change individual cell see https://phpexcel.codeplex.com/discussions/448594
not sure if it fits your need , anyway you can remove the right border from cell so that two adiacent cells looks like one, as shown in this code

Converting from div to table or parsing PDF in PHP

I'm developing a WebApp in which I take an invoice converted from PDF to HTML, then parse the invoice lines.
I have a div in my main window which displays the contents.
But when I display the contents from the invoice in that div, all the contents appear overlapped.
In the converted invoice there is no table, only divs with absolute positioning. I can't make it any other way at least with this aproach, because that's the way the converter works.
So, as a solution I'm converting from "div to table", trying to decide when there is a change of row or not, based on the top parameter from the corresponding div.
However besides the invoice data, I also have the invoice header. I'm having difficulties to decide if the table is the same or not.
But so far, I think the solution passes through making 3 tables, one for the company logo, one for the header, and one for the data.
But I need all these tables to appear in the correct positions and with the correct sizes.
At the moment, I'm not allowed to paste invoice examples, and as I'm stuck in an early stage (close to the algorithm stage). I don't think any examples of my code and of the invoices could help anyone to understand the situation better.
But I promise to update this with examples soon.
As an alternative solution I could parse the PDF myself, but I haven't found a way to do it so far.
I'm using PHP to make the WebApp and verypdf pdf2html to make the conversion.
I know with that little information, is hard to get help.
Any ideas are welcome.
How about trying to cure the overlapping itself. For example you could strip all the styling information from the DIVs after the PDF is parsed into DIVs. Then you can apply your own styles.
It might be useful to know if all the invoices are in the same format/arrangement, or not.

How to set auto-line break PhpExcel?

I'm using PHPExcel to generate some excel export. Excel has an autoline break feature. How can i activate this ?
My customer use only Microsoft Excel, so it don't have to support others programs.
Any ideas ?
I may use something like this : how to make New lines in a cell using phpexcel But that's more a kind of hack, than a real solution.
UPDATE :
See, once columns are merged, wrap text fail. I tryied to add wrap text upon every column merged, but it don't affect result.
UPDATE 2 :
The cells are merged, then i add wrap text, then i add text.
Set the column to a fixed width; setWrapText(true) for the cells, and see what happens.
Look at the second sheet created in /Tests/05featuredemo.php for an example.
You only need to use \n if you want to control exactly where a line wraps

dompdf create pdf with four columns?

I need to create a pdf with four equal length columns across the page using dompdf.
I have tried to do this however I can't work out how to correctly set the widths of the columns. It always ends up cutting the last column off.
How can I render a table with a fixed width across the entire PDF page with four equally wide columns?
Dompdf has issues with tables, page breaks and image display..you can see on their own issues website..Here
I use fpdf, and its really simple to use and consistence in design Check here for simplicity in table display in fpdf
Ok it looks like all I need to do is set the table width to auto or 100% and let the columns scale across the page automatically without setting their widths.

Categories