Skip to content

Commit

Permalink
Fix #58: New property initConfig to initialize other Mpdf initial c…
Browse files Browse the repository at this point in the history
…onfiguration properties
  • Loading branch information
kartik-v committed Jun 17, 2017
1 parent 99f672a commit 81a8cf4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Change Log: `yii2-mpdf`

**Date:** 17-Jun-2017

- (enh #58): New property `initConfig` to initialize other Mpdf initial configuration properties.
- (enh #45, #51): Updates dependency to use latest mPdf 7.x development branch.

## Version 1.0.1
Expand Down
76 changes: 42 additions & 34 deletions Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace kartik\mpdf;

use mPDF;
use Mpdf\Mpdf;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
Expand All @@ -17,7 +17,7 @@
/**
* The Pdf class is a Yii2 component that allows to convert HTML content to portable document format (PDF). It allows
* configuration of how the PDF document is generated and how it should be delivered to the user. This component uses
* the [[mPDF]] library and includes various additional enhancements specifically for the Yii2 framework.
* the [[Mpdf]] library and includes various additional enhancements specifically for the Yii2 framework.
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @since 1.0
Expand Down Expand Up @@ -142,6 +142,10 @@ class Pdf extends Component
* @var string specifies the default page orientation of the new document.
*/
public $orientation = self::ORIENT_PORTRAIT;
/**
* @var array additional Mpdf initialization configuration properties. See [[Mpdf]] documentation.
*/
public $initConfig = [];
/**
* @var string css file to prepend to the PDF
*/
Expand All @@ -168,14 +172,14 @@ class Pdf extends Component
*/
public $tempPath;
/**
* @var array the mPDF methods that will called in the sequence listed before rendering the content. Should be an
* @var array the Mpdf methods that will called in the sequence listed before rendering the content. Should be an
* associative array entered as `$method => $params` pairs, where:
* - `$method`: _string_, is the mPDF method / function name
* - `$param`: _mixed_, are the mPDF method parameters
* - `$method`: _string_, is the Mpdf method / function name
* - `$param`: _mixed_, are the Mpdf method parameters
*/
public $methods = '';
/**
* @var string the mPDF configuration options entered as `$key => value` pairs, where:
* @var string the Mpdf configuration options entered as `$key => value` pairs, where:
* - `$key`: _string_, is the configuration property name
* - `$value`: _mixed_, is the configured property value
*/
Expand All @@ -185,7 +189,7 @@ class Pdf extends Component
'tabSpaces' => 4,
];
/**
* @var mPDF api instance
* @var Mpdf api instance
*/
protected $_mpdf;
/**
Expand All @@ -199,9 +203,9 @@ class Pdf extends Component
protected $_pdfAttachments;

/**
* Defines a mPDF temporary path if not set.
* Defines a Mpdf temporary path if not set.
*
* @param string $prop the mPDF constant to define
* @param string $prop the Mpdf constant to define
* @param string $dir the directory to create
*
* @throws InvalidConfigException
Expand Down Expand Up @@ -232,7 +236,7 @@ public function init()
}

/**
* Initialize folder paths to allow [[mPDF]] to write temporary data.
* Initialize folder paths to allow [[Mpdf]] to write temporary data.
*/
public function initTempPaths()
{
Expand Down Expand Up @@ -261,36 +265,40 @@ public function render()
}

/**
* Validates and fetches the mPDF API instance.
* Validates and fetches the Mpdf API instance.
*
* @return mPDF
* @return Mpdf
*/
public function getApi()
{
if (empty($this->_mpdf) || !$this->_mpdf instanceof mPDF) {
if (empty($this->_mpdf) || !$this->_mpdf instanceof Mpdf) {
$this->setApi();
}
return $this->_mpdf;
}

/**
* Sets the mPDF API instance
* Sets the Mpdf API instance
*/
public function setApi()
{
$this->_mpdf = new mPDF(
$this->mode,
$this->format,
$this->defaultFontSize,
$this->defaultFont,
$this->marginLeft,
$this->marginRight,
$this->marginTop,
$this->marginBottom,
$this->marginHeader,
$this->marginFooter,
$this->orientation
);
$config = array_replace_recursive($this->initConfig, [
'mode' => $this->mode,
'format' => $this->format,
'default_font_size' => $this->defaultFontSize,
'default_font' => $this->defaultFont,
'mgl' => $this->marginLeft,
'mgr' => $this->marginRight,
'mgt' => $this->marginTop,
'mgb' => $this->marginBottom,
'mgh' => $this->marginHeader,
'mgf' => $this->marginFooter,
'orientation' => $this->orientation
]);
if (isset($this->tempPath) && is_dir($this->tempPath)) {
$config['tempDir'] = $this->tempPath;
}
$this->_mpdf = new Mpdf($config);
}

/**
Expand Down Expand Up @@ -334,9 +342,9 @@ public function addPdfAttachment($filePath)
}

/**
* Configures mPDF options
* Configures Mpdf options
*
* @param array $options the mPDF configuration options entered as `$key => value` pairs, where:
* @param array $options the Mpdf configuration options entered as `$key => value` pairs, where:
* - `$key`: _string_, is the configuration property name
* - `$value`: _mixed_, is the configured property value
*/
Expand All @@ -354,10 +362,10 @@ public function configure($options = [])
}

/**
* Calls the mPDF method with parameters
* Calls the Mpdf method with parameters
*
* @param string $method the mPDF method / function name
* @param array $params the mPDF parameters
* @param string $method the Mpdf method / function name
* @param array $params the Mpdf parameters
*
* @return mixed
* @throws InvalidParamException
Expand All @@ -366,7 +374,7 @@ public function execute($method, $params = [])
{
$api = $this->getApi();
if (!method_exists($api, $method)) {
throw new InvalidParamException("Invalid or undefined mPDF method '{$method}' passed to 'Pdf::execute'.");
throw new InvalidParamException("Invalid or undefined Mpdf method '{$method}' passed to 'Pdf::execute'.");
}
if (!is_array($params)) {
$params = [$params];
Expand Down Expand Up @@ -422,7 +430,7 @@ protected function parseFormat()
/**
* Appends the given attachment to the generated PDF
*
* @param mPDF $api the mPDF API instance
* @param Mpdf $api the Mpdf API instance
* @param string $attachment the attachment name
*/
private function writePdfAttachment($api, $attachment)
Expand Down

0 comments on commit 81a8cf4

Please sign in to comment.