我一直在努力让这个工作起来。现在我能够将文档保存为.docx文件,但我希望能够将文档保存为PDF,我已尝试使用DOMPDF和TCPDF,但我一直收到错误“无法加载PDF库”我确认路径是正确的。我错过了什么吗?谢谢您的帮助
$rendererName = \PhpOffice\PhpWord\Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'tcpdf.php';
$rendererLibraryPath = dirname(__FILE__) .'/plugins/tcpdf/' . $rendererLibrary;
\PhpOffice\PhpWord\Settings::setPdfRenderer($rendererName,$rendererLibraryPath);
$document->saveAs('temp.docx'); // Save to temp file
$test = \PhpOffice\PhpWord\IOFactory::load('temp.docx'); // Read the temp file
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($test, 'PDF');
$xmlWriter->save('result.pdf'); // Save to PDF
unlink('temp.docx'); // Delete the temp file
这是抛出错误的代码
public function __construct(PhpWord $phpWord)
{
parent::__construct($phpWord);
$includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;
if (file_exists($includeFile)) {
/** @noinspection PhpIncludeInspection Dynamic includes */
require_once $includeFile;
} else {
// @codeCoverageIgnoreStart
// Can't find any test case. Uncomment when found.
throw new Exception('Unable to load PDF Rendering library');
// @codeCoverageIgnoreEnd
}
}
这是完整的错误
致命错误:未捕获的异常'PhpOffice \ PhpWord \ Exception \ Exception',消息'无法加载PDF渲染库'在... / PHPWord / Writer / PDF / AbstractRenderer.php:92堆栈跟踪:#0 ... / PHPWord /Writer/PDF.php(61):PhpOffice \ PhpWord \ Writer \ PDF \ AbstractRenderer - > __ construct(Object(PhpOffice \ PhpWord \ PhpWord))#1 ... / PHPWord / IOFactory.php(34):PhpOffice \ PhpWord \ Writer \ PDF - > __ construct(Object(PhpOffice \ PhpWord \ PhpWord))#2 ... / download_report.php(578):PhpOffice \ PhpWord \ IOFactory :: createWriter(Object(PhpOffice \ PhpWord \ PhpWord),'PDF ')#3 {main}抛出... / PHPWord / Writer / PDF / AbstractRenderer.php在第92行
知道了,我猜测答案正在改变:
$includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;
至
$includeFile = Settings::getPdfRendererPath();
当我将错误打印到日志时,我意识到它正在尝试从无效位置加载dompdf_config.inc.php。见下文。
C:\瓦帕\ WWW \ cccake \应用\卖方\ DOMPDF \ dompdf.php / dompdf_config.inc.php \ n
而不是更改PHPExcel / PHPWord的类文件,更改视图中的配置是明智的。在$ rendererLibraryPath中完全忽略$ rendererLibrary修复了我的问题。我认为PHPExcel / PHPExcel知道如何在你的情况下选择dompdf.php文件(tcpdf.php)。尝试以下代码,如果它不起作用,请告诉我们。
$ rendererLibrary ='tcpdf.php'; $ rendererLibraryPath = dirname(FILE)。'/ plugins / tcpdf';
确保在以下位置发送DOMPDF路径:
$domPdfPath = "..vendor/dompdf/dompdf/";
Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
Settings::setPdfRendererPath($domPdfPath); //<---
然后在AbstractRerender构造中,如果你回显$ this-> Include文件,你应该看到'dompdf_config.inc.php'
如果您没有dompdf_config.inc.php文件,请尝试使用dompdf 0.6.2