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 somemail merge fields
in my document. Once I removed them theException
does not come up anymore, but the converted
I thought about using another way to generate the PDF, but I could only find 4 ways:
PHPWord
and tcpdf
, but still not usable as images are missing, and most (not all!) of the stylesIs there a 5th way to generate the PDF? Or is there any solution to make the generated PDF documents look nice?