Skip to content

allow user to set custom config for pdf rendering library (specially mpdf) #1640

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 1 commit 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
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
14 changes: 11 additions & 3 deletions src/PhpWord/Writer/PDF/MPDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
*/
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 +62,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() ?: array();
$pdf = new $mPdfClass($mPdfConfig);
}
$pdf->_setPageSize($paperSize, $orientation);
$pdf->addPage($orientation);

Expand Down Expand Up @@ -90,10 +98,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;
}
}