我在我的应用程序中使用了 laravel,dompdf 位于:
../供应商/dompdf/dompdf
我想要实现的是将我的 .docx 文件(Microsoft Word)转换为 .pdf 文件。docx 文件由 phpWord 通过加载模板文件并替换值生成。
这是片段:
// 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 );
之后,将生成一个 .docx 文件。我也想制作一个PDF版本。所以我搜索并找到了这个代码片段并添加到我的代码中:
\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');
但我得到了这个错误:
{"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}}
我如何将 DomPDF 作为 PHPWord 的 pdf 编写器?我找不到任何其他选择,所以我在这里问。我希望你能帮助我。谢谢!