forked from bariew/yii2-dompdf-formatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResponseFormatter.php
59 lines (52 loc) · 1.19 KB
/
ResponseFormatter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* ResponseFormatter class file.
* @copyright (c) 2015, Galament
* @license http://www.opensource.org/licenses/bsd-license.php
*/
namespace bariew\yii2Dompdf;
use DOMPDF;
use Yii;
use yii\base\Component;
use yii\web\Response;
use yii\web\ResponseFormatterInterface;
/**
* Description.
*
* Usage:
* @author Pavel Bariev <[email protected]>
*/
class ResponseFormatter extends Component implements ResponseFormatterInterface
{
public $options = [];
/**
* @var \Closure function($pdf, $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
* @return string
*/
protected function formatPdf($response)
{
define('DOMPDF_ENABLE_REMOTE', true);
define('DOMPDF_UNICODE_ENABLED', true);
require_once Yii::getAlias('@vendor/bariew/dompdf/dompdf_config.inc.php');
$pdf = new DOMPDF();
$pdf->load_html($response->data);
$pdf->render();
return $pdf->output();
}
}