我一整天都在尝试让它发挥作用。现在我能够将文档保存为.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 \作家\ PDF-> __构建体(对象(PhpOffice \ PhpWord \ PhpWord))#2 ... / download_report.php(578):PhpOffice \ PhpWord \ IOFactory :: createWriter(对象(PhpOffice \ PhpWord \ PhpWord), 'PDF')#3 {}主要在... / PHPWord /作家抛出/ PDF / AbstractRenderer.php上第92行
答案 0 :(得分:2)
知道这一点,我猜测答案正在改变:
$includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;
到
$includeFile = Settings::getPdfRendererPath();
答案 1 :(得分:0)
当我将错误打印到日志时,我意识到它正在尝试从无效位置加载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';
答案 2 :(得分:0)
确保将DOMPDF路径发送到:
$ vim filename
然后在AbstractRerender构造中,如果您回显$ this-> include文件,则应看到'dompdf_config.inc.php'
如果没有dompdf_config.inc.php文件,请尝试使用dompdf 0.6.2