PHPWord to PDF not able to load library

2019-02-26 08:07发布

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

Here is the code that is throwing the error

  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
    }
}

Here is the full error

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

2条回答
劫难
2楼-- · 2019-02-26 08:08

When I print the error to log, I realize it is trying to load the dompdf_config.inc.php from an invalid location. See below.

C:\wamp\www\cccake\app\Vendor\dompdf\dompdf.php/dompdf_config.inc.php\n

Rather than altering the class files of PHPExcel/ PHPWord, it's wise to change the configuration in your view. Ignoring the $rendererLibrary completely in the $rendererLibraryPath fixed my issue. I think the PHPExcel/PHPExcel know how to pick dompdf.php file (tcpdf.php) in your case. Try below code let us know if it doesn't work.

$rendererLibrary = 'tcpdf.php'; $rendererLibraryPath = dirname(FILE) .'/plugins/tcpdf';

查看更多
ら.Afraid
3楼-- · 2019-02-26 08:28

Knowing al that, I'm guessing the answer is changing:

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

to

$includeFile = Settings::getPdfRendererPath();
查看更多
登录 后发表回答