Generating PowerPoint Presentations with PHPPresentation Library

Overview

The PHPPresentation library enables PHP developers to create and export PowerPoint presentations programmatically. This functionality provides an effectiev solution for generating dynamic presentation content from web applications.

Prerequisites

Ensure PHP version 7.1 or higher is installed on your system.

Installation Process

Using Composer

Install the PHPPresentation package via Composer:

composer require phpoffice/phppresentation dev-develop

Note that the master branch has been inactive for three years, while the develop branch continues active maintenance with recent bug fixes.

Documentation Reference

Documentation can be found at: https://phppowerpoint.readthedocs.io/en/latest/

Environment Setup

Sample Testing

Begin by examining the examples located in the PHPPresentation/samples directory. These samples help configure your environment and demonstrate various features.

When running Sample_04_Table.php, you may encounter an error regarding missing Common\Adapter\Zip\ZipArchiveAdapter. The required Common directory can be found at https://github.com/PHPOffice/Common.

Integration Examples

Basic Presentation Creation

Initialize a new presentation object and configure slide dimensions:

// Create new presentation instance
$presenter = new \PhpPresentation\PhpPresentation();
$layout = new \PhpPresentation\DocumentLayout();

// Set custom dimensions in points
$layout->setDocumentLayout(\PhpPresentation\DocumentLayout::LAYOUT_CUSTOM, true)
    ->setCX(959, \PhpPresentation\DocumentLayout::UNIT_POINT)
    ->setCY(540, \PhpPresentation\DocumentLayout::UNIT_POINT);

// Alternative: Set dimensions in pixels
$layout->setDocumentLayout(\PhpPresentation\DocumentLayout::LAYOUT_CUSTOM, true)
    ->setCX(1280, \PhpPresentation\DocumentLayout::UNIT_PIXEL)
    ->setCY(720, \PhpPresentation\DocumentLayout::UNIT_PIXEL);

// Set dimensions in centimeters
$layout->setDocumentLayout(\PhpPresentation\DocumentLayout::LAYOUT_CUSTOM, true)
    ->setCX(33.87, \PhpPresentation\DocumentLayout::UNIT_CENTIMETER)
    ->setCY(19.05, \PhpPresentation\DocumentLayout::UNIT_CENTIMETER);

// Apply layout and remove default first slide
$presenter->setLayout($layout);
$presenter->removeSlideByIndex(0);

Creating Template-Based Slides

Create slides with custom backgrounds using template images:

public function generateTemplateSlide(\PhpPresentation\PhpPresentation $presenter, $slideTitle) {
    // Create new slide
    $currentSlide = $presenter->createSlide();
    
    // Configure background image
    $backgroundImage = new \PhpPresentation\Slide\Background\Image();
    $backgroundImage->setPath('./Public/Images/ppt_template.png');
    $currentSlide->setBackground($backgroundImage);
    
    // Create title text element
    $titleElement = $currentSlide->createRichTextShape()
        ->setHeight(60)
        ->setWidth(1200)
        ->setOffsetX(60)
        ->setOffsetY(20);
        
    $titleElement->getActiveParagraph()->getAlignment()
        ->setHorizontal(\PhpPresentation\Style\Alignment::HORIZONTAL_LEFT);
        
    $textBlock = $titleElement->createTextRun($slideTitle);
    $textBlock->getFont()->setBold(true)
        ->setSize(32)
        ->setColor(new \PhpPresentation\Style\Color('FFB60005'));
        
    return $currentSlide;
}

Building Data Tables

Generate tables with formatted content:

// Initialize table with specified column count
$tableElement = $presenter->createTableShape(2);

// Configure table dimensions and position
$tableElement->setHeight(300)->setWidth(800)->setOffsetX(120)->setOffsetY(120);

// Create header row
$headerRow = $tableElement->createRow();
$firstCell = $headerRow->nextCell();
$firstCell->setWidth(202);
$headerText = $firstCell->createTextRun('Header One');
$headerText->getFont()->setSize(16)->setBold(true)
    ->setColor(new \PhpPresentation\Style\Color('FFFFFFFF'));

$secondCell = $headerRow->nextCell();
$secondCell->setWidth(104);
$headerText2 = $secondCell->createTextRun('Header Two');
$headerText2->getFont()->setSize(16)->setBold(true)
    ->setColor(new \PhpPresentation\Style\Color('FFFFFFFF'));

// Configure row appearance
$headerRow->setHeight(40);
$headerRow->getFill()->setFillType(\PhpPresentation\Style\Fill::FILL_SOLID)
    ->setStartColor(new \PhpPresentation\Style\Color('FFB60005'))
    ->setEndColor(new \PhpPresentation\Style\Color('FFB60005'));

