Forum


Replies: 1   Views: 50
Create table with data chunks

Posted by Teresa Alvarez  · 17-07-2025 - 21:45

Hi

I'm creating a report containing a table of approximately 6,400 records. Following the examples in the documentation, creating a report with approximately 840 pages takes a long time (19 minutes). Upon analysis, I find that the bottleneck is creating the $valuesTable array with all the values (6,400 rows) to add to the $docx->addTable() statement.

One solution is to use chunks, but if I want to create a single table, I have to use variables by reference (&$valuesTable), and this makes no difference. Another solution is to create a table for each chunk. Although this doesn't cause me any problems, each $docx->addTable() statement leaves a blank line, which is something I want to avoid.

Is there a solution to create the table with $docx->addTable() without having to create the $valuesTable array, or can $docx->addTable() process the array in chunks or remove the line after table?

I'm using phpdocx 15 and a little context: in my local environment I use 1GB of memory, and if I create a table for each chunk, the report is generated in approximately 3 minutes, the same 845 pages.

Posted by admin  · 18-07-2025 - 05:40

Hello,

In this case, the easiest approach is to create multiple tables rather than a single huge one.

Please check the information available on https://www.phpdocx.com/documentation/snippets/tables-without-spacing-between-them, which details how to reduce the space between tables. MS Word requires adding at least one paragraph between tables, but you can set a minimum paragraph size between them.

Also note that if you are using WordFragments to add cell contents, the time needed is longer than adding strings.

Regards.