Skip to content

Commit

Permalink
PDF Writer : Add config for defining the default font
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Sep 13, 2023
1 parent e737afa commit 3fa49d3
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/changes/1.x/1.2.0.md
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions docs/usage/writers.md
Expand Up @@ -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`.

Expand Down
5 changes: 2 additions & 3 deletions src/PhpWord/IOFactory.php
Expand Up @@ -28,19 +28,18 @@ abstract class IOFactory
* Create new writer.
*
* @param string $name
* @param array $config
*
* @return WriterInterface
*/
public static function createWriter(PhpWord $phpWord, $name = 'Word2007', $config = array())
public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
{
if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF'], true)) {
throw new Exception("\"{$name}\" is not a valid writer.");
}

$fqName = "PhpOffice\\PhpWord\\Writer\\{$name}";

return ($name == 'PDF' && !empty($config)) ? new $fqName($phpWord, $config) : new $fqName($phpWord);
return new $fqName($phpWord);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/PhpWord/Settings.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
13 changes: 8 additions & 5 deletions src/PhpWord/Writer/PDF/AbstractRenderer.php
Expand Up @@ -77,13 +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, $config = array())
public function __construct(PhpWord $phpWord)
{
parent::__construct($phpWord);

if ($this->includeFile != null) {
$includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;
if (file_exists($includeFile)) {
Expand All @@ -96,7 +94,12 @@ public function __construct(PhpWord $phpWord, $config = array())
// @codeCoverageIgnoreEnd
}
}
if(isset($config['font']) && empty($config['font'])) $this->font = $config['font'];

// Configuration
$options = Settings::getPdfRendererOptions();
if (!empty($options['font'])) {
$this->setFont($options['font']);
}
}

/**
Expand Down
11 changes: 4 additions & 7 deletions src/PhpWord/Writer/PDF/DomPDF.php
Expand Up @@ -43,15 +43,12 @@ class DomPDF extends AbstractRenderer implements WriterInterface
*/
protected function createExternalWriterInstance()
{
if(!empty($this->font)){
$options = new Options();
$options->set('defaultFont', $this->font);
$instance = new DompdfLib($options);
}else{
$instance = new DompdfLib();
$options = new Options();
if ($this->getFont()) {
$options->set('defaultFont', $this->getFont());
}

return $instance;
return new DompdfLib($options);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/PhpWord/Writer/PDF/MPDF.php
Expand Up @@ -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);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/PhpWord/Writer/PDF/TCPDF.php
Expand Up @@ -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;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/PhpWordTests/SettingsTest.php
Expand Up @@ -41,6 +41,8 @@ class SettingsTest extends TestCase

private $pdfRendererName;

private $pdfRendererOptions;

Check failure on line 44 in tests/PhpWordTests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Property PhpOffice\PhpWordTests\SettingsTest::$pdfRendererOptions has no type specified.

private $pdfRendererPath;

private $tempDir;
Expand All @@ -56,6 +58,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();
Expand All @@ -70,6 +73,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);
Expand Down Expand Up @@ -124,6 +128,22 @@ 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());
self::assertNull(Settings::setPdfRendererOptions([

Check failure on line 139 in tests/PhpWordTests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method PHPUnit\Framework\Assert::assertNull() with void will always evaluate to false.

Check failure on line 139 in tests/PhpWordTests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Result of static method PhpOffice\PhpWord\Settings::setPdfRendererOptions() (void) is used.
'font' => 'Arial',
]));
self::assertEquals([
'font' => 'Arial',
], Settings::getPdfRendererOptions());
}

/**
* Test set/get measurement unit.
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/PhpWordTests/Writer/PDF/DomPDFTest.php
Expand Up @@ -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);

Check failure on line 88 in tests/PhpWordTests/Writer/PDF/DomPDFTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #2 $libraryBaseDir of static method PhpOffice\PhpWord\Settings::setPdfRenderer() expects string, string|false given.
Settings::setPdfRendererOptions([
'font' => 'Arial'
]);
$writer = new PDF(new PhpWord());
self::assertEquals('Arial', $writer->getFont());

Check failure on line 93 in tests/PhpWordTests/Writer/PDF/DomPDFTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method PhpOffice\PhpWord\Writer\PDF::getFont().
}
}
15 changes: 15 additions & 0 deletions tests/PhpWordTests/Writer/PDF/MPDFTest.php
Expand Up @@ -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);

Check failure on line 61 in tests/PhpWordTests/Writer/PDF/MPDFTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #2 $libraryBaseDir of static method PhpOffice\PhpWord\Settings::setPdfRenderer() expects string, string|false given.
Settings::setPdfRendererOptions([
'font' => 'Arial'
]);
$writer = new PDF(new PhpWord());
self::assertEquals('Arial', $writer->getFont());

Check failure on line 66 in tests/PhpWordTests/Writer/PDF/MPDFTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method PhpOffice\PhpWord\Writer\PDF::getFont().
}
}
15 changes: 15 additions & 0 deletions tests/PhpWordTests/Writer/PDF/TCPDFTest.php
Expand Up @@ -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());
}
}

0 comments on commit 3fa49d3

Please sign in to comment.