Baj*_*kie 8 php dompdf composer-php phpword
我使用laravel为我的应用程序和dompdf找到:
../vendor/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 );
Run Code Online (Sandbox Code Playgroud)
之后,将生成.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');
Run Code Online (Sandbox Code Playgroud)
但我得到了这个错误:
{"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}}
Run Code Online (Sandbox Code Playgroud)
我如何将DomPDF作为PHPWord的pdf编写器?我找不到任何其他选项,所以我在这里问.我希望你能帮助我.谢谢!
您必须将其设置为绝对路径.把它放到你的bootstrap.php文件中.
define('PHPWORD_BASE_DIR', realpath(__DIR__));
Run Code Online (Sandbox Code Playgroud)
这是你的电话:
$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');
Run Code Online (Sandbox Code Playgroud)