Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/1.x/1.2.0.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
14 changes: 12 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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

-
Expand Down Expand Up @@ -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

-
Expand All @@ -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

Expand All @@ -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

Expand Down
23 changes: 23 additions & 0 deletions src/PhpWord/Settings.php
Original file line number Diff line number Diff line change
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
7 changes: 7 additions & 0 deletions src/PhpWord/Writer/PDF/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -93,6 +94,12 @@ public function __construct(PhpWord $phpWord)
// @codeCoverageIgnoreEnd
}
}

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

/**
Expand Down
8 changes: 7 additions & 1 deletion src/PhpWord/Writer/PDF/DomPDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace PhpOffice\PhpWord\Writer\PDF;

use Dompdf\Dompdf as DompdfLib;
use Dompdf\Options;
use PhpOffice\PhpWord\Writer\WriterInterface;

/**
Expand All @@ -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);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/PhpWord/Writer/PDF/MPDF.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
24 changes: 24 additions & 0 deletions tests/PhpWordTests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class SettingsTest extends TestCase

private $pdfRendererName;

/**
* @var array
*/
private $pdfRendererOptions;

private $pdfRendererPath;

private $tempDir;
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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.
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/PhpWordTests/Writer/PDF/DomPDFTest.php
Original file line number Diff line number Diff line change
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);
Settings::setPdfRendererOptions([
'font' => 'Arial',
]);
$writer = new PDF(new PhpWord());
self::assertEquals('Arial', $writer->getFont());
}
}
15 changes: 15 additions & 0 deletions tests/PhpWordTests/Writer/PDF/MPDFTest.php
Original file line number Diff line number Diff line change
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);
Settings::setPdfRendererOptions([
'font' => 'Arial',
]);
$writer = new PDF(new PhpWord());
self::assertEquals('Arial', $writer->getFont());
}
}
15 changes: 15 additions & 0 deletions tests/PhpWordTests/Writer/PDF/TCPDFTest.php
Original file line number Diff line number Diff line change
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());
}
}