Skip to content

Commit

Permalink
allow user to set custom config for pdf rendering library (specially …
Browse files Browse the repository at this point in the history
…mpdf)

Issue on mpdf library : mpdf/mpdf#701 (comment)

Rejected PR on mpdf library : mpdf/mpdf#1047
  • Loading branch information
Alexandre Cousin committed Jun 19, 2019
1 parent 7b7d4e4 commit 902c842
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/PhpWord/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ class Settings
*/
private static $pdfRendererPath = null;

/**
* Config to the external Library used for rendering PDF files
*
* @var string
*/
private static $pdfRendererConfig = null;

/**
* Measurement unit
*
Expand Down Expand Up @@ -230,6 +237,19 @@ public static function setPdfRendererName($libraryName)
return true;
}

/**
* Set the config to use for the PDF Rendering Library.
*
* @param array $libraryConfig
* @return string
*/
public static function setPdfRendererConfig($libraryConfig)
{
self::$pdfRendererConfig = $libraryConfig;

return true;
}

/**
* Return the directory path to the PDF Rendering Library.
*
Expand All @@ -240,6 +260,16 @@ public static function getPdfRendererPath()
return self::$pdfRendererPath;
}

/**
* Return the config to instantiate PDF Rendering Library.
*
* @return array
*/
public static function getPdfRendererConfig()
{
return self::$pdfRendererConfig;
}

/**
* Location of external library to use for rendering PDF files
*
Expand Down
13 changes: 10 additions & 3 deletions src/PhpWord/Writer/PDF/MPDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
class MPDF extends AbstractRenderer implements WriterInterface
{
const MPDF_5 = '\mpdf';
const MPDF_6 = '\Mpdf\Mpdf';
/**
* Overridden to set the correct includefile, only needed for MPDF 5
*
Expand Down Expand Up @@ -59,7 +61,12 @@ public function save($filename = null)

// Create PDF
$mPdfClass = $this->getMPdfClassName();
$pdf = new $mPdfClass();
if (self::MPDF_5 === $mPdfClass) {
$pdf = new $mPdfClass();
} else {
$mPdfConfig = Settings::getPdfRendererConfig() ?: [];
$pdf = new $mPdfClass($mPdfConfig);
}
$pdf->_setPageSize($paperSize, $orientation);
$pdf->addPage($orientation);

Expand Down Expand Up @@ -90,10 +97,10 @@ private function getMPdfClassName()
{
if ($this->includeFile != null) {
// MPDF version 5.*
return '\mpdf';
return self::MPDF_5;
}

// MPDF version > 6.*
return '\Mpdf\Mpdf';
return self::MPDF_6;
}
}

0 comments on commit 902c842

Please sign in to comment.