-
Notifications
You must be signed in to change notification settings - Fork 4
/
PdfFile.php
174 lines (151 loc) · 4.66 KB
/
PdfFile.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* PdfFile
*
* PdfFile represents a PDF document.
*
* It extends WkHtmlToPdf and adds methods to render PDF pages from view files.
*
* To render the view files the current controller must be set in $controller.
* Alternatively - e.g. for offline rendering - $layout, $layoutPath and $viewPath
* can be set.
*
* @author Michael Härtl <[email protected]>
* @version 1.0.0
* @license http://www.opensource.org/licenses/MIT
*/
require_once(__DIR__.'/WkHtmlToPdf.php');
class PdfFile extends WkHtmlToPdf
{
/**
* @var mixed the current controller. If null, $layoutPath and $viewPath must be set.
*/
public $controller;
/**
* @var string the layout name.
*/
public $layout;
/**
* @var string path to the layout directory. If empty, no layout is applied.
*/
public $layoutPath;
/**
* @var string path to the base view directory
*/
public $baseViewPath;
/**
* @var string path to the controller view directory
*/
public $viewPath;
/**
* @var array default PDF options
*/
public $defaultOptions = array(
'no-outline',
'encoding' => 'UTF-8',
'margin-top' => 0,
'margin-right' => 0,
'margin-bottom' => 0,
'margin-left' => 0,
);
/**
* @var array default PDF page options
*/
public $defaultPageOptions = array(
'disable-smart-shrinking',
);
/**
* Either set supplied options/pageOptions (can also be empty array) or use default
* options/pageOptions.
*
* @param array $options global options for wkhtmltopdf.
* @param array $pageOptions page options for wkhtmltopdf.
*/
public function __construct($options=null, $pageOptions=null)
{
$this->setOptions($options===null ? $this->defaultOptions : $options);
$this->setPageOptions($pageOptions===null ? $this->defaultPageOptions : $pageOptions);
}
/**
* Render a single view as PDF and add it to this document
*
* @param string $view name of the view to be rendered. See CController::render.
* @param array $data view data. See CController::render.
* @param array $options options for this page. See 'wkhtml -H' for available page options
*/
public function renderPage($view, $data=array(), $options=array())
{
$this->addPage($this->render($view, $data), $options);
}
/**
* Render a view file and return result
*
* If $controller is not set, the (localized) view file is searched in $viewPath
* and the layout in $layoutFile is applied, if set.
*
* @param string $view name of view to render
* @param array $data view data
* @return string rendered content
*/
private function render($view, $data=array())
{
if(($controller=$this->controller)===null)
{
static $controller;
if($controller===null)
$controller = new CController('pdffile');
$controller->layout = $this->layout;
// Required to make console app play nicely during rendering
Yii::app()->attachBehavior('consoleWorkaround', array(
'class' => 'ConsoleAppWorkaround',
'_viewPath' => $this->baseViewPath,
'_layoutPath' => $this->layoutPath,
));
$file = Yii::app()->findLocalizedFile($this->viewPath.'/'.$view.'.php');
if($file===false)
throw new CException('Could not find view file '.$view);
ini_set('implicit_flush', false);
$content = $controller->renderInternal($file, $data, true);
if($controller->layout===null)
return $content;
return $controller->renderInternal($controller->getLayoutFile($controller->layout), array(
'content' => $content,
), true);
}
else
return $controller->render($view, $data, true);
}
}
/**
* ConsoleAppWorkaround
*
* Workaround some limitations of console applications.
*/
class ConsoleAppWorkaround extends CBehavior
{
public $_layoutPath;
public $_viewPath;
private $_factory;
public function getWidgetFactory()
{
if($this->_factory===null)
$this->_factory = Yii::createComponent(array('class'=>'CWidgetFactory'));
return $this->_factory;
}
public function getLayoutPath()
{
return $this->_layoutPath;
}
public function getViewPath()
{
return $this->_viewPath;
}
public function getTheme()
{
return null;
}
public function getViewRenderer()
{
return null;
}
}