【问题标题】:PHPWord to PDF not able to load libraryPHPWord 到 PDF 无法加载库
【发布时间】:2015-07-02 06:34:29
【问题描述】:

我整天都在努力让它发挥作用。现在我可以将文档另存为 .docx 文件,但我希望能够将文档另存为 PDF 我曾尝试使用 DOMPDF 和 TCPPDF 但我不断收到错误“无法加载 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} throw in .../PHPWord/Writer/PDF/AbstractRenderer.php 在第 92 行

【问题讨论】:

  • 您在哪一行得到错误?它显示吗?你检查过tcpdf文件夹的权限了吗?
  • 错误行 AbstractRenderer.php:92 和 PDF.php(61) 我没有检查权限它们应该是什么?
  • 可能 755 是开始获取权限的好方法。尽管您可以,作为测试将它们设置为 777 只是为了查看是否是问题所在,然后将其切换回来。
  • 它们在 755,我将它们更改为 777,但仍然没有相同的内容我将发布引发错误的代码行
  • 好吧,它 def 必须是路径,因为 file_exists 失败了。 var_dump $includeFile 设置后的行。结果如何?

标签: php tcpdf dompdf phpword


【解决方案1】:

知道这一点,我猜答案正在改变:

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

$includeFile = Settings::getPdfRendererPath();

【讨论】:

    【解决方案2】:

    当我将错误打印到日志时,我意识到它正在尝试从无效位置加载 dompdf_config.inc.php。见下文。

    C:\wamp\www\cccake\app\Vendor\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';

    【讨论】:

      【解决方案3】:

      确保发送 DOMPDF 路径在:

      $domPdfPath = "..vendor/dompdf/dompdf/";
      Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
      Settings::setPdfRendererPath($domPdfPath); //<---
      

      然后在 AbstractRerender 构造中,如果您回显 $this->包含文件,您应该会看到 'dompdf_config.inc.php'

      如果您没有 dompdf_config.inc.php 文件,请尝试使用 dompdf 0.6.2

      【讨论】:

        猜你喜欢
        • 2021-06-10
        • 1970-01-01
        • 2018-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-21
        • 2016-10-08
        • 1970-01-01
        相关资源
        最近更新 更多