Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDF/HTML Writer : Added support #855

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"require-dev": {
"phpunit/phpunit": ">=7.0",
"phpmd/phpmd": "2.*",
"phpstan/phpstan": "^0.12.88 || ^1.0.0"
"phpstan/phpstan": "^0.12.88 || ^1.0.0",
"dompdf/dompdf": "^3.1"
},
"suggest": {
"ext-gd": "Required to add images"
Expand Down
2 changes: 2 additions & 0 deletions docs/changes/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- `createAutoShape` : Add method to create geometric shapes by [@mhasanshahid](https://github.com/mhasanshahid) & [@Progi1984](https://github.com/Progi1984) in [#848](https://github.com/PHPOffice/PHPPresentation/pull/848)
- Reader : Option to not load images by [@Progi1984](https://github.com/Progi1984) fixing [#795](https://github.com/PHPOffice/PHPPresentation/issues/795) in [#850](https://github.com/PHPOffice/PHPPresentation/pull/850)
- ODPresentation Writer : Support for rotation for RichText by [@Progi1984](https://github.com/Progi1984) fixing [#279](https://github.com/PHPOffice/PHPPresentation/pull/279) in [#409](https://github.com/PHPOffice/PHPPresentation/pull/409)
- HTML Writer by [@Progi1984](https://github.com/Progi1984) fixing [#221](https://github.com/PHPOffice/PHPPresentation/pull/221), [#567](https://github.com/PHPOffice/PHPPresentation/pull/567), [#644](https://github.com/PHPOffice/PHPPresentation/pull/644) in [#850](https://github.com/PHPOffice/PHPPresentation/pull/855)
- PDF Writer by [@Progi1984](https://github.com/Progi1984) fixing [#181](https://github.com/PHPOffice/PHPPresentation/pull/181), [#381](https://github.com/PHPOffice/PHPPresentation/pull/381), [#472](https://github.com/PHPOffice/PHPPresentation/pull/472), [#693](https://github.com/PHPOffice/PHPPresentation/pull/693), [#725](https://github.com/PHPOffice/PHPPresentation/pull/725) in [#850](https://github.com/PHPOffice/PHPPresentation/pull/855)

## Bug fixes

Expand Down
12 changes: 7 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Documentation.
- Output to different file formats:
- PowerPoint 2007 (.pptx)
- OpenDocument Presentation (.odp)
- HTML (.html)
- PDF (.pdf)
- Serialized Spreadsheet
- ... and lots of other things!

Expand All @@ -50,15 +52,15 @@ Below are the supported features for each file formats.
| **Document** | Mark as final | | | | :material-check: |
| **Document Properties** | Standard | | :material-check: | | :material-check: |
| | Custom | | :material-check: | | :material-check: |
| **Slides** | | | :material-check: | | :material-check: |
| **Slides** | | :material-check: | :material-check: | :material-check: | :material-check: |
| | Name | | :material-check: | | |
| **Element Shape** | AutoShape | | | | :material-check: |
| | Image | | :material-check: | | :material-check: |
| | Hyperlink | | :material-check: | | :material-check: |
| | Image | :material-check: | :material-check: | :material-check: | :material-check: |
| | Hyperlink | :material-check: | :material-check: | :material-check: | :material-check: |
| | Line | | :material-check: | | :material-check: |
| | MemoryImage | | :material-check: | | :material-check: |
| | MemoryImage | :material-check: | :material-check: | :material-check: | :material-check: |
| | RichText | | :material-check: | | :material-check: |
| | Table | | :material-check: | | :material-check: |
| | Table | :material-check: | :material-check: | :material-check: | :material-check: |
| | Text | | :material-check: | | :material-check: |
| **Charts** | Area | | :material-check: | | :material-check: |
| | Bar | | :material-check: | | :material-check: |
Expand Down
23 changes: 23 additions & 0 deletions docs/usage/writers.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Writers

## HTML
The name of the writer is `HTML`.

``` php
<?php

$writer = IOFactory::createWriter($oPhpPresentation, 'HTML');
$writer->save(__DIR__ . '/sample.html');
```

## ODPresentation
The name of the writer is `ODPresentation`.

Expand All @@ -10,6 +20,19 @@ $writer = IOFactory::createWriter($oPhpPresentation, 'PowerPoint2007');
$writer->save(__DIR__ . '/sample.pptx');
```

## PDF
The name of the writer is `PDF`.

``` php
<?php

use PhpOffice\PhpPresentation\Writer\PDF\DomPDF;

$writer = IOFactory::createWriter($oPhpPresentation, 'PDF');
$writer->setPDFAdapter(new DomPDF());
$writer->save(__DIR__ . '/sample.pdf');
```

## PowerPoint2007
The name of the writer is `PowerPoint2007`.

Expand Down
14 changes: 8 additions & 6 deletions samples/Sample_01_Complex.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@
$shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);

$textRun = $shape->createTextRun('Introduction to');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(28);
$textRun->getFont()->setColor($colorBlack);
$textRun->getFont()
->setBold(true)
->setSize(28)
->setColor($colorBlack);

$shape->createBreak();

$textRun = $shape->createTextRun('PHPPresentation');
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(60);
$textRun->getFont()->setColor($colorBlack);
$textRun->getFont()
->setBold(true)
->setSize(60)
->setColor($colorBlack);

// Create templated slide
echo date('H:i:s') . ' Create templated slide' . EOL;
Expand Down
21 changes: 16 additions & 5 deletions samples/Sample_Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Style\Bullet;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Writer\PDF\DomPDF;

error_reporting(E_ALL);
define('CLI', (PHP_SAPI == 'cli') ? true : false);
Expand Down Expand Up @@ -48,7 +49,12 @@
}

// Set writers
$writers = ['PowerPoint2007' => 'pptx', 'ODPresentation' => 'odp'];
$writers = [
'PowerPoint2007' => 'pptx',
'ODPresentation' => 'odp',
'HTML' => 'html',
'PDF' => 'pdf',
];

// Set titles and names
$pageHeading = str_replace('_', ' ', SCRIPT_FILENAME);
Expand Down Expand Up @@ -118,9 +124,13 @@ function write(PhpPresentation $phpPresentation, string $filename, array $writer
foreach ($writers as $writer => $extension) {
$result .= date('H:i:s') . " Write to {$writer} format";
if (null !== $extension) {
$xmlWriter = IOFactory::createWriter($phpPresentation, $writer);
$xmlWriter->save(__DIR__ . "/{$filename}.{$extension}");
rename(__DIR__ . "/{$filename}.{$extension}", __DIR__ . "/results/{$filename}.{$extension}");
$pathFile = __DIR__ . "/results/{$filename}.{$extension}";
if (file_exists($pathFile)) {
unlink($pathFile);
}
$ioWriter = IOFactory::createWriter($phpPresentation, $writer);
$ioWriter->setPDFAdapter(new DomPDF());
$ioWriter->save($pathFile);
} else {
$result .= ' ... NOT DONE!';
}
Expand Down Expand Up @@ -188,7 +198,8 @@ function createTemplatedSlide(PhpPresentation $objPHPPresentation): Slide
->setHeight(36)
->setOffsetX(10)
->setOffsetY(10);
$shape->getShadow()->setVisible(true)
$shape->getShadow()
->setVisible(true)
->setDirection(45)
->setDistance(10);

Expand Down
12 changes: 5 additions & 7 deletions src/PhpPresentation/AbstractShape.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function setOffsetY(int $pValue = 0)
}

/**
* Get Width.
* Get Width (in pixels).
*
* @return int
*/
Expand All @@ -271,7 +271,7 @@ public function getWidth()
}

/**
* Set Width.
* Set Width (in pixels).
*
* @return $this
*/
Expand All @@ -283,7 +283,7 @@ public function setWidth(int $pValue = 0)
}

/**
* Get Height.
* Get Height (in pixels).
*
* @return int
*/
Expand All @@ -293,7 +293,7 @@ public function getHeight()
}

/**
* Set Height.
* Set Height (in pixels).
*
* @return $this
*/
Expand Down Expand Up @@ -369,10 +369,8 @@ public function setShadow(?Shadow $pValue = null)

/**
* Has Hyperlink?
*
* @return bool
*/
public function hasHyperlink()
public function hasHyperlink(): bool
{
return null !== $this->hyperlink;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* This file is part of PHPPresentation - A pure PHP library for reading and writing
* presentations documents.
*
* PHPPresentation is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
*
* @see https://github.com/PHPOffice/PHPPresentation
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

declare(strict_types=1);

namespace PhpOffice\PhpPresentation\Exception;

class WriterPDFAdapterNotDefinedException extends PhpPresentationException
{
public function __construct()
{
parent::__construct('The PDF Adapter has not been defined');
}
}
2 changes: 1 addition & 1 deletion src/PhpPresentation/Shape/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getMimeType(): string

break;
case 'ogv':
$mimetype = 'video/ogg';
$mimetype = 'video/ogv';

break;
case 'wmv':
Expand Down
18 changes: 18 additions & 0 deletions src/PhpPresentation/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use PhpOffice\PhpPresentation\Shape\Chart;
use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter;
use PhpOffice\PhpPresentation\Shape\Group;
use PhpOffice\PhpPresentation\Writer\PDF\PDFWriterInterface;

abstract class AbstractWriter
{
Expand All @@ -44,6 +45,11 @@ abstract class AbstractWriter
*/
protected $oPresentation;

/**
* @var null|PDFWriterInterface
*/
protected $pdfAdapter;

/**
* @var null|ZipInterface
*/
Expand All @@ -57,6 +63,11 @@ public function getDrawingHashTable(): HashTable
return $this->oDrawingHashTable;
}

public function getPDFAdapter(): ?PDFWriterInterface
{
return $this->pdfAdapter;
}

/**
* Get PhpPresentation object.
*/
Expand All @@ -65,6 +76,13 @@ public function getPhpPresentation(): ?PhpPresentation
return $this->oPresentation;
}

public function setPDFAdapter(PDFWriterInterface $pdfAdapter): self
{
$this->pdfAdapter = $pdfAdapter;

return $this;
}

/**
* Get PhpPresentation object.
*
Expand Down
Loading