PHPWord 到 PDF 无法加载库

标签 php tcpdf dompdf phpword

我一整天都在努力让它工作。现在我可以将文档保存为 .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
    }
}

这是完整的错误

fatal error :未捕获的异常“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} 在第 92 行抛出 .../PHPWord/Writer/PDF/AbstractRenderer.php

最佳答案

知道所有这些,我猜答案正在改变:

$includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;

$includeFile = Settings::getPdfRendererPath();

关于PHPWord 到 PDF 无法加载库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29834208/

相关文章:

php - TCPDF - DIV 前后显示额外空间

php - DOMPDF - 在个案基础上定义 DOMPDF_DPI?

php - php中的数组,比较2个数组并保留重复值

php - 缓存包含在 PHP 中以供迭代重用

php - 如何根据不同的外部值显示 mysql 表中的行

php - 在 TCPDF 中缩放 HTML 表格以适应 PDF 页面

audio - TCPDF-在浏览器中生成PDF并播放音频

javascript - 单击父复选框时选中所有选中的框

php - 使用 dompdf 从数据库渲染数据时出错

html - 如何将多个页面的正文内容与固定的页眉和页脚分开