From 42720306afc696898d5ee2cd2d24331c1be849b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E4=BF=8A?= Date: Fri, 15 Jul 2022 18:22:20 +0800 Subject: [PATCH] Add custom font for Dompdf libary --- src/PhpWord/IOFactory.php | 5 +++-- src/PhpWord/Writer/PDF/AbstractRenderer.php | 4 +++- src/PhpWord/Writer/PDF/DomPDF.php | 11 ++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/PhpWord/IOFactory.php b/src/PhpWord/IOFactory.php index 3929f485e4..68557490ac 100644 --- a/src/PhpWord/IOFactory.php +++ b/src/PhpWord/IOFactory.php @@ -28,12 +28,13 @@ abstract class IOFactory * * @param PhpWord $phpWord * @param string $name + * @param array $config * * @throws \PhpOffice\PhpWord\Exception\Exception * * @return WriterInterface */ - public static function createWriter(PhpWord $phpWord, $name = 'Word2007') + public static function createWriter(PhpWord $phpWord, $name = 'Word2007', $config = array()) { if ($name !== 'WriterInterface' && !in_array($name, array('ODText', 'RTF', 'Word2007', 'HTML', 'PDF'), true)) { throw new Exception("\"{$name}\" is not a valid writer."); @@ -41,7 +42,7 @@ public static function createWriter(PhpWord $phpWord, $name = 'Word2007') $fqName = "PhpOffice\\PhpWord\\Writer\\{$name}"; - return new $fqName($phpWord); + return ($name == 'PDF' && !empty($config)) ? new $fqName($phpWord, $config) : new $fqName($phpWord); } /** diff --git a/src/PhpWord/Writer/PDF/AbstractRenderer.php b/src/PhpWord/Writer/PDF/AbstractRenderer.php index 5f9e3b3a8c..876d48ad1b 100644 --- a/src/PhpWord/Writer/PDF/AbstractRenderer.php +++ b/src/PhpWord/Writer/PDF/AbstractRenderer.php @@ -77,10 +77,11 @@ abstract class AbstractRenderer extends HTML * Create new instance * * @param PhpWord $phpWord PhpWord object + * @param array $config * * @throws \PhpOffice\PhpWord\Exception\Exception */ - public function __construct(PhpWord $phpWord) + public function __construct(PhpWord $phpWord, $config = array()) { parent::__construct($phpWord); if ($this->includeFile != null) { @@ -95,6 +96,7 @@ public function __construct(PhpWord $phpWord) // @codeCoverageIgnoreEnd } } + if(isset($config['font']) && empty($config['font'])) $this->font = $config['font']; } /** diff --git a/src/PhpWord/Writer/PDF/DomPDF.php b/src/PhpWord/Writer/PDF/DomPDF.php index 53eea81588..f882af46dc 100644 --- a/src/PhpWord/Writer/PDF/DomPDF.php +++ b/src/PhpWord/Writer/PDF/DomPDF.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Writer\PDF; use Dompdf\Dompdf as DompdfLib; +use Dompdf\Options; use PhpOffice\PhpWord\Writer\WriterInterface; /** @@ -42,7 +43,15 @@ class DomPDF extends AbstractRenderer implements WriterInterface */ protected function createExternalWriterInstance() { - return new DompdfLib(); + if(!empty($this->font)){ + $options = new Options(); + $options->set('defaultFont', $this->font); + $instance = new DompdfLib($options); + }else{ + $instance = new DompdfLib(); + } + + return $instance; } /**