PHPOffice / PHPPresentation

A pure PHP library for reading and writing presentations documents
https://phpoffice.github.io/PHPPresentation
Other
1.31k stars 523 forks source link

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

Open arulselvan008 opened 5 months ago

arulselvan008 commented 5 months ago
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

arulselvan008 commented 5 months ago

Hi, does anyone have an idea about these queries? Please help us resolve the issue.