1
1
<?php
2
+
2
3
namespace Barryvdh \DomPDF ;
3
4
4
5
use Dompdf \Dompdf ;
15
16
* @package laravel-dompdf
16
17
* @author Barry vd. Heuvel
17
18
*/
18
- class PDF {
19
+ class PDF
20
+ {
19
21
20
22
/** @var Dompdf */
21
23
protected $ dompdf ;
@@ -39,7 +41,8 @@ class PDF{
39
41
* @param \Illuminate\Filesystem\Filesystem $files
40
42
* @param \Illuminate\Contracts\View\Factory $view
41
43
*/
42
- public function __construct (Dompdf $ dompdf , ConfigRepository $ config , Filesystem $ files , ViewFactory $ view ){
44
+ public function __construct (Dompdf $ dompdf , ConfigRepository $ config , Filesystem $ files , ViewFactory $ view )
45
+ {
43
46
$ this ->dompdf = $ dompdf ;
44
47
$ this ->config = $ config ;
45
48
$ this ->files = $ files ;
@@ -53,7 +56,8 @@ public function __construct(Dompdf $dompdf, ConfigRepository $config, Filesystem
53
56
*
54
57
* @return Dompdf
55
58
*/
56
- public function getDomPDF (){
59
+ public function getDomPDF ()
60
+ {
57
61
return $ this ->dompdf ;
58
62
}
59
63
@@ -64,7 +68,8 @@ public function getDomPDF(){
64
68
* @param string $orientation
65
69
* @return $this
66
70
*/
67
- public function setPaper ($ paper , $ orientation = 'portrait ' ){
71
+ public function setPaper ($ paper , $ orientation = 'portrait ' )
72
+ {
68
73
$ this ->dompdf ->setPaper ($ paper , $ orientation );
69
74
return $ this ;
70
75
}
@@ -75,7 +80,8 @@ public function setPaper($paper, $orientation = 'portrait'){
75
80
* @param bool $warnings
76
81
* @return $this
77
82
*/
78
- public function setWarnings ($ warnings ){
83
+ public function setWarnings ($ warnings )
84
+ {
79
85
$ this ->showWarnings = $ warnings ;
80
86
return $ this ;
81
87
}
@@ -87,7 +93,8 @@ public function setWarnings($warnings){
87
93
* @param string $encoding Not used yet
88
94
* @return static
89
95
*/
90
- public function loadHTML ($ string , $ encoding = null ){
96
+ public function loadHTML ($ string , $ encoding = null )
97
+ {
91
98
$ string = $ this ->convertEntities ($ string );
92
99
$ this ->dompdf ->loadHtml ($ string , $ encoding );
93
100
$ this ->rendered = false ;
@@ -100,7 +107,8 @@ public function loadHTML($string, $encoding = null){
100
107
* @param string $file
101
108
* @return static
102
109
*/
103
- public function loadFile ($ file ){
110
+ public function loadFile ($ file )
111
+ {
104
112
$ this ->dompdf ->loadHtmlFile ($ file );
105
113
$ this ->rendered = false ;
106
114
return $ this ;
@@ -112,8 +120,9 @@ public function loadFile($file){
112
120
* @param array $info
113
121
* @return static
114
122
*/
115
- public function addInfo ($ info ){
116
- foreach ($ info as $ name =>$ value ){
123
+ public function addInfo ($ info )
124
+ {
125
+ foreach ($ info as $ name => $ value ) {
117
126
$ this ->dompdf ->add_info ($ name , $ value );
118
127
}
119
128
return $ this ;
@@ -128,7 +137,8 @@ public function addInfo($info){
128
137
* @param string $encoding Not used yet
129
138
* @return static
130
139
*/
131
- public function loadView ($ view , $ data = array (), $ mergeData = array (), $ encoding = null ){
140
+ public function loadView ($ view , $ data = array (), $ mergeData = array (), $ encoding = null )
141
+ {
132
142
$ html = $ this ->view ->make ($ view , $ data , $ mergeData )->render ();
133
143
return $ this ->loadHTML ($ html , $ encoding );
134
144
}
@@ -139,7 +149,8 @@ public function loadView($view, $data = array(), $mergeData = array(), $encoding
139
149
* @param array $options
140
150
* @return static
141
151
*/
142
- public function setOptions (array $ options ) {
152
+ public function setOptions (array $ options )
153
+ {
143
154
$ options = new Options ($ options );
144
155
$ this ->dompdf ->setOptions ($ options );
145
156
return $ this ;
@@ -150,8 +161,9 @@ public function setOptions(array $options) {
150
161
*
151
162
* @return string The rendered PDF as string
152
163
*/
153
- public function output (){
154
- if (!$ this ->rendered ){
164
+ public function output ()
165
+ {
166
+ if (!$ this ->rendered ) {
155
167
$ this ->render ();
156
168
}
157
169
return $ this ->dompdf ->output ();
@@ -163,7 +175,8 @@ public function output(){
163
175
* @param $filename
164
176
* @return static
165
177
*/
166
- public function save ($ filename ){
178
+ public function save ($ filename )
179
+ {
167
180
$ this ->files ->put ($ filename , $ this ->output ());
168
181
return $ this ;
169
182
}
@@ -174,11 +187,12 @@ public function save($filename){
174
187
* @param string $filename
175
188
* @return \Illuminate\Http\Response
176
189
*/
177
- public function download ($ filename = 'document.pdf ' ){
190
+ public function download ($ filename = 'document.pdf ' )
191
+ {
178
192
$ output = $ this ->output ();
179
193
return new Response ($ output , 200 , array (
180
194
'Content-Type ' => 'application/pdf ' ,
181
- 'Content-Disposition ' => 'attachment; filename=" ' . $ filename. '" ' ,
195
+ 'Content-Disposition ' => 'attachment; filename=" ' . $ filename . '" ' ,
182
196
'Content-Length ' => strlen ($ output ),
183
197
));
184
198
}
@@ -189,33 +203,35 @@ public function download($filename = 'document.pdf' ){
189
203
* @param string $filename
190
204
* @return \Illuminate\Http\Response
191
205
*/
192
- public function stream ($ filename = 'document.pdf ' ){
206
+ public function stream ($ filename = 'document.pdf ' )
207
+ {
193
208
$ output = $ this ->output ();
194
209
return new Response ($ output , 200 , array (
195
210
'Content-Type ' => 'application/pdf ' ,
196
- 'Content-Disposition ' => 'inline; filename=" ' . $ filename. '" ' ,
211
+ 'Content-Disposition ' => 'inline; filename=" ' . $ filename . '" ' ,
197
212
));
198
213
}
199
214
200
215
/**
201
216
* Render the PDF
202
217
*/
203
- protected function render (){
204
- if (!$ this ->dompdf ){
218
+ protected function render ()
219
+ {
220
+ if (!$ this ->dompdf ) {
205
221
throw new Exception ('DOMPDF not created yet ' );
206
222
}
207
223
208
224
$ this ->dompdf ->render ();
209
225
210
- if ( $ this ->showWarnings ) {
226
+ if ($ this ->showWarnings ) {
211
227
global $ _dompdf_warnings ;
212
- if (!empty ($ _dompdf_warnings ) && count ($ _dompdf_warnings )){
228
+ if (!empty ($ _dompdf_warnings ) && count ($ _dompdf_warnings )) {
213
229
$ warnings = '' ;
214
- foreach ($ _dompdf_warnings as $ msg ){
230
+ foreach ($ _dompdf_warnings as $ msg ) {
215
231
$ warnings .= $ msg . "\n" ;
216
232
}
217
233
// $warnings .= $this->dompdf->get_canvas()->get_cpdf()->messages;
218
- if (!empty ($ warnings )){
234
+ if (!empty ($ warnings )) {
219
235
throw new Exception ($ warnings );
220
236
}
221
237
}
@@ -224,25 +240,26 @@ protected function render(){
224
240
}
225
241
226
242
227
- public function setEncryption ($ password ) {
228
- if (!$ this ->dompdf ) {
229
- throw new Exception ("DOMPDF not created yet " );
230
- }
231
- $ this ->render ();
232
- return $ this ->dompdf ->getCanvas ()->get_cpdf ()->setEncryption ("pass " , $ password );
243
+ public function setEncryption ($ password )
244
+ {
245
+ if (!$ this ->dompdf ) {
246
+ throw new Exception ("DOMPDF not created yet " );
247
+ }
248
+ $ this ->render ();
249
+ return $ this ->dompdf ->getCanvas ()->get_cpdf ()->setEncryption ("pass " , $ password );
233
250
}
234
251
235
252
236
- protected function convertEntities ($ subject ){
253
+ protected function convertEntities ($ subject )
254
+ {
237
255
$ entities = array (
238
256
'€ ' => '€ ' ,
239
257
'£ ' => '£ ' ,
240
258
);
241
259
242
- foreach ($ entities as $ search => $ replace ){
260
+ foreach ($ entities as $ search => $ replace ) {
243
261
$ subject = str_replace ($ search , $ replace , $ subject );
244
262
}
245
263
return $ subject ;
246
264
}
247
-
248
265
}
0 commit comments