I used laravel for my app and the dompdf is found in:
../vendor/dompdf/dompdf
What I wanted to achieve is to convert my .docx file (Microsoft Word) to .pdf file. The docx file was generated by phpWord by loading the template file and replacing the values.
Here's the snippet:
// Get the absolute path of the template file
$wordTemplatePath = $this->getDocumentTemplatePath('resignation.docx');
// Load the template file
$document = $this->phpWord->loadTemplate($wordTemplatePath);
// This will be filled with data
$wordData = []
.... Process to fill the data .....
// Replace value to actual word document
$this->setTemplateValues($wordData, $document);
// Generate its filename
$file = $this->generateFileName('Retirement-Certificate.docx', $id);
// Fetch the absolute path to save the document
$filepath = $this->getSavePath($file);
// Save the word document
$document->saveAs( $filepath );
After that, a .docx file will be generated. I wanted to make a PDF version of that as well. so I search and found this code snippet and added in my code:
\PhpOffice\PhpWord\Settings::setPdfRendererPath('../vendor/dompdf/dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererName('DOMPDF');
//Load temp file
$phpWord = \PhpOffice\PhpWord\IOFactory::load($filepath);
//Save it
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save('result.pdf');
But I got this error:
{"error":
{"type":"PhpOffice\\PhpWord\\Exception\\Exception","message":"PDF rendering library or library path has not been defined.",
"file":"C:\\xampp\\htdocs\\vagrant\\vendor\\phpoffice\\phpword\\src\\PhpWord\\Writer\\PDF.php",
"line":49}}
How would I make DomPDF as my pdf writer for PHPWord? I can't find any other options so I ask here. I hope you can help me. Thanks!
You must set it to the absolute path. Put this into your bootstrap.php file.
define('PHPWORD_BASE_DIR', realpath(__DIR__));
This is your call:
$domPdfPath = realpath(PHPWORD_BASE_DIR . '/../vendor/dompdf/dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererPath($domPdfPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');
//Load temp file
$phpWord = \PhpOffice\PhpWord\IOFactory::load($filepath);
//Save it
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save('result.pdf');
I think @Franz Holzingers answer is right on point except that the version of PHP word that I installed today requires the renderer name to be DomPDF. The method fails if it is all caps as shown. Also the version that I installed to day uses a configuration file so while the explicit calls work, if you place them after the configuration file loads, they are overwritten if you place them before the configuration loads. I propose a simpler answer is to locate the file "phpword.ini.dist" located in the directory above "src" in the phpword install directories; edit the line for the DomPDF directory to show where you installed DomPDF and then save the file in the same directory but under the name phpword.ini. Now the configuration loader will perform the calls that Frank documented for you. If you enter the path to DomPDF incorrectly, you get no warning but you can test that it installed correctly using the method \PhpOffice\PhpWord\Settings::getPdfRendererPath(domPdfPath); If the return from that method is empty, it means that the path name you specified in the configuration file did not exist. Note that the loader does not check that the path contains DomPDF, only that the path exists.
It is worth noting that at this time, phpword supports three PDF rendering engines. Any of the three can be configured in the ini file mentioned above by providing the path to the installation and setting the name of the PDF renderer to one of these values (case sensitive): DomPDF, TCPDF or MPDF. DomPDF is recommended and the default but it appears in the code that if you have an investment in or a preference for one of the others, you can used it with phpword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With