PHPPresentation icon indicating copy to clipboard operation
PHPPresentation copied to clipboard

Save with php://output broke my file

Open GandonNicolas opened this issue 3 years ago • 3 comments

Hi here,

When i save my file with this method, i can still open my file normally :

$oWriter = IOFactory::createWriter($powerPoint); $oWriter->save($this->appKernel->getProjectDir() . "/data/test.pptx");

but if i try to save with php://output, document it always needs repairing to open it : $oWriter->save('php://output'); I work with PhpPresentation 1.0.0 MacOS BigSur

Does anyone have the same problem?

GandonNicolas avatar Mar 31 '22 11:03 GandonNicolas

I had this issue not that long ago. The way I fixed it was that I ran ob_start and ob_clean() before saving to the buffer and then getting the buffer and putting it in a variable and then ending the buffer clean

Like so:

ob_start();
ob_clean();
$writerPPTX->save('php://output');
$contents = ob_get_contents();
ob_end_clean();

Then you can return the $contents. Remember to set the correct headers

header('Content-Type', 'application/vnd.openxmlformats-officedocument.presentationml.presentation') header('Content-Disposition', 'attachment;filename=presentation.pptx') header('Cache-Control', 'max-age=0');

GisliNielsen avatar Apr 01 '22 07:04 GisliNielsen

Hi @GisliNielsen, thank you for your reply!

I tried your way of doing things, but I'm not sure I'm doing it right. Here is my code:

        $powerPoint = $this->powerPoint->createPowerPoint($_SESSION['resulat']);

        header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation');
        header('Content-Disposition: attachment;filename="'.$date.'-CR-TMA-'.$_SESSION['resulat'][0]['project_name'].'.pptx"');
        header('Cache-Control: max-age=0');

        $oWriter = IOFactory::createWriter($powerPoint);
        ob_start();
        ob_clean();
        $oWriter->save('php://output');
        ob_end_clean();

With this method in a first time a warning message tells me that "the file still needs to be repaired" then in a second time a message tells me that "powerpoint can't read the file"

GandonNicolas avatar Apr 04 '22 12:04 GandonNicolas

UPDATE

I added exit() at the end of my code and it works.

header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation', true);
        header('Content-Disposition: attachment;filename="'.$date.'-CR-TMA-'.$_SESSION['resulat'][0]['project_name'].'.pptx"');

        $oWriter = IOFactory::createWriter($powerPoint);
        $oWriter->save('php://output');
        exit();

GandonNicolas avatar Apr 04 '22 14:04 GandonNicolas

Thanks for help @GisliNielsen , and answer @GandonNicolas .

Progi1984 avatar Dec 30 '22 20:12 Progi1984