Skip to content

Commit

Permalink
Enable mPdf config to be set via options reverts #58
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Jun 18, 2017
1 parent 81a8cf4 commit efa2a84
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 40 deletions.
1 change: 0 additions & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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
56 changes: 17 additions & 39 deletions Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ 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 Down Expand Up @@ -179,7 +175,7 @@ class Pdf extends Component
*/
public $methods = '';
/**
* @var string the Mpdf configuration options entered as `$key => value` pairs, where:
* @var array 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 Down Expand Up @@ -255,7 +251,6 @@ public function initTempPaths()
*/
public function render()
{
$this->configure($this->options);
if (!empty($this->methods)) {
foreach ($this->methods as $method => $param) {
$this->execute($method, $param);
Expand All @@ -282,19 +277,22 @@ public function getApi()
*/
public function setApi()
{
$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
]);
$config = array_replace_recursive(
$this->options,
[
'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;
}
Expand Down Expand Up @@ -341,26 +339,6 @@ public function addPdfAttachment($filePath)
$this->_pdfAttachments[] = $filePath;
}

/**
* Configures Mpdf options
*
* @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
*/
public function configure($options = [])
{
if (empty($options)) {
return;
}
$api = $this->getApi();
foreach ($options as $key => $value) {
if (property_exists($api, $key)) {
$api->$key = $value;
}
}
}

/**
* Calls the Mpdf method with parameters
*
Expand Down

0 comments on commit efa2a84

Please sign in to comment.