Tables in .docx misformated in OpenOffice


Hi all,
I am very happy that I have found the PHPWord class which really does a great job.
The only problem I have concerns opening the generated .docx files in Open Office, latest version 3.3. I use many tables and they are completely misformatted. All cells are sized up and the document contains many empty lines which are not intended (see attached images).
I also tried to generate a .docx file with the AdvancedTable.php example from the PHPword class. The result is the same (see attached image).
In contrast if I open the generated .docx in Word, just press save and open it in OpenOffice afterwards, everything is fine.
Do you have any idea?

Best regards,


file attachments


api2001 wrote Oct 11, 2011 at 10:17 AM

I have found the reason for the malformat and how to solve it.
The .docx format is a zipped bunch of xml files. The document.xml contains all texts in the document. PHPWord (Beta 0.6.3, 08.07.2011) writes XML code to this file which is nicely formated (indents and newlines). Unfortunately OpenOffice somehow interprets those newlines as newlines in the Word document text. Microsoft Word does it right. And if you use Word to save the PHPWord document to a new document, it automatically deletes all newlines and indents. That is how I got behind it.
The solution for the problem is easy because it is already prepared. In PHP file Shared/XMLWriter.php line 79 and 84 you can switch off the indenting for the XML output documents. It is already proposed in a comment "Indenting proposed to be false in production version" but it is not implemented yet. Afterwards all XML documents are equally formated as Word does it and all generated documents look great!

TrevorBradley wrote Oct 25, 2011 at 11:20 PM

I'm not sure this fixes the problem. It appears to make no change to AdvancedTable.docx in the Examples, which is completely mangled with LibreOffice 3.4.3. See attached file for comparison.

neilt17 wrote Jan 14, 2013 at 9:36 PM

As was previously noted, the formatting in the XML files is not causing this problem. The problem actually appears to be caused by the lack of a tblGrid element in the document.xml file within the docx file generated by PHPWord. This element defines the columns of the table. Word appears to be able to work out some sensible formatting for the table without this element, but Open/Libre Office doesn't, typically generating a very wide table disappearing off the edge of the page. If you save the document with Word, a tblGrid element is generated in the resulting document - and this is why Libre Office can then display tables correctly when you re-open the document with it.

I am using a copy of PHPWord in the HTML to docx Converter project and have updated the code in it - in Base.php - to create a tblGrid element. You can get this code by going to http://htmltodocx.codeplex.com/SourceControl/list/changesets and going to the download link to download the latest source code, or you can clone it using Git. If you do do this, it would be great if you could leave some feedback about whether or not it worked as expected with tables you create with it. Note, this code has not yet been incorporated into a recommended release for the HTML to docx converter.

neilt17 wrote Feb 1, 2013 at 9:45 PM

As of 31st January 2013, the modification I described in my last post is now incorporated into the version of PHPWord used with the latest version of the HTML to docx Converter. It would be great if this modification could be included in PHPWord here too.

monir_kb wrote Oct 23, 2014 at 7:48 AM

I have same problem. I download updated source code but same problem showing. any help for problem. thanks for advance