0

I am using PhpWord to create a document that contains comments. I can create the file successfully, but when I try to open it, Word reports that it contains unreadable content. Although Word will continue to open the file, and the comments appear to be in the correct places, I would still like to find an elegant solution that prevents Word from displaying the warning.

I renamed the .docx file to a .zip and found that the package contains 2 comments.xml files.

Duplicate comments.xml files

The two files are identical, except that one contains the id for the comments, and the other does not.

XML file with Comment ID

XML file without Comment ID

Below is the code. It works fine if I do not include the comments. Any suggestions?

<?php
ob_start(); // output buffering is turned on
require_once('bootstrap.php');

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->getCompatibility()->setOoxmlVersion(15);

// Adding an empty Section to the document...
$section1 = $phpWord->addSection();

      // A comment
      $comment = new \PhpOffice\PhpWord\Element\Comment('Authors name');
      $comment->addText('Test comment', ['bold' => true]);
      $phpWord->addComment($comment);

      $textrun = $section1->addTextRun();
      $textrun->addText('This ');
      $text = $textrun->addText('is');
      $text->setCommentRangeStart($comment);
      $textrun->addText(' a test');
                        
ob_clean();

$temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');

\PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);

// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($temp_file);

header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="duplicates.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');

readfile($temp_file); 
file_get_contents($temp_file);

?>

1 Answer 1

0

I was able to address this issue by commenting out line 271 in PHPWord/src/PhpWord/Writer/Word2007.php. However, I am a novice at object-oriented programming, so this might not be a good solution.

private function addComments(ZipArchive $zip, &$rId): void
{
    $phpWord = $this->getPhpWord();
    $collection = $phpWord->getComments();
    $partName = 'comments';

    // Add comment relations and contents
    /** @var \PhpOffice\PhpWord\Collection\AbstractCollection $collection Type hint */
    if ($collection->countItems() > 0) {
        $this->relationships[] = ['target' => "{$partName}.xml", 'type' => $partName, 'rID' => ++$rId];

        // Write content file, e.g. word/comments.xml
        $writerPart = $this->getWriterPart($partName)->setElements($collection->getItems());
// Comment this line:        $zip->addFromString("word/{$partName}.xml", $writerPart->write());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.