Creare presentazioni PowerPoint con PHP e PHPOffice
PHPOffice è una libreria (di cui abbiamo parlato già varie volte) che ci permette di manipolare file Office usando PHP.
E parliamo sia di MS Office, che di programmi come LibreOffice.
Oggi vediamo come creare presentazioni PowerPoint, sia in formato PPTX che ODP.
Prima di tutto installiamo la libreria; io ho usato composer:
$ composer require phpoffice/phppresentation
Questo il codice PHP di esempio:
require_once './vendor/autoload.php';
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Alignment;
$phpPres = new PhpPresentation();
$currentSlide = $phpPres->getActiveSlide();
$shapeImg = $currentSlide->createDrawingShape();
$shapeImg->setName('PHPPresentation logo')
->setDescription('PHPPresentation logo')
->setPath('./img.png')
->setHeight(36)
->setOffsetX(10)
->setOffsetY(10);
$shapeImg->getShadow()->setVisible(true)
->setDirection(45)
->setDistance(10);
$shapeText = $currentSlide->createRichTextShape()
->setHeight(300)
->setWidth(600)
->setOffsetX(170)
->setOffsetY(180);
$shapeText->getActiveParagraph()->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_CENTER);
$textRun = $shapeText->
createTextRun('Creiamo presentazioni in PHP con PhpPresentation');
$textRun->getFont()->setBold(true)->setSize(50)->setColor(new Color('FFE06B20'));
//$file = 'file.pptx';
$file = 'file.odp';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
//$objWriter = IOFactory::createWriter($phpPres, 'PowerPoint2007');
$objWriter = IOFactory::createWriter($phpPres, 'ODPresentation');
$objWriter->save('php://output');
Come potete vedere, nella parte finale, vi ho messo due esempi per entrambi i formati.
Rimane tutto uguale, a parte la scelta del formato.
Così facendo, mandiamo il file in output sul browser.
Enjoy!
php phpoffice phppresentation powerpoint pptx odp composer
Commentami!