diff --git a/docs/changes/1.x/1.2.0.md b/docs/changes/1.x/1.2.0.md index 41ad3e5fdd..ba8e6bfb38 100644 --- a/docs/changes/1.x/1.2.0.md +++ b/docs/changes/1.x/1.2.0.md @@ -10,6 +10,7 @@ - Word2007 Reader : Added option to disable loading images by [@aelliott1485](https://github.com/aelliott1485) in GH-2450 - HTML Writer : Added border-spacing to default styles for table by [@kernusr](https://github.com/kernusr) in GH-2451 - Word2007 Reader : Support for table cell borders and margins by [@kernusr](https://github.com/kernusr) in GH-2454 +- PDF Writer : Add config for defining the default font by [@MikeMaldini](https://github.com/MikeMaldini) in [#2262](https://github.com/PHPOffice/PHPWord/pull/2262) & [#2468](https://github.com/PHPOffice/PHPWord/pull/2468) ### Bug fixes diff --git a/docs/usage/writers.md b/docs/usage/writers.md index 86458cecef..8610ef3cf0 100644 --- a/docs/usage/writers.md +++ b/docs/usage/writers.md @@ -30,6 +30,24 @@ $writer = IOFactory::createWriter($oPhpWord, 'PDF'); $writer->save(__DIR__ . '/sample.pdf'); ``` +### Options + +You can define options like : +* `font`: default font + +Options must be defined before creating the writer. + +``` php +use PhpOffice\PhpWord\Settings; + +Settings::setPdfRendererOptions([ + 'font' => 'Arial' +]); + +$writer = IOFactory::createWriter($oPhpWord, 'PDF'); +$writer->save(__DIR__ . '/sample.pdf'); +``` + ## RTF The name of the writer is `RTF`. diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4efe5078ef..e4b89e68dd 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -2002,7 +2002,7 @@ parameters: - message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#" - count: 1 + count: 2 path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php - @@ -2047,7 +2047,7 @@ parameters: - message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#" - count: 2 + count: 3 path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php - @@ -2057,6 +2057,11 @@ parameters: - message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#" + count: 2 + path: tests/PhpWordTests/Writer/PDF/MPDFTest.php + + - + message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#" count: 1 path: tests/PhpWordTests/Writer/PDF/MPDFTest.php @@ -2067,6 +2072,11 @@ parameters: - message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#" + count: 2 + path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php + + - + message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#" count: 1 path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php diff --git a/src/PhpWord/Settings.php b/src/PhpWord/Settings.php index 22d067b0b8..d79cea6795 100644 --- a/src/PhpWord/Settings.php +++ b/src/PhpWord/Settings.php @@ -89,6 +89,13 @@ class Settings */ private static $pdfRendererName; + /** + * Options used for rendering PDF files. + * + * @var array + */ + private static $pdfRendererOptions = []; + /** * Directory Path to the external Library used for rendering PDF files. * @@ -226,6 +233,22 @@ public static function getPdfRendererPath(): ?string return self::$pdfRendererPath; } + /** + * Set options of the external library for rendering PDF files. + */ + public static function setPdfRendererOptions(array $options): void + { + self::$pdfRendererOptions = $options; + } + + /** + * Return the PDF Rendering Options. + */ + public static function getPdfRendererOptions(): array + { + return self::$pdfRendererOptions; + } + /** * Location of external library to use for rendering PDF files. * diff --git a/src/PhpWord/Writer/PDF/AbstractRenderer.php b/src/PhpWord/Writer/PDF/AbstractRenderer.php index e8be7c06a5..6ab6535f4c 100644 --- a/src/PhpWord/Writer/PDF/AbstractRenderer.php +++ b/src/PhpWord/Writer/PDF/AbstractRenderer.php @@ -81,6 +81,7 @@ abstract class AbstractRenderer extends HTML public function __construct(PhpWord $phpWord) { parent::__construct($phpWord); + if ($this->includeFile != null) { $includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile; if (file_exists($includeFile)) { @@ -93,6 +94,12 @@ public function __construct(PhpWord $phpWord) // @codeCoverageIgnoreEnd } } + + // Configuration + $options = Settings::getPdfRendererOptions(); + if (!empty($options['font'])) { + $this->setFont($options['font']); + } } /** diff --git a/src/PhpWord/Writer/PDF/DomPDF.php b/src/PhpWord/Writer/PDF/DomPDF.php index 26ceb79b55..ea167b4d0a 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,12 @@ class DomPDF extends AbstractRenderer implements WriterInterface */ protected function createExternalWriterInstance() { - return new DompdfLib(); + $options = new Options(); + if ($this->getFont()) { + $options->set('defaultFont', $this->getFont()); + } + + return new DompdfLib($options); } /** diff --git a/src/PhpWord/Writer/PDF/MPDF.php b/src/PhpWord/Writer/PDF/MPDF.php index f37b615561..481d4629b3 100644 --- a/src/PhpWord/Writer/PDF/MPDF.php +++ b/src/PhpWord/Writer/PDF/MPDF.php @@ -52,7 +52,12 @@ protected function createExternalWriterInstance() { $mPdfClass = $this->getMPdfClassName(); - return new $mPdfClass(); + $options = []; + if ($this->getFont()) { + $options['default_font'] = $this->getFont(); + } + + return new $mPdfClass($options); } /** diff --git a/src/PhpWord/Writer/PDF/TCPDF.php b/src/PhpWord/Writer/PDF/TCPDF.php index 02853ed7bc..0847a0c3e8 100644 --- a/src/PhpWord/Writer/PDF/TCPDF.php +++ b/src/PhpWord/Writer/PDF/TCPDF.php @@ -46,7 +46,13 @@ class TCPDF extends AbstractRenderer implements WriterInterface */ protected function createExternalWriterInstance($orientation, $unit, $paperSize) { - return new \TCPDF($orientation, $unit, $paperSize); + $instance = new \TCPDF($orientation, $unit, $paperSize); + + if ($this->getFont()) { + $instance->setFont($this->getFont(), $instance->getFontStyle(), $instance->getFontSizePt()); + } + + return $instance; } /** diff --git a/tests/PhpWordTests/SettingsTest.php b/tests/PhpWordTests/SettingsTest.php index 58490c4a4f..86a240cb75 100644 --- a/tests/PhpWordTests/SettingsTest.php +++ b/tests/PhpWordTests/SettingsTest.php @@ -41,6 +41,11 @@ class SettingsTest extends TestCase private $pdfRendererName; + /** + * @var array + */ + private $pdfRendererOptions; + private $pdfRendererPath; private $tempDir; @@ -56,6 +61,7 @@ protected function setUp(): void $this->measurementUnit = Settings::getMeasurementUnit(); $this->outputEscapingEnabled = Settings::isOutputEscapingEnabled(); $this->pdfRendererName = Settings::getPdfRendererName(); + $this->pdfRendererOptions = Settings::getPdfRendererOptions(); $this->pdfRendererPath = Settings::getPdfRendererPath(); $this->tempDir = Settings::getTempDir(); $this->zipClass = Settings::getZipClass(); @@ -70,6 +76,7 @@ protected function tearDown(): void Settings::setMeasurementUnit($this->measurementUnit); Settings::setOutputEscapingEnabled($this->outputEscapingEnabled); Settings::setPdfRendererName($this->pdfRendererName); + Settings::setPdfRendererOptions($this->pdfRendererOptions); Settings::setPdfRendererPath($this->pdfRendererPath); Settings::setTempDir($this->tempDir); Settings::setZipClass($this->zipClass); @@ -124,6 +131,23 @@ public function testSetGetPdfRenderer(): void self::assertEquals($domPdfPath, Settings::getPdfRendererPath()); } + /** + * Test set/get PDF renderer. + */ + public function testSetGetPdfOptions(): void + { + $domPdfPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/dompdf/dompdf'); + + self::assertEquals([], Settings::getPdfRendererOptions()); + + Settings::setPdfRendererOptions([ + 'font' => 'Arial', + ]); + self::assertEquals([ + 'font' => 'Arial', + ], Settings::getPdfRendererOptions()); + } + /** * Test set/get measurement unit. */ diff --git a/tests/PhpWordTests/Writer/PDF/DomPDFTest.php b/tests/PhpWordTests/Writer/PDF/DomPDFTest.php index 5365a9dcaa..789519ded1 100644 --- a/tests/PhpWordTests/Writer/PDF/DomPDFTest.php +++ b/tests/PhpWordTests/Writer/PDF/DomPDFTest.php @@ -75,4 +75,21 @@ public function testSetGetAbstractRendererProperties(): void $writer->setTempDir(Settings::getTempDir()); self::assertEquals(Settings::getTempDir(), $writer->getTempDir()); } + + /** + * Test set/get abstract renderer options. + */ + public function testSetGetAbstractRendererOptions(): void + { + define('DOMPDF_ENABLE_AUTOLOAD', false); + + $rendererName = Settings::PDF_RENDERER_DOMPDF; + $rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/dompdf/dompdf'); + Settings::setPdfRenderer($rendererName, $rendererLibraryPath); + Settings::setPdfRendererOptions([ + 'font' => 'Arial', + ]); + $writer = new PDF(new PhpWord()); + self::assertEquals('Arial', $writer->getFont()); + } } diff --git a/tests/PhpWordTests/Writer/PDF/MPDFTest.php b/tests/PhpWordTests/Writer/PDF/MPDFTest.php index 44effe3b26..5905fa4eaf 100644 --- a/tests/PhpWordTests/Writer/PDF/MPDFTest.php +++ b/tests/PhpWordTests/Writer/PDF/MPDFTest.php @@ -50,4 +50,19 @@ public function testConstruct(): void unlink($file); } + + /** + * Test set/get abstract renderer options. + */ + public function testSetGetAbstractRendererOptions(): void + { + $rendererName = Settings::PDF_RENDERER_MPDF; + $rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/mpdf/mpdf'); + Settings::setPdfRenderer($rendererName, $rendererLibraryPath); + Settings::setPdfRendererOptions([ + 'font' => 'Arial', + ]); + $writer = new PDF(new PhpWord()); + self::assertEquals('Arial', $writer->getFont()); + } } diff --git a/tests/PhpWordTests/Writer/PDF/TCPDFTest.php b/tests/PhpWordTests/Writer/PDF/TCPDFTest.php index 89e6acb701..c3f05b2b16 100644 --- a/tests/PhpWordTests/Writer/PDF/TCPDFTest.php +++ b/tests/PhpWordTests/Writer/PDF/TCPDFTest.php @@ -49,4 +49,19 @@ public function testConstruct(): void unlink($file); } + + /** + * Test set/get abstract renderer options. + */ + public function testSetGetAbstractRendererOptions(): void + { + $rendererName = Settings::PDF_RENDERER_TCPDF; + $rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/tecnickcom/tcpdf'); + Settings::setPdfRenderer($rendererName, $rendererLibraryPath); + Settings::setPdfRendererOptions([ + 'font' => 'Arial', + ]); + $writer = new PDF(new PhpWord()); + self::assertEquals('Arial', $writer->getFont()); + } }