问题描述
我一直在努力使它正常工作。现在,我能够将文档另存为.docx文件,但是我希望能够将文档另存为PDF,这是我在DOMPDF和TCPDF上尝试过的,但是我一直收到错误消息无法加载PDF库,我确认路径正确。我想念什么吗?谢谢您的帮助
I have been trying all day to get this to work. Right now I am able to save the document as .docx file but I wanted to be able to save the document as PDF I have tried with DOMPDF and TCPDF but I keep getting error 'Unable to load PDF Library' I confirmed the path is correct. Am I missing something? thank you for your help
$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行
Fatal error: Uncaught exception 'PhpOffice\PhpWord\Exception\Exception' with message 'Unable to load PDF Rendering library' in.../PHPWord/Writer/PDF/AbstractRenderer.php:92 Stack trace: #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} thrown in .../PHPWord/Writer/PDF/AbstractRenderer.php on line 92
推荐答案
我知道,我猜答案是更改:
Knowing al that, I'm guessing the answer is changing:
$includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;
到
$includeFile = Settings::getPdfRendererPath();
这篇关于PHPWord到PDF无法加载库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!