XML解析错误;PHPWord

Ann*_*ine 3 php xml laravel phpword phpoffice

我在Laravel应用程序中使用PHPOffice / PHPWord。它用于生成带有表结果的.docx文档。这对于包含6行的3个表的文档非常有用,但是当有更多行时会生成该文档,但是在打开它时会发生以下错误:

We're sorry, We can't open (documentname) because we found a problem with its contents.

Details: XML parsing error Location: Part:/word/document.xml, Line: 2, Column 14349. 
Run Code Online (Sandbox Code Playgroud)

现在,我开始在另一个结果页面上工作,在该页面上我还想生成一个.docx文档。这将包含5个表,但是在3行中,我得到相同的XML解析错误,但是位置不同(位置:Part:/word/document.xml,Line:4,Column:2888)。有人可以向我解释这是我的代码还是phpword / words错误吗?

我已经通过删除所有内容并缓慢添加新行来进行一些故障排除。我发现了错误,但我该如何解决。前两个表生成良好。

    $phpWord = new \PhpOffice\PhpWord\PhpWord();

    $section = $phpWord->addSection();
    $section->addImage('../public/img/2.jpg', array('width' => 230, 'height' => 65, 'alignment' => 'left'));
    $section->addText('Project IDs:' . $parameter);
    $header =$section->addHeader();
    $header->addText('Results Summary');

    $section->addLine(
        array(
            'width'       => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16),
            'height'      => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0),
            'positioning' => 'absolute',
        )
    );
    $tableName = 'rStyle';
    $phpWord->addFontStyle($tableName, array('italic' => true, 'size' => 12));
    $thName = 'tStyle';
    $phpWord->addFontStyle($thName, array('bold' => true, 'size' => 9));

    $section->addText('General Information Table', $tableName);
    $fancyTableStyle = array('borderSize' => 6, 'borderColor' => '999999');
    $spanTableStyleName = 'Overview tables';
    $phpWord->addTableStyle($spanTableStyleName, $fancyTableStyle);
    $table = $section->addTable($spanTableStyleName);
    $table->addRow(null, array('tblHeader' => true, 'cantSplit' => true));
    $table->addCell(1750)->addText('Project ID',$thName);
    $table->addCell(1750)->addText('Description',$thName);
    $table->addCell(1750)->addText('Notes',$thName);
    foreach ($id_array_explode as $char) {
        $table->addRow();
        $singlenumber = (int)$char;
        $cursor = $collection->find(array("id" => $singlenumber));
        foreach ($cursor as $document) {
                    $table->addCell(1750)->addText($document["project_id"]);
                    $table->addCell(1750)->addText($document["description"]);
                    $table->addCell(1750)->addText($document["notes"]);
        }
    }
    $section->addText('        
');
    $section->addLine(
        array(
            'width'       => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16),
            'height'      => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0),
            'positioning' => 'absolute',
        )
    );
    $section->addText('Input Table', $tableName);
    $table1 = $section->addTable($spanTableStyleName);
    $table1->addRow(null, array('tblHeader' => true, 'cantSplit' => true));
    $table1->addCell(1750)->addText('Project ID',$thName);
    $table1->addCell(1750)->addText('#',$thName);
    foreach ($id_array_explode as $char) {
        $table1->addRow();
        $singlenumber = (int)$char;
        $cursor = $collection->find(array("id" => $singlenumber));
        foreach ($cursor as $document) {
            if (is_array($document['input'])) {
                foreach ($document['input'] as $samples) {
                    $table1->addCell(1750)->addText($document["project_id"]);
                    $table1->addCell(1750)->addText($samples['nr']);
                }
            }
        }
    }
    $section->addText('        
');
    $section->addLine(
        array(
            'width'       => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16),
            'height'      => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0),
            'positioning' => 'absolute',
        )
    );
    $section->addText('Output Table', $tableName);
    $table2 = $section->addTable($spanTableStyleName);
//// THIS IS WHERE THE ERROR OCCURS!! 
        $table2->addRow(null, array('tblHeader' => true, 'cantSplit' => true));
        $table2->addCell(1750)->addText('ID',$thName);
Run Code Online (Sandbox Code Playgroud)

谢谢!

解决方案 好吧,所以我删除了整个文档,并分别添加了每个句子,以查看错误发生的位置。这导致看到错误来自我得到的数据。它无法处理“>”和“&”符号!

因此,如果每个人都有此错误,请检查要打印的数据!

JMa*_*Mac 8

更好的解决方案是在对word文档执行任何操作之前添加以下代码行:

PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
Run Code Online (Sandbox Code Playgroud)

这将自动转义任何有问题的字符。