1

I have plenty of ppt and pptx files that I am trying to convert to pdf using PHP. Basing on PHPOffice I have able to convert xlsx or xls to pdf, when it comes to ppt or pptx I am facing difculities as I am assuing its not as much reliable in that sense in comparision to PHPSpreadsheet as its PHPPresentation/Writer directory is very limited and is not rich as PHPSpreadsheet.

So I tried to use MPDF or Dompdf to help overcome those limitations of PHPPresentation library came with the below code utilizing imagick as well.

$objReader = PresentationIOFactory::createReader('PowerPoint2007');
        $objPHPPresentation = $objReader->load($pf);

        // Create a new Imagick object for image manipulation
        $imagick = new \Imagick();

        // Loop through each slide and convert it to an image
        $slideCount = $objPHPPresentation->getSlideCount();
        for ($i = 0; $i < $slideCount; $i++) {
            $oSlide = $objPHPPresentation->getSlide($i);
            $oDrawing = new Drawing\Gd();
            $oDrawing->setRenderingFunction(Drawing\Gd::RENDERING_PNG);
            $oDrawing->setImageResource($oSlide->render());
            $oDrawing->setSize($oSlide->getExtent()->getWidth(), $oSlide->getExtent()->getHeight());
            $oDrawing->setMimeType(Drawing\Gd::MIMETYPE_DEFAULT);
            $imageString = $oDrawing->getContents();

            // Add the image to the Imagick object
            $imagick->readImageBlob($imageString);
        }

        // Create a new mPDF object
        $mpdf = new MpdfLib([
            'mode' => 'utf-8',
            'format' => [$objPHPPresentation->getLayout()->getDocument()->getDocumentPart()->getExtent()->getWidth(), $objPHPPresentation->getLayout()->getDocument()->getDocumentPart()->getExtent()->getHeight()]
        ]);

        // Add the slide images to the mPDF object
        $imagick->resetIterator();
        foreach ($imagick as $index => $slideImage) {
            // Output slide image as a base64-encoded data URI
            $dataUri = 'data:image/png;base64,' . base64_encode($slideImage->getImageBlob());

            // Add the slide image to the mPDF object
            $mpdf->AddPage();
            $mpdf->WriteHTML('<img src="' . $dataUri . '" />');
        }

        // Save the PDF
        $mpdf->Output($outputFilePath, 'F');

        echo "PowerPoint file has been converted to PDF.";

The main issue that I am facing now is reading pptx and ppt files as I get an error that ppt file is not compatible any ideas how to overcome this issue?

As when I tried pptx with creating a 'PowerPoint2007' reader as can be seen in the above code I got the below error, any idea what I am doing wrong?

PPTX File Load Error:

Fatal error: Uncaught TypeError:PhpOffice\PhpPresentation\Reader\PowerPoint2007::loadShapeRichText(): Argument #3 ($oSlide) must be of type PhpOffice\PhpPresentation\Slide\AbstractSlide, PhpOffice\PhpPresentation\Slide\Note given, called in C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php on line 1375 and defined in C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php:888
    Stack trace:
    #0 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php(1375): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadShapeRichText(Object(PhpOffice\Common\XMLReader), Object(DOMElement), Object(PhpOffice\PhpPresentation\Slide\Note))
    #1 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php(755): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadSlideShapes(Object(PhpOffice\PhpPresentation\Slide\Note), Object(DOMNodeList), Object(PhpOffice\Common\XMLReader))
    #2 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php(369): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadSlideNote('notesSlide1.xml', Object(PhpOffice\PhpPresentation\Slide))
    #3 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php(169): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadSlides('<?xml version="...')
    #4 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php(136): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadFile('C:/xampp/htdocs...')
    #5 C:\xampp\htdocs\a\create_thumbnail.php(70): PhpOffice\PhpPresentation\Reader\PowerPoint2007->load('C:/xampp/htdocs...')
    #6 C:\xampp\htdocs\a\create_thumbnail.php(142): getThumbnail('../uploads/pptx...', 'Results Discuss...', 'C:/xampp/htdocs...', '../uploads/tmp/', 'pptx', '126', Object(mysqli))
    #7 {main}
      thrown in C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint2007.php on line 888

PPT File Load Error:

Fatal error: Uncaught TypeError: 

PhpOffice\Common\Microsoft\OLERead::getStream(): Argument #1 ($stream) must be of type int, null given, called in C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint97.php on line 487 and defined in C:\xampp\htdocs\vendor\phpoffice\common\src\Common\Microsoft\OLERead.php:203
    Stack trace:
    #0 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint97.php(487): PhpOffice\Common\Microsoft\OLERead->getStream(NULL)
    #1 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint97.php(454): PhpOffice\PhpPresentation\Reader\PowerPoint97->loadOLE()
    #2 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\Reader\PowerPoint97.php(442): PhpOffice\PhpPresentation\Reader\PowerPoint97->loadFile()
    #3 C:\xampp\htdocs\vendor\phpoffice\phppresentation\src\PhpPresentation\IOFactory.php(73): PhpOffice\PhpPresentation\Reader\PowerPoint97->load('C:/xampp/htdocs...')
    #4 C:\xampp\htdocs\a\create_thumbnail.php(112): PhpOffice\PhpPresentation\IOFactory::load('C:/xampp/htdocs...')
    #5 C:\xampp\htdocs\a\create_thumbnail.php(177): getThumbnail('../uploads/ppt/...', 'Architecture_xx...', 'C:/xampp/htdocs...', '../uploads/tmp/', 'ppt', '139', Object(mysqli))
    #6 {main}
      thrown in C:\xampp\htdocs\vendor\phpoffice\common\src\Common\Microsoft\OLERead.php on line 203

I have been struggling with this for some time, I would appreciate your help and if there is any workaround (considering that the PHP script shall be running on a shared-hosting).

UPDATE (Partial Fix - PPTX):

I was able to partially fix the issue, the part concerning loading PPTX files through the below changes (Reference Issue #710):

vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php

protected function loadShapeRichText(XMLReader $document, DOMElement $node, AbstractSlide $oSlide): void
 {
        if (!$document->elementExists('p:txBody/a:p/a:r', $node)) {
            return;
        }

replace to

protected function loadShapeRichText(XMLReader $document, DOMElement $node, $oSlide): void
{
    if (!$document->elementExists('p:txBody/a:p/a:r', $node) || !$oSlide instanceof AbstractSlide) {
        return;
    }

in vendor/phpoffice/phppresentation/src/PhpPresentation/Style/Border.php

 public function setLineWidth(int $pValue = 1): self
    {
        $this->lineWidth =  $pValue;
    }

replace to

 public function setLineWidth($pValue = 1): self
    {
        $this->lineWidth = (int) $pValue;
    }

Still struggling with the PPT files part as per the error shared earlier, kindly advise.

Appreciate your time, thanks.

Osama Hussein
  • 69
  • 2
  • 8

0 Answers0