5

I am using PHPOffice/PHPWord in my Laravel Application. It is used to generate a .docx document with results in tables. This works great for a document of 3 tables with 6 rows, but when there are more rows the document is generated but when opening it the following error occurs:

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. 

Now, I have started working on another result page where I would also want to generate a .docx document. This will contain 5 tables, but with 3 rows I get the same XML parsing error but in a different location (Location: Part: /word/document.xml, Line:4, Column:2888). Could someone explain to me whether this is a error in my code, or phpword/words?

I have done some troubleshooting by deleting everything, and slowly adding new rows. I have found the error but how could i fix it. The first two tables are generated good..

    $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);

Thank you!

SOLUTION Oke, so I have deleted the whole document and added every single sentence separately to see where the error occurred. This led to seeing that the error came from the data which I was getting. It couldn't handle ">" and "&" signs!

So, if you every have this error, check the data which you're printing!

2
  • Weirdest thing.. I removed the parameters in ->addRow(), added all the ->addCell once at a time.. and how it works again. I thing the problem was too much table heads?.. Commented Oct 28, 2016 at 9:09
  • So the problem was non-escaped output, the answer of JMac solves this: stackoverflow.com/a/49453308/2395363 Commented Oct 7, 2018 at 18:40

3 Answers 3

15

A better solution is to add the following line of code before you do anything with the word document:

PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);

This will automatically escape any problematic characters.

Sign up to request clarification or add additional context in comments.

Comments

3

Indeed, It comes from your data : you have a XML special character in it and when Word, parses your doc, it doesn't understand. I solved this problem by using htmlspecialchars(). I'm not sure it is the best way but it works.

2 Comments

The & character was the culprit for me; phpword doesn't seem to do a lot of escaping by itself
In my case I have had to add also strip_tags and html_entity_decode to solve the issue.
0

Not very familiar with PHPWord, but make sure the encoding of your document and the data you are inserting into it are the same. Used to have the same problem with an old library for creating excel files.

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.