PHPWord generated charts don't display when saving as PDF

55 views Asked by At

I'm running some reporting via a bespoke Laravel system. These reports are generated as docx files using PHPWord and are then converted to PDF via dompdf and saved to disk. The docx file is generating correctly, and the conversion to PDF works without error, but if there is a chart in the docx file, this is completely ignored and omitted from the PDF.

My initial thinking was that this could be retailed to dompdf requiring the isRemoteEnabled flag to be set to true however this doesn't work. In fact I can embed an image in the docx file and this is present in the PDF without issue.

Is there something I'm missing here, or is saving to PDF with charts simply not supported?

Code below...

    $file_path = storage_path() . '/app/reports/' . $file_name;
    $word = new PhpWord();
    
    // Set default font for pdf
    $word->setDefaultFontName( 'Helvetica' );
    
    // Add a section to the word document
    $section = $word->addSection();
    
    // Add content to doc section
    $section->addText( 'This is a test' );

    // Add chart to document
    $chart = $section->addChart( 'pie', array('A', 'B', 'C', 'D', 'E'), array(1, 3, 2, 5, 4) );
    $chart->getStyle()->setHeight( Converter::inchToEmu(6.5) )->setWidth( Converter::inchToEmu(6.5) );
    
    // Save docx to local storage
    $writer = IOFactory::createWriter( $word, 'Word2007' );
    $writer->save( $file_path . '.docx' );
    
    // Convert docx to pdf
    Settings::setPdfRendererName( 'DomPDF' );
    Settings::setPdfRendererPath( storage_path() );
    $pdfWriter = IOFactory::createWriter( $word , 'PDF' );
    $pdfWriter->save( $file_path . '.pdf', 'PDF' );
0

There are 0 answers