Skip to content

generating a PPT file using a template with placeholders getting the error "getIndex() on null" #804

Description

@arulselvan008
public function generatePresentationAction()
    {
        $presentation = new PhpPresentation();
        $productName = "NewProduct";
        $sku = "Test01";
    
        $templatePath = '/home/pim_svc/b.pptx';
        $templatePresentation = IOFactory::createReader('PowerPoint2007')->load($templatePath);
        $masterSlides = $templatePresentation->getAllMasterSlides();
        foreach ($masterSlides as $masterSlide) {
            $slideLayouts = $masterSlide->getAllSlideLayouts();
            
            foreach ($slideLayouts as $slideLayout) {
                $slide = new Slide();
                $slide->setShapeCollection($slideLayout->getShapeCollection());
                $presentation->addSlide($slide);
                $shapes = $slide->getShapeCollection();
                foreach ($shapes as $shape) {
                    if ($shape instanceof RichText) {
                        $text = $shape->getPlainText();

                        if (stripos($text, 'PRODUCTNAME')!== false) {
                            $shape->getActiveParagraph()->getRichTextElements()[0]->setText(str_replace('PRODUCTNAME', $productName, $text));
                        }
                        // if (stripos($text, 'SKU')!== false) {
                        //     $shape->getActiveParagraph()->getRichTextElements()[0]->setText(str_replace('SKU', $sku, $text));
                        // }
                    }
                }
            }
        }
        
        $filename = "newOutPUT".'.pptx';
        // $tmpFilename = sys_get_temp_dir(). '/'. $filename;
        $writer = IOFactory::createWriter($presentation, 'PowerPoint2007');
        $writer->save($filename);
        
        $response = new BinaryFileResponse($tmpFilename);
        $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename);
    
        return $response;
    }

For your reference my code above, I am trying to generate a PPT file based on a sample template with placeholders, but I am getting an error: "Call to a member function getIndex() on null." How can I fix this? Can anyone help me resolve this issue?

Error
b.pptx
b.pptx

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions