I am creating .docx files from a template using PHPWord . It works fine, but now I want to convert the generated file to PDF .
At 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 upload a file to convert it to PDF , I get the following exception when uploading a file
Fatal error: Throw a "BadMethodCallException" exception with the message "Cannot add PreserveText to section".
After some research, I found that some others also have this error ( phpWord - Cannot add PreserveText to section )
EDIT
After I tried a little more, I found that Exception only occurs when there are mail merge fields in my document. As soon as I deleted them, Exception no longer appears, but the converted PDF files look awful. All information about the style has disappeared, and I canβt use the result, so the need for an alternative remains.
I was thinking of using another way to create a PDF file, but I could only find 4 ways:
- Using OpenOffice . It is impossible, since I cannot install any software on the Server. In addition, the mentioned method here did not work either as my host ( Strato ) uses SunOS as an OS, and this requires Linux
- Using phpdocx - I donβt have a budget to pay, and the demo cannot create a PDF
- Using PHPLiveDocx - it works, but has a limit of 250 documents per day and 20 per hour, and I have to convert about 300 documents at the same time, maybe even several times a day.
- Using PHP-Digital-Format-Convert - the output looks better than with
PHPWord and tcpdf , but is still unavailable because there are no images, and most (not all!) Styles
Is there a 5th way to create a PDF file? Or is there any solution so that the generated PDFs look good?
php pdf docx phpword
Pinguin895
source share