Skip to content
Advertisement

PhpWord output doesn’t work in LibreOffice but works fine in MS Word

This relates to a semi-known issue listed on the GitHub page [link], where the way PhpWord generates tables causes it to set the cell width to “”, which isn’t valid in LibreOffice, but is fine in Word.

The issue on GitHub lists html-to-word conversion specifically, but I’m getting this issue when just going by the regular object-oriented interface also.

I’ve tried manually setting the cell width, but it doesn’t seem to do anything.

I’m asking here to see if anyone has any workaround to this issue, as it makes it nearly impossible to debug my output on a Linux box without access to MS Word. I need to export .docx for a client.

Why not just export to .odt to test it instead? If I do that it won’t render the list I have on the page either…..

Here’s a rough outline of the document I’m working with (I’m not at liberty to share the exact code due to NDAs):

JavaScript

I’m using PHP 7.0 (ancient legacy system, can’t do anything about it) on Ubuntu.

So just to reiterate the question here at the end:

Is there something I can do to make this document render correctly on all the different outputs? The documentation doesn’t seem to be much help.

Advertisement

Answer

Well, I’m not a maintainer from the PHPOffice package but here is a workaround which I made digging in the package code and fixing your two issues:

  1. ODT List does not work;
  2. Tables on Word2007 Writer when opening with LibreOffice does not work.

I realized that the ListItem.php class did not exist on ODText writer, and I suppose that this is why you can not add lists to .odt files.

You need to add the widths manually to columns to make it work on LibreOffice (as Nigel said, you can go through each addCell() call and add some number, even “1” works).

How to fix both issues

We will “override” the original files with some updates that fix the issues. In order to make it work, you should have installed PHPWord using Composer. (This is the only option available on the installation guide, but there are other ways to do that).

Without Composer, require the custom files after PHPWord require (I think that it works). Or change directly the files on their locations (a bad idea).

Files

Create a structure like that:

JavaScript

Of course, you can use any other, but we’re going to “override” the original package files, so I kept its structure.

We will use Autoload Files to get those files:

composer.json:

JavaScript

If there’s no “autoload” key on your composer.json, you have to add it, and the same goes for the “files” key.

Run composer dump-autoload to update the changes.

Custom classes

Now, we have to add the code to our custom files.

Custom/Writer/ODText/Element/ListItem.php:

JavaScript

The file has been adapted from the Word2007 version and fixes your first issue. Now lists will work on ODText.

Custom/Element/Table.php:

JavaScript

As we are “overwriting” the file, we cannot reuse the original file extending it (at least, I don’t know how). The only change here is on public function addCell($width = 1, $style = null) that makes always “1” as default value to the function. This fixes your second issue, and now you can call $table->addCell() without a value.

DEMO

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement