PHPWord 到 PDF 无法加载库

PHPWord to PDF not able to load library

我整天都在努力让它工作。现在我可以将文档另存为 .docx 文件,但我希望能够将文档另存为 PDF 我已经尝试使用 DOMPDF 和 TCPDF 但我一直收到错误 'Unable to load PDF Library' 我确认路径是正确的。我错过了什么吗?谢谢你的帮助

$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' 中的消息 'Unable to load PDF Rendering library' .../PHPWord/Writer/PDF/AbstractRenderer.php:92 堆栈跟踪:#0 .../PHPWord/Writer/PDF.php(61): PhpOffice\PhpWord\Writer\PDF\AbstractRenderer->__construct(对象(PhpOffice\PhpWord\PhpWord)) #1 .../PHPWord/IOFactory.php(34): PhpOffice\PhpWord\Writer\PDF->__construct(对象(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:\wamp\www\cccake\app\Vendor\dompdf\dompdf.php/dompdf_config.inc.php\n

与其更改 PHPExcel/PHPWord 的 class 文件,不如在您的视图中更改配置是明智的。在 $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