Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lucasnetau opened this issue Nov 21, 2023 · 0 comments · May be fixed by #2516
Open
Milestone

Comments

@lucasnetau
Copy link

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
oleibman added a commit to oleibman/PHPWord that referenced this issue Nov 30, 2023
Fix PHPOffice#2505. Word treats file as corrupt if a table row does not contain a cell (documentation for why this is so is included in the issue). Person reporting the issue suggests that dropping such a row from the output file is preferred. However, I think generating an empty cell instead is closer to the user's expectation. For example, as demonstrated in the unit tests added with this PR, if a table has row 1 and 3 which contain cells, but row 2 does not, the table as written to the file will have 3 rows, with the second containing an empty cell.
@oleibman oleibman linked a pull request Nov 30, 2023 that will close this issue
3 tasks
@Progi1984 Progi1984 added this to the 2.0.0 milestone Dec 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants