Forum


Replies: 2   Views: 393
Using wordfragment and addformelement with addtable
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by tjchen  · 07-11-2023 - 09:52

Dear phpdocx support team,

I am currently using WordFragment and addFormElement with addTable to generate a Word document with a table. The table consists of 250 rows, and each row contains 5 cells. In the first two cells, I add text, and in the remaining three, I add checkboxes by addFormElement. After this, I add another row with two cells, the first one being empty and the second one containing a text field by addFormElement.

Here is a simplified version of my code:

$docx = new CreateDocx();
for ($i = 1; $i <= 250; $i++) {
    for ($j = 1; $j <= 5; $j++) {
        $item = new WordFragment($docx);
        if ($j >= 3) {
            $item->addFormElement('checkbox');
        } else {
            $item->addText($i);
        }

        $cell = ['value' => $item, 'border' => 'nil', 'cellMargin' => ['left' => 0, 'right' => 0]];
        $row[] = $cell;
    }
    $table[] = $row;
    unset($row);

    for ($j = 1; $j <= 2; $j++) {
        $itemRemark = new CRWordFragment($docx);
        $cell = [];
        if ($j == 2) {
            $itemRemark->addFormElement('textfield');
        } else {
            $itemRemark->addText('');
        }
        $cell = ['value' => $itemRemark, 'border' => 'nil', 'cellMargin' => ['left' => 50, 'right' => 50]];
        $rowRemark[] = $cell;
    }
    $table[] = $rowRemark;
    unset($rowRemark);
}

$options = ['columnWidths' => [950, 8300, 250, 250, 250], 'cantSplitRows' => true, 'tableLayout' => 'fixed', 'tableAlign' => 'center', 'tableWidth' => ['type' => 'twips', 'value' => 10000]];
$docx->addTable($table, $options);
$docx->createDocx("text.docx");

The issue I am facing is that this operation takes around 10 seconds to complete and the output file is only 70KB, which is a bit too long for my use case. I was wondering if there is any way to optimize this process or if there are any best practices that I could follow to improve the performance.

Thank you for your help.

Posted by admin  · 07-11-2023 - 11:15

Hello,

Please note that the time needed to generate a DOCX is not related to the file size but to the contents.

The addTable method is the line that takes more time in your script. Your code is generating a table with 1250 complex contents (WordFragments) that generate 70.000 internal XML lines that must be filled, parsed and cleaned. We have run your code using a test server and it takes 4s to process the whole table and generate the DOCX.
There's no way to improve the taken time to run the script but adding smaller tables instead of a huge table. Instead of one big table, you could generate five smaller tables (each table with 50 rows) to improve the performace.

Regards.

Posted by tjchen  · 08-11-2023 - 06:18

Dividing the large table into many small tables greatly speeds up the process, thank you for your help.