Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to use customized pdf library #266

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 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 easer use extended classes of PDF libraries - @SailorMax [#266](https://github.com/PHPOffice/PhpSpreadsheet/pull/266)

### Changed

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

Expand Down
19 changes: 19 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,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
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 @@ -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.
*
Expand Down Expand Up @@ -51,7 +61,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 @@ -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.
*
Expand Down Expand Up @@ -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;
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 @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down