PHPOffice / PHPPresentation

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

When updating pptx, it removes texts #731

Open soth78 opened 1 year ago

soth78 commented 1 year ago

Hi!

I'm trying to use this code to open a template pptx, add some images, and write an output text.

The issue I'm seeing is that any texts from the original PPTX are removed. Background stays, style stays, and other images stay as well, but original texts are removed. Could someone help me please?

$filepath = 'includes/template2.pptx';
$outputFilepath = 'data/presentation-' .$id .".pptx";
@unlink($outputFilepath);
$pptReader = IOFactory::createReader('PowerPoint2007');
$presentation = $pptReader->load($filepath);

// INTRODUCTION
$currentSlide = $presentation->getSlide(1);

$shape = $currentSlide->createDrawingShape();
$shape->setName('My Image')
    ->setDescription('This is an example image')
    ->setPath('data/ages_chart-' .$id .'.png')
    ->setHeight(200)
    ->setResizeProportional(true)
    ->setOffsetX(100)
    ->setOffsetY(100);

// Create a shape (text)
$shape = $currentSlide->createRichTextShape()
    ->setHeight(300)
    ->setWidth(600)
    ->setOffsetX(170)
    ->setOffsetY(100);
$shape->getActiveParagraph()->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_CENTER);
$shape->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE));
$textRun = $shape->createTextRun('Thank you for using PHPPresentation!');
$textRun->getFont()->setBold(true)
    ->setSize(60)
    ->setColor(new Color(Color::COLOR_WHITE));

// EXPOSURE
$currentSlide = $presentation->getSlide(1);

$shape = $currentSlide->createDrawingShape();
$shape->setName('Exposure Graph')
    ->setDescription('Graph with stolen credentials over time')
    ->setPath('data/infection_chart-' .$id .'.png')
    ->setWidth(900)
    ->setResizeProportional(true)
    ->setOffsetX(10)
    ->setOffsetY(220);

$writer = IOFactory::createWriter($presentation, 'PowerPoint2007');
$writer->save($outputFilepath);

header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($outputFilepath) . "\""); 
readfile($outputFilepath); 
Progi1984 commented 3 weeks ago

@soth78 Hi, Could you send me a sample file with error, please, for reproducing the bug ?