diff --git a/src/PhpWord/IOFactory.php b/src/PhpWord/IOFactory.php index 9ceb7026a3..c232ce8fb4 100644 --- a/src/PhpWord/IOFactory.php +++ b/src/PhpWord/IOFactory.php @@ -30,10 +30,9 @@ abstract class IOFactory * Create new writer. * * @param string $name - * - * @return WriterInterface + * @param array $config */ - public static function createWriter(PhpWord $phpWord, $name = 'Word2007') + public static function createWriter(PhpWord $phpWord, $name = 'Word2007', $config = []): WriterInterface { if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF'], true)) { throw new Exception("\"{$name}\" is not a valid writer."); @@ -41,7 +40,7 @@ public static function createWriter(PhpWord $phpWord, $name = 'Word2007') $fqName = "PhpOffice\\PhpWord\\Writer\\{$name}"; - return new $fqName($phpWord); + return new $fqName($phpWord, $config); } /** diff --git a/src/PhpWord/Writer/AbstractWriter.php b/src/PhpWord/Writer/AbstractWriter.php index 8ebf98c7b5..d5f30b4a17 100644 --- a/src/PhpWord/Writer/AbstractWriter.php +++ b/src/PhpWord/Writer/AbstractWriter.php @@ -93,6 +93,24 @@ abstract class AbstractWriter implements WriterInterface */ private $tempFilename; + /** + * some options in config. + * + * @var array + */ + protected $config; + + /** + * construct method. + * + * @param array $config + */ + public function __construct(?PhpWord $phpWord = null, $config = []) + { + $this->setPhpWord($phpWord); + $this->config = $config; + } + /** * Get PhpWord object. * diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index 647890591d..071eebd4f6 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -68,10 +68,13 @@ class HTML extends AbstractWriter implements WriterInterface /** * Create new instance. + * + * @param array $config */ - public function __construct(?PhpWord $phpWord = null) + public function __construct(?PhpWord $phpWord = null, $config = []) { - $this->setPhpWord($phpWord); + // Assign PhpWord + parent::__construct($phpWord, $config); $this->parts = ['Head', 'Body']; foreach ($this->parts as $partName) { diff --git a/src/PhpWord/Writer/ODText.php b/src/PhpWord/Writer/ODText.php index 616119e5cc..e367362b9d 100644 --- a/src/PhpWord/Writer/ODText.php +++ b/src/PhpWord/Writer/ODText.php @@ -38,11 +38,13 @@ class ODText extends AbstractWriter implements WriterInterface /** * Create new ODText writer. + * + * @param array $config */ - public function __construct(?PhpWord $phpWord = null) + public function __construct(?PhpWord $phpWord = null, $config = []) { // Assign PhpWord - $this->setPhpWord($phpWord); + parent::__construct($phpWord, $config); // Create parts $this->parts = [ diff --git a/src/PhpWord/Writer/PDF.php b/src/PhpWord/Writer/PDF.php index f937f599c1..755c73f933 100644 --- a/src/PhpWord/Writer/PDF.php +++ b/src/PhpWord/Writer/PDF.php @@ -27,7 +27,7 @@ * * @since 0.10.0 */ -class PDF +class PDF implements WriterInterface { /** * The wrapper for the requested PDF rendering engine. @@ -38,8 +38,10 @@ class PDF /** * Instantiate a new renderer of the configured type within this container class. + * + * @param array $config */ - public function __construct(PhpWord $phpWord) + public function __construct(PhpWord $phpWord, $config = []) { $pdfLibraryName = Settings::getPdfRendererName(); $pdfLibraryPath = Settings::getPdfRendererPath(); @@ -54,7 +56,7 @@ public function __construct(PhpWord $phpWord) } $rendererName = static::class . '\\' . $pdfLibraryName; - $this->renderer = new $rendererName($phpWord); + $this->renderer = new $rendererName($phpWord, $config); } /** diff --git a/src/PhpWord/Writer/PDF/AbstractRenderer.php b/src/PhpWord/Writer/PDF/AbstractRenderer.php index c143a6cb5c..534372cd37 100644 --- a/src/PhpWord/Writer/PDF/AbstractRenderer.php +++ b/src/PhpWord/Writer/PDF/AbstractRenderer.php @@ -76,11 +76,13 @@ abstract class AbstractRenderer extends HTML /** * Create new instance. * - * @param PhpWord $phpWord PhpWord object + * @param array $config */ - public function __construct(PhpWord $phpWord) + public function __construct(PhpWord $phpWord, $config = []) { - parent::__construct($phpWord); + // Assign PhpWord + parent::__construct($phpWord, $config); + $this->isPdf = true; if ($this->includeFile != null) { $includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile; diff --git a/src/PhpWord/Writer/PDF/MPDF.php b/src/PhpWord/Writer/PDF/MPDF.php index 311f743d6d..8f22a8007e 100644 --- a/src/PhpWord/Writer/PDF/MPDF.php +++ b/src/PhpWord/Writer/PDF/MPDF.php @@ -35,15 +35,16 @@ class MPDF extends AbstractRenderer implements WriterInterface /** * Overridden to set the correct includefile, only needed for MPDF 5. * + * @param array $config * @codeCoverageIgnore */ - public function __construct(PhpWord $phpWord) + public function __construct(PhpWord $phpWord, $config = []) { if (file_exists(Settings::getPdfRendererPath() . '/mpdf.php')) { // MPDF version 5.* needs this file to be included, later versions not $this->includeFile = 'mpdf.php'; } - parent::__construct($phpWord); + parent::__construct($phpWord, $config); } /** @@ -55,7 +56,7 @@ protected function createExternalWriterInstance() { $mPdfClass = $this->getMPdfClassName(); - $options = []; + $options = $this->config; if ($this->getFont()) { $options['default_font'] = $this->getFont(); } diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 0a04d4f53e..7f0ad84075 100644 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -35,10 +35,13 @@ class RTF extends AbstractWriter implements WriterInterface /** * Create new instance. + * + * @param array $config */ - public function __construct(?PhpWord $phpWord = null) + public function __construct(?PhpWord $phpWord = null, $config = []) { - $this->setPhpWord($phpWord); + // Assign PhpWord + parent::__construct($phpWord, $config); $this->parts = ['Header', 'Document']; foreach ($this->parts as $partName) { diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index ab4fd1e3eb..9e1d308871 100644 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -44,12 +44,13 @@ class Word2007 extends AbstractWriter implements WriterInterface /** * Create new Word2007 writer. * - * @param \PhpOffice\PhpWord\PhpWord + * @param \PhpOffice\PhpWord\PhpWord $phpWord + * @param array $config */ - public function __construct(?PhpWord $phpWord = null) + public function __construct(?PhpWord $phpWord = null, $config = []) { // Assign PhpWord - $this->setPhpWord($phpWord); + parent::__construct($phpWord, $config); // Create parts // The first four files need to be in this order for Mimetype detection to work @@ -78,7 +79,7 @@ public function __construct(?PhpWord $phpWord = null) foreach (array_keys($this->parts) as $partName) { $partClass = static::class . '\\Part\\' . $partName; if (class_exists($partClass)) { - /** @var \PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart $part Type hint */ + /** @var \PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart $part */ $part = new $partClass(); $part->setParentWriter($this); $this->writerParts[strtolower($partName)] = $part; diff --git a/tests/PhpWordTests/Writer/PDF/MPDFTest.php b/tests/PhpWordTests/Writer/PDF/MPDFTest.php index 0fe53456ba..809c2d2522 100644 --- a/tests/PhpWordTests/Writer/PDF/MPDFTest.php +++ b/tests/PhpWordTests/Writer/PDF/MPDFTest.php @@ -46,7 +46,7 @@ public function testConstruct(): void $section = $phpWord->addSection($oSettings); // @phpstan-ignore-line $section->addText('Section 2 - landscape'); - $writer = new MPDF($phpWord); + $writer = new MPDF($phpWord, ['mode' => 'zh-cn', 'margin_top' => 28]); $writer->save($file); self::assertFileExists($file);