Skip to content

Add custom font for Dompdf libary #2262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 3 additions & 2 deletions src/PhpWord/IOFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ abstract class IOFactory
* Create new writer.
*
* @param string $name
* @param array $config
*
* @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, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF'], true)) {
throw new Exception("\"{$name}\" is not a valid writer.");
}

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

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

/**
Expand Down
7 changes: 6 additions & 1 deletion src/PhpWord/Writer/PDF/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ 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) {
Expand All @@ -93,6 +97,7 @@ public function __construct(PhpWord $phpWord)
// @codeCoverageIgnoreEnd
}
}
if(isset($config['font']) && empty($config['font'])) $this->font = $config['font'];
}

/**
Expand Down
11 changes: 10 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,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;
}

/**
Expand Down