diff --git a/CHANGELOG.md b/CHANGELOG.md index faf7ae4bbc..22c0030d2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Support for chart fill color - @CrazyBite [#158](https://github.com/PHPOffice/PhpSpreadsheet/pull/158) - Support for read Hyperlink for xml - @GreatHumorist [#223](https://github.com/PHPOffice/PhpSpreadsheet/pull/223) - Support for cell value validation according to data validation rules - @SailorMax [#257](https://github.com/PHPOffice/PhpSpreadsheet/pull/257) +- Support for easer use extended classes of PDF libraries - @SailorMax [#266](https://github.com/PHPOffice/PhpSpreadsheet/pull/266) ### Changed @@ -32,12 +33,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Easier usage of chart renderers, see the [migration guide](./docs/topics/migration-from-PHPExcel.md). - Rename a few more classes to keep them in their related namespaces: - `CalcEngine` => `Calculation\Engine` - - `PhpSpreadsheet\Calculation` => `PhpSpreadsheet\Calculation\Calculation` - - `PhpSpreadsheet\Cell` => `PhpSpreadsheet\Cell\Cell` - - `PhpSpreadsheet\Chart` => `PhpSpreadsheet\Chart\Chart` - - `PhpSpreadsheet\RichText` => `PhpSpreadsheet\RichText\RichText` - - `PhpSpreadsheet\Style` => `PhpSpreadsheet\Style\Style` - - `PhpSpreadsheet\Worksheet` => `PhpSpreadsheet\Worksheet\Worksheet` + - `PhpSpreadsheet\Calculation` => `PhpSpreadsheet\Calculation\Calculation` + - `PhpSpreadsheet\Cell` => `PhpSpreadsheet\Cell\Cell` + - `PhpSpreadsheet\Chart` => `PhpSpreadsheet\Chart\Chart` + - `PhpSpreadsheet\RichText` => `PhpSpreadsheet\RichText\RichText` + - `PhpSpreadsheet\Style` => `PhpSpreadsheet\Style\Style` + - `PhpSpreadsheet\Worksheet` => `PhpSpreadsheet\Worksheet\Worksheet` ## [1.0.0-beta] - 2017-08-17 diff --git a/docs/topics/reading-and-writing-to-file.md b/docs/topics/reading-and-writing-to-file.md index 0e64212044..9baa861ea0 100644 --- a/docs/topics/reading-and-writing-to-file.md +++ b/docs/topics/reading-and-writing-to-file.md @@ -779,6 +779,25 @@ Or you can instantiate directly the writer of your choice like so: $writer = \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet); ``` +You can also use extended classes, based on supported libraries: + +``` php +class MY_TCPDF extends TCPDF +{ + // ... +} + +class MY_TCPDF_WRITER extends \PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf implements \PhpOffice\PhpSpreadsheet\Writer\IWriter +{ + protected function createExternalWriterInstance($orientation, $unit, $paperSize) + { + return new MY_TCPDF($orientation, $unit, $paperSize); + } +} + +\PhpOffice\PhpSpreadsheet\IOFactory::registerWriter('Pdf', MY_TCPDF_WRITER::class); +``` + #### Writing a spreadsheet Once you have identified the Renderer that you wish to use for PDF diff --git a/src/PhpSpreadsheet/Writer/Pdf/Dompdf.php b/src/PhpSpreadsheet/Writer/Pdf/Dompdf.php index 0872840ee6..a7004568aa 100644 --- a/src/PhpSpreadsheet/Writer/Pdf/Dompdf.php +++ b/src/PhpSpreadsheet/Writer/Pdf/Dompdf.php @@ -8,6 +8,16 @@ class Dompdf extends Pdf implements IWriter { + /** + * Gets the implementation of external PDF library that should be used. + * + * @return \Dompdf\Dompdf implementation + */ + protected function createExternalWriterInstance() + { + return new \Dompdf\Dompdf(); + } + /** * Save Spreadsheet to file. * @@ -51,7 +61,7 @@ public function save($pFilename) } // Create PDF - $pdf = new \Dompdf\Dompdf(); + $pdf = $this->createExternalWriterInstance(); $pdf->setPaper(strtolower($paperSize), $orientation); $pdf->loadHtml( diff --git a/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php b/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php index cc829b1fa9..dd723e139c 100644 --- a/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php +++ b/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php @@ -9,6 +9,18 @@ class Mpdf extends Pdf implements IWriter { + /** + * Gets the implementation of external PDF library that should be used. + * + * @param array $config Configuration array + * + * @return \Mpdf\Mpdf implementation + */ + protected function createExternalWriterInstance($config) + { + return new \Mpdf\Mpdf($config); + } + /** * Save Spreadsheet to file. * @@ -55,7 +67,7 @@ public function save($pFilename) // Create PDF $config = ['tempDir' => $this->tempDir]; - $pdf = new \Mpdf\Mpdf($config); + $pdf = $this->createExternalWriterInstance($config); $ortmp = $orientation; $pdf->_setPageSize(strtoupper($paperSize), $ortmp); $pdf->DefOrientation = $orientation; diff --git a/src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php b/src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php index c4b7aa55a2..b07b573c4f 100644 --- a/src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php +++ b/src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php @@ -8,6 +8,20 @@ class Tcpdf extends Pdf implements IWriter { + /** + * Gets the implementation of external PDF library that should be used. + * + * @param string $orientation Page orientation + * @param string $unit Unit measure + * @param string $paperSize Paper size + * + * @return TCPDF implementation + */ + protected function createExternalWriterInstance($orientation, $unit, $paperSize) + { + return new \TCPDF($orientation, $unit, $paperSize); + } + /** * Save Spreadsheet to file. * @@ -51,7 +65,7 @@ public function save($pFilename) } // Create PDF - $pdf = new \TCPDF($orientation, 'pt', $paperSize); + $pdf = $this->createExternalWriterInstance($orientation, 'pt', $paperSize); $pdf->setFontSubsetting(false); // Set margins, converting inches to points (using 72 dpi) $pdf->SetMargins($printMargins->getLeft() * 72, $printMargins->getTop() * 72, $printMargins->getRight() * 72);