php – No styling when converting DOCX into PDF with PHPWord-ThrowExceptions

Exception or error:

I am trying to convert a DOCX file to PDF with PHPWord. When I execute the script it looks like that some style elements are not converted. In the DOCX file I have one image, two tables with border 1px and hidden borders and I am using Tabs.

When I execute the script I get a PDF file without the image, all the Tabs are replaced with Space and all the tables have a border 3px.

Does someone know why I am missing these styles?

Here is my script:

while ($data2 = mysql_fetch_array($rsSql)){
  $countLines=$countLines+1;
  $templateProcessor->setValue('quantity#'.$countLines, $data2['quantity']);
  $templateProcessor->setValue('name#'.$countLines, $data2['name']);
  $templateProcessor->setValue('price#'.$countLines, "€ " .$data2['price'] ."");
}

\PhpOffice\PhpWord\Settings::setPdfRenderer('./dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererPath('./dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererName('DOMPDF');

$temp_file = tempnam(sys_get_temp_dir(), 'Word');
\$templateProcessor->saveAS($temp_file);

$phpWord = \PhpOffice\PhpWord\IOFactory::load($temp_file); 

$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save('result.pdf');  

header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='result.pdf'");
readfile("result.pdf");
How to solve:

After a look on the source code, it seems that PHPWord previously converts the document into an HTML representation before letting it be saved it into PDF by dompdf, another converter.

That’s what the opened issue #1139 confirms, moreover it deals with styles missing:

The PDF writers being used are taking in the HTML output, which also lacks the styling. The classes are being defined in the <style> tag, but they are just not being used.

Also the last message adds:

This still seems to be an issue. html and pdf outputs do not replicate the some styles in docx (header / footers).

Concerning your border problem, another SO question shows a similar issue in a conversion HTML -> PDF. A solution was to edit the CSS style, which you obviously cannot perform in your sample code, unless you proceed to pre-convert into HTML.

In conclusion, you may not solve your problem in the short term. If you won’t be a part of the dev team, you could submit bug reports to them (and not to dompdf, since it’s an HTML-to-PDF converter and they are outside the scope). Github lets you to add DOCX files to the issue report.

Alternatives

You could check out a SO question 204860 about server sides PDF editing library. Below two alternatives, one is free software, the other is closed source and priced.

LibreOffice

Another way is to use LibreOffice in headless mode (command line execution without interface):

libreoffice --headless --convert-to pdf <filename_to_convert>

A PHP wrapper for LibreOffice, Office Converter is also available here if you don’t want to bother using libreoffice through exec().

Check if LibreOffice conversion will suit your needs (it may not cover all cases, but be satisfying your scope).

Aspose

The best converter I ever used at work is Aspose, an API covering Documents with Aspose.Words package, Worksheets with Aspose.Cells, Presentations with Aspose.Slides and so on. But it’s closed-source and pretty expensive (and you’ll pay for updates if you want them after your license expiration).

There is a way to use it in PHP through Java (Aspose.Words and Aspose.Cells) or .NET (Aspose.Words same seems to go with Aspose.Cells).

Leave a Reply

Your email address will not be published. Required fields are marked *