// Format all cells in row
foreach ($headerRow->getCells() as $cell) {
    // Remove borders
    $cell->getBorders()->getTop()
        ->setColor(new \PhpPresentation\Style\Color('FFFFFFFF'));
    
    // Center text alignment
    $cell->getActiveParagraph()->getAlignment()
        ->setHorizontal(\PhpPresentation\Style\Alignment::HORIZONTAL_CENTER)
        ->setVertical(\PhpPresentation\Style\Alignment::VERTICAL_CENTER);
}

Creating Line Charts

Implement line charts with multiple data series:

// Define chart data
$chartData = [
    'series1' => [
        'Day 1' => 2, 'Day 2' => 5, 'Day 3' => 3, 
        'Day 4' => 7, 'Day 5' => 4, 'Day 6' => 9, 'Day 7' => 7
    ],
    'series2' => [
        'Day 1' => 12, 'Day 2' => 15, 'Day 3' => 13, 
        'Day 4' => 17, 'Day 5' => 14, 'Day 6' => 19, 'Day 7' => 17
    ]
];

// Create chart element
$chartElement = $presenter->createChartShape();
$chartElement->setResizeProportional(false)
    ->setHeight(260)->setWidth(880)
    ->setOffsetX(80)->setOffsetY(280);
$chartElement->getTitle()->setVisible(false);

// Configure legend
$chartElement->getLegend()->setVisible(true);
$chartElement->getLegend()->getFont()->setSize(12);
$chartElement->getLegend()->getBorder()
    ->setLineStyle(\PhpPresentation\Style\Border::LINE_NONE);

// Set axis styling
$xAxisStyle = new \PhpPresentation\Style\Outline();
$xAxisStyle->getFill()->setFillType(\PhpPresentation\Style\Fill::FILL_SOLID)
    ->setStartColor(new \PhpPresentation\Style\Color('FF808080'));
    
$yAxisStyle = new \PhpPresentation\Style\Outline();
$yAxisStyle->getFill()->setFillType(\PhpPresentation\Style\Fill::FILL_SOLID)
    ->setStartColor(new \PhpPresentation\Style\Color('FF808080'));

$chartElement->getPlotArea()->getAxisX()->setOutline($xAxisStyle);
$chartElement->getPlotArea()->getAxisY()->setOutline($yAxisStyle);

// Configure grid lines
$gridLines = new \PhpPresentation\Shape\Chart\Gridlines();
$gridLines->getOutline()->setWidth(10);
$gridLines->getOutline()->getFill()->setFillType(\PhpPresentation\Style\Fill::FILL_SOLID)
    ->setStartColor(new \PhpPresentation\Style\Color('FF808080'));
$chartElement->getPlotArea()->getAxisY()->setMajorGridlines($gridLines);

// Add data series
$colors = ['FFB60005', 'FFF08080', 'FF808080', 'FF4169E1', 'FFFF8C00'];
$markers = ['diamond', 'square', 'triangle', 'x', 'star'];

$index = 0;
$lineType = new \PhpPresentation\Shape\Chart\Type\Line();

foreach($chartData as $seriesName => $values) {
    $dataSeries = new \PhpPresentation\Shape\Chart\Series($seriesName, $values);
    $dataSeries->setShowSeriesName(false);
    $dataSeries->setShowValue(false);
    $dataSeries->setShowLeaderLines(false);
    
    // Configure markers
    $dataSeries->getMarker()->setSymbol($markers[$index]);
    if(in_array($markers[$index], ['circle', 'square', 'diamond', 'triangle'])) {
        $dataSeries->getMarker()->getFill()->setFillType(\PhpPresentation\Style\Fill::FILL_SOLID);
        $dataSeries->getMarker()->getFill()->setStartColor(new \PhpPresentation\Style\Color($colors[$index]));
    }
    $dataSeries->getMarker()->getBorder()->setColor(new \PhpPresentation\Style\Color($colors[$index]));
    $dataSeries->getMarker()->setSize(5);
    
    // Configure line colors
    $outlineStyle = new \PhpPresentation\Style\Outline();
    $outlineStyle->getFill()->setFillType(\PhpPresentation\Style\Fill::FILL_SOLID);
    $outlineStyle->getFill()->setStartColor(new \PhpPresentation\Style\Color($colors[$index]));
    $outlineStyle->setWidth(150000000);
    $dataSeries->setOutline($outlineStyle);
    
    $lineType->addSeries($dataSeries);
    $index++;
}

$chartElement->getPlotArea()->setType($lineType);

Exporting Presentations

Download the generated presentation file:

$filename = "presentation.pptx";
header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation");
header("Content-Disposition: attachment; filename=$filename");

$writer = \PhpPresentation\IOFactory::createWriter($presenter, 'PowerPoint2007');
$writer->save('php://output');

unlink($filename);
exit;

Tags: PHP PowerPoint phppresentation export documentation

Posted on Sun, 02 Aug 2026 16:41:52 +0000 by veronicabend