Skip to content

Commit d94b00c

Browse files
authored
Merge pull request #2468 from PHPOffice/pr2262
PDF Writer : Add config for defining the default font
2 parents 3502278 + 275616a commit d94b00c

File tree

12 files changed

+152
-5
lines changed

12 files changed

+152
-5
lines changed

docs/changes/1.x/1.2.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Word2007 Reader : Added option to disable loading images by [@aelliott1485](https://github.com/aelliott1485) in GH-2450
1111
- HTML Writer : Added border-spacing to default styles for table by [@kernusr](https://github.com/kernusr) in GH-2451
1212
- Word2007 Reader : Support for table cell borders and margins by [@kernusr](https://github.com/kernusr) in GH-2454
13+
- 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)
1314

1415
### Bug fixes
1516

docs/usage/writers.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ $writer = IOFactory::createWriter($oPhpWord, 'PDF');
3030
$writer->save(__DIR__ . '/sample.pdf');
3131
```
3232

33+
### Options
34+
35+
You can define options like :
36+
* `font`: default font
37+
38+
Options must be defined before creating the writer.
39+
40+
``` php
41+
use PhpOffice\PhpWord\Settings;
42+
43+
Settings::setPdfRendererOptions([
44+
'font' => 'Arial'
45+
]);
46+
47+
$writer = IOFactory::createWriter($oPhpWord, 'PDF');
48+
$writer->save(__DIR__ . '/sample.pdf');
49+
```
50+
3351
## RTF
3452
The name of the writer is `RTF`.
3553

phpstan-baseline.neon

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,7 @@ parameters:
20022002

20032003
-
20042004
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#"
2005-
count: 1
2005+
count: 2
20062006
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php
20072007

20082008
-
@@ -2047,7 +2047,7 @@ parameters:
20472047

20482048
-
20492049
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
2050-
count: 2
2050+
count: 3
20512051
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php
20522052

20532053
-
@@ -2057,6 +2057,11 @@ parameters:
20572057

20582058
-
20592059
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
2060+
count: 2
2061+
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
2062+
2063+
-
2064+
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#"
20602065
count: 1
20612066
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
20622067

@@ -2067,6 +2072,11 @@ parameters:
20672072

20682073
-
20692074
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
2075+
count: 2
2076+
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php
2077+
2078+
-
2079+
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#"
20702080
count: 1
20712081
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php
20722082

src/PhpWord/Settings.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ class Settings
8989
*/
9090
private static $pdfRendererName;
9191

92+
/**
93+
* Options used for rendering PDF files.
94+
*
95+
* @var array
96+
*/
97+
private static $pdfRendererOptions = [];
98+
9299
/**
93100
* Directory Path to the external Library used for rendering PDF files.
94101
*
@@ -226,6 +233,22 @@ public static function getPdfRendererPath(): ?string
226233
return self::$pdfRendererPath;
227234
}
228235

236+
/**
237+
* Set options of the external library for rendering PDF files.
238+
*/
239+
public static function setPdfRendererOptions(array $options): void
240+
{
241+
self::$pdfRendererOptions = $options;
242+
}
243+
244+
/**
245+
* Return the PDF Rendering Options.
246+
*/
247+
public static function getPdfRendererOptions(): array
248+
{
249+
return self::$pdfRendererOptions;
250+
}
251+
229252
/**
230253
* Location of external library to use for rendering PDF files.
231254
*

src/PhpWord/Writer/PDF/AbstractRenderer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ abstract class AbstractRenderer extends HTML
8181
public function __construct(PhpWord $phpWord)
8282
{
8383
parent::__construct($phpWord);
84+
8485
if ($this->includeFile != null) {
8586
$includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;
8687
if (file_exists($includeFile)) {
@@ -93,6 +94,12 @@ public function __construct(PhpWord $phpWord)
9394
// @codeCoverageIgnoreEnd
9495
}
9596
}
97+
98+
// Configuration
99+
$options = Settings::getPdfRendererOptions();
100+
if (!empty($options['font'])) {
101+
$this->setFont($options['font']);
102+
}
96103
}
97104

98105
/**

src/PhpWord/Writer/PDF/DomPDF.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpWord\Writer\PDF;
1919

2020
use Dompdf\Dompdf as DompdfLib;
21+
use Dompdf\Options;
2122
use PhpOffice\PhpWord\Writer\WriterInterface;
2223

2324
/**
@@ -42,7 +43,12 @@ class DomPDF extends AbstractRenderer implements WriterInterface
4243
*/
4344
protected function createExternalWriterInstance()
4445
{
45-
return new DompdfLib();
46+
$options = new Options();
47+
if ($this->getFont()) {
48+
$options->set('defaultFont', $this->getFont());
49+
}
50+
51+
return new DompdfLib($options);
4652
}
4753

4854
/**

src/PhpWord/Writer/PDF/MPDF.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ protected function createExternalWriterInstance()
5252
{
5353
$mPdfClass = $this->getMPdfClassName();
5454

55-
return new $mPdfClass();
55+
$options = [];
56+
if ($this->getFont()) {
57+
$options['default_font'] = $this->getFont();
58+
}
59+
60+
return new $mPdfClass($options);
5661
}
5762

5863
/**

src/PhpWord/Writer/PDF/TCPDF.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ class TCPDF extends AbstractRenderer implements WriterInterface
4646
*/
4747
protected function createExternalWriterInstance($orientation, $unit, $paperSize)
4848
{
49-
return new \TCPDF($orientation, $unit, $paperSize);
49+
$instance = new \TCPDF($orientation, $unit, $paperSize);
50+
51+
if ($this->getFont()) {
52+
$instance->setFont($this->getFont(), $instance->getFontStyle(), $instance->getFontSizePt());
53+
}
54+
55+
return $instance;
5056
}
5157

5258
/**

tests/PhpWordTests/SettingsTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class SettingsTest extends TestCase
4141

4242
private $pdfRendererName;
4343

44+
/**
45+
* @var array
46+
*/
47+
private $pdfRendererOptions;
48+
4449
private $pdfRendererPath;
4550

4651
private $tempDir;
@@ -56,6 +61,7 @@ protected function setUp(): void
5661
$this->measurementUnit = Settings::getMeasurementUnit();
5762
$this->outputEscapingEnabled = Settings::isOutputEscapingEnabled();
5863
$this->pdfRendererName = Settings::getPdfRendererName();
64+
$this->pdfRendererOptions = Settings::getPdfRendererOptions();
5965
$this->pdfRendererPath = Settings::getPdfRendererPath();
6066
$this->tempDir = Settings::getTempDir();
6167
$this->zipClass = Settings::getZipClass();
@@ -70,6 +76,7 @@ protected function tearDown(): void
7076
Settings::setMeasurementUnit($this->measurementUnit);
7177
Settings::setOutputEscapingEnabled($this->outputEscapingEnabled);
7278
Settings::setPdfRendererName($this->pdfRendererName);
79+
Settings::setPdfRendererOptions($this->pdfRendererOptions);
7380
Settings::setPdfRendererPath($this->pdfRendererPath);
7481
Settings::setTempDir($this->tempDir);
7582
Settings::setZipClass($this->zipClass);
@@ -124,6 +131,23 @@ public function testSetGetPdfRenderer(): void
124131
self::assertEquals($domPdfPath, Settings::getPdfRendererPath());
125132
}
126133

134+
/**
135+
* Test set/get PDF renderer.
136+
*/
137+
public function testSetGetPdfOptions(): void
138+
{
139+
$domPdfPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/dompdf/dompdf');
140+
141+
self::assertEquals([], Settings::getPdfRendererOptions());
142+
143+
Settings::setPdfRendererOptions([
144+
'font' => 'Arial',
145+
]);
146+
self::assertEquals([
147+
'font' => 'Arial',
148+
], Settings::getPdfRendererOptions());
149+
}
150+
127151
/**
128152
* Test set/get measurement unit.
129153
*/

tests/PhpWordTests/Writer/PDF/DomPDFTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,21 @@ public function testSetGetAbstractRendererProperties(): void
7575
$writer->setTempDir(Settings::getTempDir());
7676
self::assertEquals(Settings::getTempDir(), $writer->getTempDir());
7777
}
78+
79+
/**
80+
* Test set/get abstract renderer options.
81+
*/
82+
public function testSetGetAbstractRendererOptions(): void
83+
{
84+
define('DOMPDF_ENABLE_AUTOLOAD', false);
85+
86+
$rendererName = Settings::PDF_RENDERER_DOMPDF;
87+
$rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/dompdf/dompdf');
88+
Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
89+
Settings::setPdfRendererOptions([
90+
'font' => 'Arial',
91+
]);
92+
$writer = new PDF(new PhpWord());
93+
self::assertEquals('Arial', $writer->getFont());
94+
}
7895
}

tests/PhpWordTests/Writer/PDF/MPDFTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,19 @@ public function testConstruct(): void
5050

5151
unlink($file);
5252
}
53+
54+
/**
55+
* Test set/get abstract renderer options.
56+
*/
57+
public function testSetGetAbstractRendererOptions(): void
58+
{
59+
$rendererName = Settings::PDF_RENDERER_MPDF;
60+
$rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/mpdf/mpdf');
61+
Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
62+
Settings::setPdfRendererOptions([
63+
'font' => 'Arial',
64+
]);
65+
$writer = new PDF(new PhpWord());
66+
self::assertEquals('Arial', $writer->getFont());
67+
}
5368
}

tests/PhpWordTests/Writer/PDF/TCPDFTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,19 @@ public function testConstruct(): void
4949

5050
unlink($file);
5151
}
52+
53+
/**
54+
* Test set/get abstract renderer options.
55+
*/
56+
public function testSetGetAbstractRendererOptions(): void
57+
{
58+
$rendererName = Settings::PDF_RENDERER_TCPDF;
59+
$rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/tecnickcom/tcpdf');
60+
Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
61+
Settings::setPdfRendererOptions([
62+
'font' => 'Arial',
63+
]);
64+
$writer = new PDF(new PhpWord());
65+
self::assertEquals('Arial', $writer->getFont());
66+
}
5267
}

0 commit comments

Comments
 (0)