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.