Skip to content

Commit

Permalink
Support custom PDF library instances or configurations
Browse files Browse the repository at this point in the history
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 PHPOffice#266
  • Loading branch information
SailorMax authored and Frederic Delaunay committed Oct 29, 2018
1 parent 85096dd commit d9dff58
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions docs/topics/reading-and-writing-to-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion src/PhpSpreadsheet/Writer/Pdf/Dompdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

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.
*
Expand Down Expand Up @@ -50,7 +60,7 @@ public function save($pFilename)
}

// Create PDF
$pdf = new \Dompdf\Dompdf();
$pdf = $this->createExternalWriterInstance();
$pdf->setPaper(strtolower($paperSize), $orientation);

$pdf->loadHtml(
Expand Down
14 changes: 13 additions & 1 deletion src/PhpSpreadsheet/Writer/Pdf/Mpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@

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.
*
Expand Down Expand Up @@ -54,7 +66,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;
Expand Down
16 changes: 15 additions & 1 deletion src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@

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.
*
Expand Down Expand Up @@ -50,7 +64,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);
Expand Down

0 comments on commit d9dff58

Please sign in to comment.