Skip to content

Commit ba9213b

Browse files
authored
PDF/HTML Writer : Added support (#855)
1 parent 70cf191 commit ba9213b

18 files changed

+884
-37
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"require-dev": {
2929
"phpunit/phpunit": ">=7.0",
3030
"phpmd/phpmd": "2.*",
31-
"phpstan/phpstan": "^0.12.88 || ^1.0.0"
31+
"phpstan/phpstan": "^0.12.88 || ^1.0.0",
32+
"dompdf/dompdf": "^3.1"
3233
},
3334
"suggest": {
3435
"ext-gd": "Required to add images"

docs/changes/1.2.0.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
- `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)
1212
- 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)
1313
- 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)
14+
- 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)
15+
- 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)
1416

1517
## Bug fixes
1618

docs/index.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Documentation.
3535
- Output to different file formats:
3636
- PowerPoint 2007 (.pptx)
3737
- OpenDocument Presentation (.odp)
38+
- HTML (.html)
39+
- PDF (.pdf)
3840
- Serialized Spreadsheet
3941
- ... and lots of other things!
4042

@@ -50,15 +52,15 @@ Below are the supported features for each file formats.
5052
| **Document** | Mark as final | | | | :material-check: |
5153
| **Document Properties** | Standard | | :material-check: | | :material-check: |
5254
| | Custom | | :material-check: | | :material-check: |
53-
| **Slides** | | | :material-check: | | :material-check: |
55+
| **Slides** | | :material-check: | :material-check: | :material-check: | :material-check: |
5456
| | Name | | :material-check: | | |
5557
| **Element Shape** | AutoShape | | | | :material-check: |
56-
| | Image | | :material-check: | | :material-check: |
57-
| | Hyperlink | | :material-check: | | :material-check: |
58+
| | Image | :material-check: | :material-check: | :material-check: | :material-check: |
59+
| | Hyperlink | :material-check: | :material-check: | :material-check: | :material-check: |
5860
| | Line | | :material-check: | | :material-check: |
59-
| | MemoryImage | | :material-check: | | :material-check: |
61+
| | MemoryImage | :material-check: | :material-check: | :material-check: | :material-check: |
6062
| | RichText | | :material-check: | | :material-check: |
61-
| | Table | | :material-check: | | :material-check: |
63+
| | Table | :material-check: | :material-check: | :material-check: | :material-check: |
6264
| | Text | | :material-check: | | :material-check: |
6365
| **Charts** | Area | | :material-check: | | :material-check: |
6466
| | Bar | | :material-check: | | :material-check: |

docs/usage/writers.md

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Writers
22

3+
## HTML
4+
The name of the writer is `HTML`.
5+
6+
``` php
7+
<?php
8+
9+
$writer = IOFactory::createWriter($oPhpPresentation, 'HTML');
10+
$writer->save(__DIR__ . '/sample.html');
11+
```
12+
313
## ODPresentation
414
The name of the writer is `ODPresentation`.
515

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

23+
## PDF
24+
The name of the writer is `PDF`.
25+
26+
``` php
27+
<?php
28+
29+
use PhpOffice\PhpPresentation\Writer\PDF\DomPDF;
30+
31+
$writer = IOFactory::createWriter($oPhpPresentation, 'PDF');
32+
$writer->setPDFAdapter(new DomPDF());
33+
$writer->save(__DIR__ . '/sample.pdf');
34+
```
35+
1336
## PowerPoint2007
1437
The name of the writer is `PowerPoint2007`.
1538

samples/Sample_01_Complex.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,18 @@
4141
$shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
4242

4343
$textRun = $shape->createTextRun('Introduction to');
44-
$textRun->getFont()->setBold(true);
45-
$textRun->getFont()->setSize(28);
46-
$textRun->getFont()->setColor($colorBlack);
44+
$textRun->getFont()
45+
->setBold(true)
46+
->setSize(28)
47+
->setColor($colorBlack);
4748

4849
$shape->createBreak();
4950

5051
$textRun = $shape->createTextRun('PHPPresentation');
51-
$textRun->getFont()->setBold(true);
52-
$textRun->getFont()->setSize(60);
53-
$textRun->getFont()->setColor($colorBlack);
52+
$textRun->getFont()
53+
->setBold(true)
54+
->setSize(60)
55+
->setColor($colorBlack);
5456

5557
// Create templated slide
5658
echo date('H:i:s') . ' Create templated slide' . EOL;

samples/Sample_Header.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpOffice\PhpPresentation\Style\Alignment;
1717
use PhpOffice\PhpPresentation\Style\Bullet;
1818
use PhpOffice\PhpPresentation\Style\Color;
19+
use PhpOffice\PhpPresentation\Writer\PDF\DomPDF;
1920

2021
error_reporting(E_ALL);
2122
define('CLI', (PHP_SAPI == 'cli') ? true : false);
@@ -48,7 +49,12 @@
4849
}
4950

5051
// Set writers
51-
$writers = ['PowerPoint2007' => 'pptx', 'ODPresentation' => 'odp'];
52+
$writers = [
53+
'PowerPoint2007' => 'pptx',
54+
'ODPresentation' => 'odp',
55+
'HTML' => 'html',
56+
'PDF' => 'pdf',
57+
];
5258

5359
// Set titles and names
5460
$pageHeading = str_replace('_', ' ', SCRIPT_FILENAME);
@@ -118,9 +124,13 @@ function write(PhpPresentation $phpPresentation, string $filename, array $writer
118124
foreach ($writers as $writer => $extension) {
119125
$result .= date('H:i:s') . " Write to {$writer} format";
120126
if (null !== $extension) {
121-
$xmlWriter = IOFactory::createWriter($phpPresentation, $writer);
122-
$xmlWriter->save(__DIR__ . "/{$filename}.{$extension}");
123-
rename(__DIR__ . "/{$filename}.{$extension}", __DIR__ . "/results/{$filename}.{$extension}");
127+
$pathFile = __DIR__ . "/results/{$filename}.{$extension}";
128+
if (file_exists($pathFile)) {
129+
unlink($pathFile);
130+
}
131+
$ioWriter = IOFactory::createWriter($phpPresentation, $writer);
132+
$ioWriter->setPDFAdapter(new DomPDF());
133+
$ioWriter->save($pathFile);
124134
} else {
125135
$result .= ' ... NOT DONE!';
126136
}
@@ -188,7 +198,8 @@ function createTemplatedSlide(PhpPresentation $objPHPPresentation): Slide
188198
->setHeight(36)
189199
->setOffsetX(10)
190200
->setOffsetY(10);
191-
$shape->getShadow()->setVisible(true)
201+
$shape->getShadow()
202+
->setVisible(true)
192203
->setDirection(45)
193204
->setDistance(10);
194205

src/PhpPresentation/AbstractShape.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public function setOffsetY(int $pValue = 0)
261261
}
262262

263263
/**
264-
* Get Width.
264+
* Get Width (in pixels).
265265
*
266266
* @return int
267267
*/
@@ -271,7 +271,7 @@ public function getWidth()
271271
}
272272

273273
/**
274-
* Set Width.
274+
* Set Width (in pixels).
275275
*
276276
* @return $this
277277
*/
@@ -283,7 +283,7 @@ public function setWidth(int $pValue = 0)
283283
}
284284

285285
/**
286-
* Get Height.
286+
* Get Height (in pixels).
287287
*
288288
* @return int
289289
*/
@@ -293,7 +293,7 @@ public function getHeight()
293293
}
294294

295295
/**
296-
* Set Height.
296+
* Set Height (in pixels).
297297
*
298298
* @return $this
299299
*/
@@ -369,10 +369,8 @@ public function setShadow(?Shadow $pValue = null)
369369

370370
/**
371371
* Has Hyperlink?
372-
*
373-
* @return bool
374372
*/
375-
public function hasHyperlink()
373+
public function hasHyperlink(): bool
376374
{
377375
return null !== $this->hyperlink;
378376
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPPresentation - A pure PHP library for reading and writing
5+
* presentations documents.
6+
*
7+
* PHPPresentation is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPPresentation
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
declare(strict_types=1);
20+
21+
namespace PhpOffice\PhpPresentation\Exception;
22+
23+
class WriterPDFAdapterNotDefinedException extends PhpPresentationException
24+
{
25+
public function __construct()
26+
{
27+
parent::__construct('The PDF Adapter has not been defined');
28+
}
29+
}

src/PhpPresentation/Shape/Media.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getMimeType(): string
3636

3737
break;
3838
case 'ogv':
39-
$mimetype = 'video/ogg';
39+
$mimetype = 'video/ogv';
4040

4141
break;
4242
case 'wmv':

src/PhpPresentation/Writer/AbstractWriter.php

+18
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use PhpOffice\PhpPresentation\Shape\Chart;
2828
use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter;
2929
use PhpOffice\PhpPresentation\Shape\Group;
30+
use PhpOffice\PhpPresentation\Writer\PDF\PDFWriterInterface;
3031

3132
abstract class AbstractWriter
3233
{
@@ -44,6 +45,11 @@ abstract class AbstractWriter
4445
*/
4546
protected $oPresentation;
4647

48+
/**
49+
* @var null|PDFWriterInterface
50+
*/
51+
protected $pdfAdapter;
52+
4753
/**
4854
* @var null|ZipInterface
4955
*/
@@ -57,6 +63,11 @@ public function getDrawingHashTable(): HashTable
5763
return $this->oDrawingHashTable;
5864
}
5965

66+
public function getPDFAdapter(): ?PDFWriterInterface
67+
{
68+
return $this->pdfAdapter;
69+
}
70+
6071
/**
6172
* Get PhpPresentation object.
6273
*/
@@ -65,6 +76,13 @@ public function getPhpPresentation(): ?PhpPresentation
6576
return $this->oPresentation;
6677
}
6778

79+
public function setPDFAdapter(PDFWriterInterface $pdfAdapter): self
80+
{
81+
$this->pdfAdapter = $pdfAdapter;
82+
83+
return $this;
84+
}
85+
6886
/**
6987
* Get PhpPresentation object.
7088
*

0 commit comments

Comments
 (0)