Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate PDF from .docx generated by PHPWord

I am creating .docx files from a template using PHPWord. It works fine but now I want to convert the generated file to PDF.

First I tried using tcpdf in combination with PHPWord

$wordPdf = \PhpOffice\PhpWord\IOFactory::load($filename.".docx");

\PhpOffice\PhpWord\Settings::setPdfRendererPath(dirname(__FILE__)."/../../Office/tcpdf");
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');

$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF');
if (file_exists($filename.".pdf")) unlink($filename.".pdf");
$pdfWriter->save($filename.".pdf");  

but when I try to load the file to convert it to PDF I get the following exception while loading the file

Fatal error: Uncaught exception 'BadMethodCallException' with message 'Cannot add PreserveText in Section.'

After some research I found that some others also have this bug (phpWord - Cannot add PreserveText in Section)

EDIT

After trying around some more I found out, that the Exception only occurs when I have some mail merge fields in my document. Once I removed them the Exception does not come up anymore, but the converted PDF files look horrible. All style information are gone and I can't use the result, so the need for an alternative stays.



I thought about using another way to generate the PDF, but I could only find 4 ways:

  1. Using OpenOffice - Impossible as I cannot install any software on the Server. Also going the way mentioned here did not work either as my hoster (Strato) uses SunOS as the OS and this needs Linux
  2. Using phpdocx - I do not have any budget to pay for it and the demo cannot create PDF
  3. Using PHPLiveDocx - This works, but has the limitation of 250 documents per day and 20 per hour and I have to convert arround 300 documents at once, maybe even multiple times a day
  4. Using PHP-Digital-Format-Convert - The output looks better than with PHPWord and tcpdf, but still not usable as images are missing, and most (not all!) of the styles

Is there a 5th way to generate the PDF? Or is there any solution to make the generated PDF documents look nice?

like image 673
Pinguin895 Avatar asked Oct 12 '15 14:10

Pinguin895


People also ask

How do you turn a .DOCX file into a PDF?

Drag and drop a Microsoft Word document (DOCX or DOC) to convert to PDF. Select a Microsoft Word document (DOCX or DOC) to convert to PDF. Drag and drop a Microsoft Word document (DOCX or DOC) to convert to PDF. Your file will be uploaded to Adobe cloud storage.

How do I convert a DOCX file to PDF without word?

Drag and drop docx file if you have previously downloaded to your computer or mobile. If the docx file you want to open is stored in the cloud, click on Dropbox or Google Drive option, and import docx file from there. The docx file will automatically start to convert to PDF, as soon as you import it.


2 Answers

I used Gears/pdf to convert the docx file generated by phpword to PDF:

$success = Gears\Pdf::convert(
            'file_path/file_name.docx',
            'file_path/file_name.pdf');
like image 153
Yoga Avatar answered Sep 26 '22 12:09

Yoga


You're trying to unlink the PDF file before saving it, and you have also to unlink the DOCX document, not the PDF one.

Try this.

$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF');    
$pdfWriter->save($filename.".pdf");
unlink($wordPdf);
like image 31
Fabrizio Avatar answered Sep 22 '22 12:09

Fabrizio