Skip to content

Commit

Permalink
Merge pull request #29 from Julian-B90/master
Browse files Browse the repository at this point in the history
convert tab to space, install mpdf 8
  • Loading branch information
robregonm authored Aug 2, 2019
2 parents af535ca + 8ef9131 commit 09485f1
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 105 deletions.
152 changes: 77 additions & 75 deletions PdfResponseFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,79 +22,81 @@
*/
class PdfResponseFormatter extends Component implements ResponseFormatterInterface
{
public $mode = '';

public $format = 'A4';

public $defaultFontSize = 0;

public $defaultFont = '';

public $marginLeft = 15;

public $marginRight = 15;

public $marginTop = 16;

public $marginBottom = 16;

public $marginHeader = 9;

public $marginFooter = 9;

/**
* @var string 'Landscape' or 'Portrait'
* Default to 'Portrait'
*/
public $orientation = 'P';

public $options = [];

/**
* @var Closure function($mpdf, $data){}
*/
public $beforeRender;

/**
* Formats the specified response.
*
* @param Response $response the response to be formatted.
*/
public function format($response)
{
$response->getHeaders()->set('Content-Type', 'application/pdf');
$response->content = $this->formatPdf($response);
}

/**
* Formats response HTML in PDF
*
* @param Response $response
*/
protected function formatPdf($response)
{
$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
);

foreach ($this->options as $key => $option) {
$mpdf->$key = $option;
}

if ($this->beforeRender instanceof \Closure) {
call_user_func($this->beforeRender, $mpdf, $response->data);
}

$mpdf->WriteHTML($response->data);
return $mpdf->Output('', 'S');
}
public $mode = '';

public $format = 'A4';

public $defaultFontSize = 0;

public $defaultFont = '';

public $marginLeft = 15;

public $marginRight = 15;

public $marginTop = 16;

public $marginBottom = 16;

public $marginHeader = 9;

public $marginFooter = 9;

/**
* @var string 'Landscape' or 'Portrait'
* Default to 'Portrait'
*/
public $orientation = 'P';

public $options = [];

/**
* @var Closure function($mpdf, $data){}
*/
public $beforeRender;

/**
* Formats the specified response.
*
* @param Response $response the response to be formatted.
*/
public function format($response)
{
$response->getHeaders()->set('Content-Type', 'application/pdf');
$response->content = $this->formatPdf($response);
}

/**
* Formats response HTML in PDF
*
* @param Response $response
*/
protected function formatPdf($response)
{
$mpdf = new \Mpdf\Mpdf([
'mode' => $this->mode,
'format' => $this->format,
'default_font_size' => $this->defaultFontSize,
'default_font' => $this->defaultFont,
'margin_left' => $this->marginLeft,
'margin_right' => $this->marginRight,
'margin_top' => $this->marginTop,
'margin_bottom' => $this->marginBottom,
'margin_header' => $this->marginHeader,
'margin_footer' => $this->marginFooter,
'orientation' => $this->orientation,
]
);

foreach ($this->options as $key => $option) {
$mpdf->$key = $option;
}

if ($this->beforeRender instanceof \Closure) {
call_user_func($this->beforeRender, $mpdf, $response->data);
}

$mpdf->WriteHTML($response->data);
return $mpdf->Output('', 'S');
}
}
68 changes: 38 additions & 30 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
{
"name": "robregonm/yii2-pdf",
"description": "Yii 2 PDF Response Formatter",
"keywords": ["yii", "yii2", "pdf", "response", "formatter", "reports", "documents"],
"type": "yii2-extension",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/robregonm/yii2-pdf/issues?state=open",
"forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/",
"source": "https://github.com/robregonm/yii2-pdf"
},
"authors": [
{
"name": "robregonm",
"email": "[email protected]",
"homepage": "http://obregon.co",
"role": "Author"
}
],
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "*",
"yiisoft/yii2-composer": "*",
"mpdf/mpdf": ">=5.7.0 <7.0.0"
},
"autoload": {
"psr-4": {
"robregonm\\pdf\\": ""
}
}
"name": "robregonm/yii2-pdf",
"description": "Yii 2 PDF Response Formatter",
"keywords": [
"yii",
"yii2",
"pdf",
"response",
"formatter",
"reports",
"documents"
],
"type": "yii2-extension",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/robregonm/yii2-pdf/issues?state=open",
"forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/",
"source": "https://github.com/robregonm/yii2-pdf"
},
"authors": [
{
"name": "robregonm",
"email": "[email protected]",
"homepage": "http://obregon.co",
"role": "Author"
}
],
"require": {
"php": "^5.6 || ~7.0.0 || ~7.1.0 || ~7.2.0 || ~7.3.0",
"yiisoft/yii2": "*",
"yiisoft/yii2-composer": "*",
"mpdf/mpdf": "^8.0"
},
"autoload": {
"psr-4": {
"robregonm\\pdf\\": ""
}
}
}

0 comments on commit 09485f1

Please sign in to comment.