PHPWord to PDF not able to load library

joanb picture joanb · Apr 23, 2015 · Viewed 8k times · Source

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

Answer

dgig picture dgig · Apr 23, 2015

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

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

to

$includeFile = Settings::getPdfRendererPath();