Creating DomPDF as a PDF author for phpWord

I used laravel for my application, and dompdf is located in:

../seller/DOMPDF/DOMPDF

What I wanted to achieve was to convert my .docx file (Microsoft Word) to a .pdf file. The docx file was generated by phpWord by loading the template file and replacing the values.

Here's a 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 created. I also wanted to make a PDF version. so I found and found this piece of code and added to 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 do I make DomPDF as the author of PDF for PHPWord? I can not find any other options, so I ask here. I hope you can help me. Thanks!

+5
source share
2 answers

I think the @Franz Holzingers answer is true that the version of the PHP word that I installed today requires the rendering name to be DomPDF. The method fails if these are all caps as shown. Also the version that I installed per day uses the configuration file, so when explicit calls work, if you place them after loading the configuration file, they will be overwritten if you place them before loading the configuration. I suggest a simpler answer - find the file "phpword.ini.dist" located in the directory above "src" in the phpword installation 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 complete the calls that Frank has documented for you. If you specified the path to DomPDF incorrectly, you will not receive a warning, but you can verify that it is correct using the \ PhpOffice \ PhpWord \ Settings :: getPdfRendererPath (domPdfPath) method; If the return from this method is empty, it means that the path name specified in the configuration file does not exist. Note that the bootloader does not verify that the path contains DomPDF, only that path exists.

It is worth noting that phpword currently supports three PDF rendering mechanisms. Any of the three can be configured in the above ini file, specifying the installation path and setting the PDF rendering name to one of these values ​​(case sensitive): DomPDF, TCPDF or MPDF. DomPDF is recommended by default, but it appears in code, if you have an investment or preference for one of the others, you can use it with phpword.

+3
source

You must set it on an absolute path. Put this in the 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'); 
+2
source

Source: https://habr.com/ru/post/1201395/


All Articles