Teretto
Teretto

Reputation: 655

Add 2-3 lines of text to table cell with different formating

I try to make table using phpWord. But I have one problem - one column of my table in each cell must have text with different formatting:

example

First, I try to make difficult row with 3 rows inside

  _   _   _   _
|   | _ |   |   | - row1 of row
|   | _ |   |   | - row2 of row
| _ | _ | _ | _ | - row3 of row

But I need to disallow PHPWord to break table rows between pages.

$table->addRow(null, array('tblHeader' => true, 'cantSplit' => true));

My row consist of 3 rows, so it is does not work. So only one way - use one cell with all text inside (look at the picture).

How can I add to cell text with different formatting?

public function test2()
    {
        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        $section = $phpWord->addSection();
        $table = $section->addTable();
        for ($r = 1; $r <= 80; $r++) {
            $table->addRow(null, array('tblHeader' => false, 'cantSplit' => true));
            for ($c = 1; $c <= 10; $c++) {
                $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et ultricies orci. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Proin vehicula lorem ac sodales ullamcorper.';
                $table->addCell(1750)->addText($text);
            }
        }

        $this->output_file($phpWord, 'tt');
    }

private function output_file($phpWord, $name)
    {
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; filename="' . $name . '.docx"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Expires: 0');
        $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $xmlWriter->save("php://output");
    }

PhpWord version - latest stable (0.16.0)

Upvotes: 1

Views: 1457

Answers (1)

autumnrustle
autumnrustle

Reputation: 653

Just create cell object

$c1 = $row->addCell(100);
$c1->addText('1', ['bold' => true]);
$c1->addText('2');
$c1->addText('3', ['italic' => true]);

Upvotes: 1

Related Questions