Skip to content

Commit c8de86a

Browse files
committed
allow to use customized pdf library
1 parent ec1b3d3 commit c8de86a

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

src/PhpWord/Element/AbstractContainer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function __call($function, $args)
109109
} else {
110110
// All other elements
111111
array_unshift($args, $element); // Prepend element name to the beginning of args array
112+
112113
return call_user_func_array(array($this, 'addElement'), $args);
113114
}
114115
}

src/PhpWord/Writer/PDF/DomPDF.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ class DomPDF extends AbstractRenderer implements WriterInterface
3535
*/
3636
protected $includeFile = null;
3737

38+
/**
39+
* Gets the implementation of external PDF library that should be used.
40+
*
41+
* @return Dompdf implementation
42+
*/
43+
protected function createExternalWriterInstance()
44+
{
45+
return new DompdfLib();
46+
}
47+
3848
/**
3949
* Save PhpWord to file.
4050
*
@@ -49,7 +59,7 @@ public function save($filename = null)
4959
$orientation = 'portrait';
5060

5161
// Create PDF
52-
$pdf = new DompdfLib();
62+
$pdf = $this->createExternalWriterInstance();
5363
$pdf->setPaper(strtolower($paperSize), $orientation);
5464
$pdf->loadHtml(str_replace(PHP_EOL, '', $this->getContent()));
5565
$pdf->render();

src/PhpWord/Writer/PDF/MPDF.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ public function __construct(PhpWord $phpWord)
4444
parent::__construct($phpWord);
4545
}
4646

47+
/**
48+
* Gets the implementation of external PDF library that should be used.
49+
*
50+
* @return Mpdf implementation
51+
*/
52+
protected function createExternalWriterInstance()
53+
{
54+
$mPdfClass = $this->getMPdfClassName();
55+
56+
return new $mPdfClass();
57+
}
58+
4759
/**
4860
* Save PhpWord to file.
4961
*
@@ -58,8 +70,7 @@ public function save($filename = null)
5870
$orientation = strtoupper('portrait');
5971

6072
// Create PDF
61-
$mPdfClass = $this->getMPdfClassName();
62-
$pdf = new $mPdfClass();
73+
$pdf = $this->createExternalWriterInstance();
6374
$pdf->_setPageSize($paperSize, $orientation);
6475
$pdf->addPage($orientation);
6576

src/PhpWord/Writer/PDF/TCPDF.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ class TCPDF extends AbstractRenderer implements WriterInterface
3636
*/
3737
protected $includeFile = 'tcpdf.php';
3838

39+
/**
40+
* Gets the implementation of external PDF library that should be used.
41+
*
42+
* @param string $orientation Page orientation
43+
* @param string $unit Unit measure
44+
* @param string $paperSize Paper size
45+
*
46+
* @return \TCPDF implementation
47+
*/
48+
protected function createExternalWriterInstance($orientation, $unit, $paperSize)
49+
{
50+
return new \TCPDF($orientation, $unit, $paperSize);
51+
}
52+
3953
/**
4054
* Save PhpWord to file.
4155
*
@@ -50,7 +64,7 @@ public function save($filename = null)
5064
$orientation = 'P';
5165

5266
// Create PDF
53-
$pdf = new \TCPDF($orientation, 'pt', $paperSize);
67+
$pdf = $this->createExternalWriterInstance($orientation, 'pt', $paperSize);
5468
$pdf->setFontSubsetting(false);
5569
$pdf->setPrintHeader(false);
5670
$pdf->setPrintFooter(false);

0 commit comments

Comments
 (0)