Skip to content

Adding a row to a table without a cell produces a corrupt docx for Word (works in LibreOffice) #2505

Closed
@lucasnetau

Description

@lucasnetau

Describe the Bug

Creating a document that includes a table with a row without a cell causes Microsoft Word to consider the document corrupt

Steps to Reproduce

Please provide a code sample that reproduces the issue.

<?php declare(strict_types=1);

use PhpOffice\PhpWord\SimpleType\TblWidth;

require_once __DIR__ . '/../../vendor/autoload.php';

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$table = $section->addTable([
    'width' => 5000,
    'unit' => TblWidth::PERCENT,
]);
$row = $table->addRow();

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save(__DIR__ . '/borked.docx');

Expected Behavior

One of the following would work, the first option being the one that makes the most sense:

  • Row is discarded when writing
    • The current Word2007 Table element write discards tables without rows, so it makes sense to also discard rows without cells while also ensuring that at least one row is written
  • Document is created with an implicit cell added (and required p block element)
    • This would create content in the document not expected
  • PHP Warning/Notice is raised for the missing cell.
    • No history of the library doing such a thing

Current Behavior

Document is created and cannot be opened in Word.

When opening and saving in LibreOffice the table is discarded in the saved file

Table row XML created with only the trPr child

<w:tr>
<w:trPr/>
</w:tr>

https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.tablerow?view=openxml-2.8.1 documents this as corrupt

A tr element has one formatting child element, trPr (§17.4.82), which defines the properties for the row. Each unique property on the table row is specified by a child element of this element. As well, a table row can contain any row-level content, which allows for the use of table cells.

If a table cell does not include at least one child element other than the row properties, then this document shall be considered corrupt.

Context

Please fill in your environment information:

  • PHP Version: 8.0-8.3
  • PHPWord Version: 1.1.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions