Skip to content

Commit 442e612

Browse files
SailorMaxPowerKiKi
authored andcommitted
Support custom PDF library instances or configurations
This allow to create and configure the standard instance of the external PDF libary, before returning it to the standard writer. Or, more powerful, this allow to provide a custom implementation of the external PDF library, allowing for custom behaviors. An example of that would something like: https://tcpdf.org/examples/example_003/ Closes #266
1 parent 40efcd2 commit 442e612

File tree

5 files changed

+66
-3
lines changed

5 files changed

+66
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212
- Support for chart fill color - @CrazyBite [#158](https://github.com/PHPOffice/PhpSpreadsheet/pull/158)
1313
- Support for read Hyperlink for xml - @GreatHumorist [#223](https://github.com/PHPOffice/PhpSpreadsheet/pull/223)
1414
- Support for cell value validation according to data validation rules - @SailorMax [#257](https://github.com/PHPOffice/PhpSpreadsheet/pull/257)
15+
- Support for custom implementation, or configuration, of PDF libraries - @SailorMax [#266](https://github.com/PHPOffice/PhpSpreadsheet/pull/266)
1516

1617
### Changed
1718

docs/topics/reading-and-writing-to-file.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,32 @@ Or you can instantiate directly the writer of your choice like so:
779779
$writer = \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
780780
```
781781
782+
#### Custom implementation or configuration
783+
784+
If you need a custom implementation, or custom configuration, of a supported
785+
PDF library. You can extends the PDF library, and the PDF writer like so:
786+
787+
``` php
788+
class My_Custom_TCPDF extends TCPDF
789+
{
790+
// ...
791+
}
792+
793+
class My_Custom_TCPDF_Writer extends \PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf
794+
{
795+
protected function createExternalWriterInstance($orientation, $unit, $paperSize)
796+
{
797+
$instance = new My_Custom_TCPDF($orientation, $unit, $paperSize);
798+
799+
// more configuration of $instance
800+
801+
return $instance;
802+
}
803+
}
804+
805+
\PhpOffice\PhpSpreadsheet\IOFactory::registerWriter('Pdf', MY_TCPDF_WRITER::class);
806+
```
807+
782808
#### Writing a spreadsheet
783809
784810
Once you have identified the Renderer that you wish to use for PDF

src/PhpSpreadsheet/Writer/Pdf/Dompdf.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88
class Dompdf extends Pdf
99
{
10+
/**
11+
* Gets the implementation of external PDF library that should be used.
12+
*
13+
* @return \Dompdf\Dompdf implementation
14+
*/
15+
protected function createExternalWriterInstance()
16+
{
17+
return new \Dompdf\Dompdf();
18+
}
19+
1020
/**
1121
* Save Spreadsheet to file.
1222
*
@@ -50,7 +60,7 @@ public function save($pFilename)
5060
}
5161

5262
// Create PDF
53-
$pdf = new \Dompdf\Dompdf();
63+
$pdf = $this->createExternalWriterInstance();
5464
$pdf->setPaper(strtolower($paperSize), $orientation);
5565

5666
$pdf->loadHtml(

src/PhpSpreadsheet/Writer/Pdf/Mpdf.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88

99
class Mpdf extends Pdf
1010
{
11+
/**
12+
* Gets the implementation of external PDF library that should be used.
13+
*
14+
* @param array $config Configuration array
15+
*
16+
* @return \Mpdf\Mpdf implementation
17+
*/
18+
protected function createExternalWriterInstance($config)
19+
{
20+
return new \Mpdf\Mpdf($config);
21+
}
22+
1123
/**
1224
* Save Spreadsheet to file.
1325
*
@@ -54,7 +66,7 @@ public function save($pFilename)
5466

5567
// Create PDF
5668
$config = ['tempDir' => $this->tempDir];
57-
$pdf = new \Mpdf\Mpdf($config);
69+
$pdf = $this->createExternalWriterInstance($config);
5870
$ortmp = $orientation;
5971
$pdf->_setPageSize(strtoupper($paperSize), $ortmp);
6072
$pdf->DefOrientation = $orientation;

src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77

88
class Tcpdf extends Pdf
99
{
10+
/**
11+
* Gets the implementation of external PDF library that should be used.
12+
*
13+
* @param string $orientation Page orientation
14+
* @param string $unit Unit measure
15+
* @param string $paperSize Paper size
16+
*
17+
* @return TCPDF implementation
18+
*/
19+
protected function createExternalWriterInstance($orientation, $unit, $paperSize)
20+
{
21+
return new \TCPDF($orientation, $unit, $paperSize);
22+
}
23+
1024
/**
1125
* Save Spreadsheet to file.
1226
*
@@ -50,7 +64,7 @@ public function save($pFilename)
5064
}
5165

5266
// Create PDF
53-
$pdf = new \TCPDF($orientation, 'pt', $paperSize);
67+
$pdf = $this->createExternalWriterInstance($orientation, 'pt', $paperSize);
5468
$pdf->setFontSubsetting(false);
5569
// Set margins, converting inches to points (using 72 dpi)
5670
$pdf->SetMargins($printMargins->getLeft() * 72, $printMargins->getTop() * 72, $printMargins->getRight() * 72);

0 commit comments

Comments
 (0)