diff --git a/CHANGELOG.md b/CHANGELOG.md index 180814a0..04bce8e5 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 custom implementation, or configuration, of PDF libraries - @SailorMax [#266](https://github.com/PHPOffice/PhpSpreadsheet/pull/266) ### Changed diff --git a/docs/topics/reading-and-writing-to-file.md b/docs/topics/reading-and-writing-to-file.md index 0e642120..e04d6377 100644 --- a/docs/topics/reading-and-writing-to-file.md +++ b/docs/topics/reading-and-writing-to-file.md @@ -779,6 +779,32 @@ Or you can instantiate directly the writer of your choice like so: $writer = \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet); ``` +#### Custom implementation or configuration + +If you need a custom implementation, or custom configuration, of a supported +PDF library. You can extends the PDF library, and the PDF writer like so: + +``` php +class My_Custom_TCPDF extends TCPDF +{ + // ... +} + +class My_Custom_TCPDF_Writer extends \PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf +{ + protected function createExternalWriterInstance($orientation, $unit, $paperSize) + { + $instance = new My_Custom_TCPDF($orientation, $unit, $paperSize); + + // more configuration of $instance + + return $instance; + } +} + +\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 4db6c261..3c3044d7 100644 --- a/src/PhpSpreadsheet/Writer/Pdf/Dompdf.php +++ b/src/PhpSpreadsheet/Writer/Pdf/Dompdf.php @@ -7,6 +7,16 @@ use PhpOffice\PhpSpreadsheet\Writer\Pdf; class Dompdf extends Pdf { + /** + * 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. * @@ -50,7 +60,7 @@ class Dompdf extends Pdf } // 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 7a037052..ad0964db 100644 --- a/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php +++ b/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php @@ -8,6 +8,18 @@ use PhpOffice\PhpSpreadsheet\Writer\Pdf; class Mpdf extends Pdf { + /** + * 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. * @@ -54,7 +66,7 @@ class Mpdf extends Pdf // 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 00b6436e..13320504 100644 --- a/src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php +++ b/src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php @@ -7,6 +7,20 @@ use PhpOffice\PhpSpreadsheet\Writer\Pdf; class Tcpdf extends Pdf { + /** + * 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. * @@ -50,7 +64,7 @@ class Tcpdf extends Pdf } // 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);