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
This commit is contained in:
Maxim Bulygin 2017-10-30 17:14:34 +02:00 committed by Adrien Crivelli
parent 40efcd2fdd
commit 442e612202
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
5 changed files with 66 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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(

View File

@ -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;

View File

@ -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);