Php fatal error, uncaught type error when running php script

112 views Asked by At

I am trying to convert presentation file to script using php.

PHP Fatal error: Uncaught TypeError: Argument 3 passed to PhpOffice\PhpPresentation\Reader\PowerPoint2007::loadShapeRichText() must be an instance of PhpOffice\PhpPresentation\Slide\AbstractSlide, instance of PhpOffice\PhpPresentation\Slide\Note given, called in /home/admin/vba/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php on line 1375 and defined in /home/admin/vba/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php:888 Stack trace: #0 /home/admin/vba/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php(1375): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadShapeRichText() #1 /home/admin/vba/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php(755): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadSlideShapes() #2 /home/admin/vba/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php(369): PhpOffice\PhpPresentation\Reader\PowerPoint2007->loadSlideNote() #3 /home/admin/vba/vendor/php in /home/admin/vba/vendor/phpoffice/phppresentation/src/PhpPresentation/Reader/PowerPoint2007.php on line 888

<?php 
require 'vendor/autoload.php'; 
use PhpOffice\PhpPresentation\PhpPresentation; 
use PhpOffice\PhpPresentation\IOFactory;
// Specify the path to your 
// PowerPoint file
$pptxFilePath = '/home/admin/vba/monsoonmalabarpptfin9.pptx';
// Load the PowerPoint file
$presentation = IOFactory::load($pptxFilePath);
// Set the export file path
$exportFilePath = '/home/admin/vba/uu.txt';
// Create a string to store the 
// exported content
$exportedContent = "";
// Loop through slides and extract 
// information
foreach ($presentation->getAllSlides() as $index => $slide) {
    $exportedContent .= "Slide $index:\n"; foreach     ($slide->getShapeCollection()  as $shape) {
        if ($shape->getRichText() !== null) {
            $exportedContent .= $shape->getRichText()->getText() . "\n";
        }
    }
    $exportedContent .= "\n";
}
// Write the exported content to a 
// text file
file_put_contents($exportFilePath, $exportedContent);
echo "Exported content to $exportFilePath\n"; 
?>
1

There are 1 answers

0
Jacob On

The error your receiving is a type mismatch, specifically in your case it seems the error may be related to having notes associated with slides, and they are not handled as traditional slides.

To resolve this you can either remove the notes, or revise your script to handle multiple slide types.

  1. Include additional import statements:

     use PhpOffice\PhpPresentation\Slide\Note;
     use PhpOffice\PhpPresentation\Slide\AbstractSlide;
    
  2. Check slide type:

     // Check if the slide is a standard slide or a notes slide
     if ($slide instanceof AbstractSlide) {
         foreach ($slide->getShapeCollection() as $shape) {
             if ($shape->getRichText() !== null) {
                 $exportedContent .= $shape->getRichText()->getText() . "\n";
             }
         }
     } elseif ($slide instanceof Note) {
         // Handle notes slides differently
     }