From c8de86a7ec9e368e5e7cb48e03d51e20e979a6c0 Mon Sep 17 00:00:00 2001 From: SailorMax Date: Wed, 16 Dec 2020 17:24:42 +0300 Subject: [PATCH] allow to use customized pdf library --- src/PhpWord/Element/AbstractContainer.php | 1 + src/PhpWord/Writer/PDF/DomPDF.php | 12 +++++++++++- src/PhpWord/Writer/PDF/MPDF.php | 15 +++++++++++++-- src/PhpWord/Writer/PDF/TCPDF.php | 16 +++++++++++++++- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 0c773dbe69..6a1ed93013 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -109,6 +109,7 @@ public function __call($function, $args) } else { // All other elements array_unshift($args, $element); // Prepend element name to the beginning of args array + return call_user_func_array(array($this, 'addElement'), $args); } } diff --git a/src/PhpWord/Writer/PDF/DomPDF.php b/src/PhpWord/Writer/PDF/DomPDF.php index 5fa8f75dbb..53eea81588 100644 --- a/src/PhpWord/Writer/PDF/DomPDF.php +++ b/src/PhpWord/Writer/PDF/DomPDF.php @@ -35,6 +35,16 @@ class DomPDF extends AbstractRenderer implements WriterInterface */ protected $includeFile = null; + /** + * Gets the implementation of external PDF library that should be used. + * + * @return Dompdf implementation + */ + protected function createExternalWriterInstance() + { + return new DompdfLib(); + } + /** * Save PhpWord to file. * @@ -49,7 +59,7 @@ public function save($filename = null) $orientation = 'portrait'; // Create PDF - $pdf = new DompdfLib(); + $pdf = $this->createExternalWriterInstance(); $pdf->setPaper(strtolower($paperSize), $orientation); $pdf->loadHtml(str_replace(PHP_EOL, '', $this->getContent())); $pdf->render(); diff --git a/src/PhpWord/Writer/PDF/MPDF.php b/src/PhpWord/Writer/PDF/MPDF.php index e63f5dfd68..464f156ce5 100644 --- a/src/PhpWord/Writer/PDF/MPDF.php +++ b/src/PhpWord/Writer/PDF/MPDF.php @@ -44,6 +44,18 @@ public function __construct(PhpWord $phpWord) parent::__construct($phpWord); } + /** + * Gets the implementation of external PDF library that should be used. + * + * @return Mpdf implementation + */ + protected function createExternalWriterInstance() + { + $mPdfClass = $this->getMPdfClassName(); + + return new $mPdfClass(); + } + /** * Save PhpWord to file. * @@ -58,8 +70,7 @@ public function save($filename = null) $orientation = strtoupper('portrait'); // Create PDF - $mPdfClass = $this->getMPdfClassName(); - $pdf = new $mPdfClass(); + $pdf = $this->createExternalWriterInstance(); $pdf->_setPageSize($paperSize, $orientation); $pdf->addPage($orientation); diff --git a/src/PhpWord/Writer/PDF/TCPDF.php b/src/PhpWord/Writer/PDF/TCPDF.php index badab0460e..3abbbf069d 100644 --- a/src/PhpWord/Writer/PDF/TCPDF.php +++ b/src/PhpWord/Writer/PDF/TCPDF.php @@ -36,6 +36,20 @@ class TCPDF extends AbstractRenderer implements WriterInterface */ protected $includeFile = 'tcpdf.php'; + /** + * 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 PhpWord to file. * @@ -50,7 +64,7 @@ public function save($filename = null) $orientation = 'P'; // Create PDF - $pdf = new \TCPDF($orientation, 'pt', $paperSize); + $pdf = $this->createExternalWriterInstance($orientation, 'pt', $paperSize); $pdf->setFontSubsetting(false); $pdf->setPrintHeader(false); $pdf->setPrintFooter(false);