XML解析错误; PHPWord
XML Parsing error; PHPWord
我在 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.
现在,我已经开始处理另一个结果页面,我还想在其中生成一个 .docx 文档。这将包含 5 个表,但有 3 行我得到相同的 XML 解析错误但在不同的位置(位置:部分:/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);
谢谢!
解决方案
好的,所以我删除了整个文档并分别添加了每个句子以查看错误发生的位置。这导致看到错误来自我得到的数据。它无法处理“>”和“&”符号!
因此,如果您遇到此错误,请检查您正在打印的数据!
对 PHPWord 不是很熟悉,但请确保文档的编码与要插入其中的数据相同。曾经与用于创建 excel 文件的旧库有同样的问题。
确实,它来自您的数据:其中有一个 XML 特殊字符,当 Word 解析您的文档时,它无法理解。
我使用 htmlspecialchars()
解决了这个问题。我不确定这是最好的方法,但它确实有效。
更好的解决方案是在对 word 文档执行任何操作之前添加以下代码行:
PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
这将自动转义任何有问题的字符。
我在 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.
现在,我已经开始处理另一个结果页面,我还想在其中生成一个 .docx 文档。这将包含 5 个表,但有 3 行我得到相同的 XML 解析错误但在不同的位置(位置:部分:/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);
谢谢!
解决方案 好的,所以我删除了整个文档并分别添加了每个句子以查看错误发生的位置。这导致看到错误来自我得到的数据。它无法处理“>”和“&”符号!
因此,如果您遇到此错误,请检查您正在打印的数据!
对 PHPWord 不是很熟悉,但请确保文档的编码与要插入其中的数据相同。曾经与用于创建 excel 文件的旧库有同样的问题。
确实,它来自您的数据:其中有一个 XML 特殊字符,当 Word 解析您的文档时,它无法理解。
我使用 htmlspecialchars()
解决了这个问题。我不确定这是最好的方法,但它确实有效。
更好的解决方案是在对 word 文档执行任何操作之前添加以下代码行:
PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
这将自动转义任何有问题的字符。