-
-
Notifications
You must be signed in to change notification settings - Fork 959
/
PDF.php
241 lines (211 loc) · 6.07 KB
/
PDF.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
namespace Barryvdh\DomPDF;
use Dompdf\Dompdf;
use Dompdf\Options;
use Exception;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Http\Response;
/**
* A Laravel wrapper for Dompdf
*
* @package laravel-dompdf
* @author Barry vd. Heuvel
*/
class PDF{
/** @var Dompdf */
protected $dompdf;
/** @var \Illuminate\Contracts\Config\Repository */
protected $config;
/** @var \Illuminate\Filesystem\Filesystem */
protected $files;
/** @var \Illuminate\Contracts\View\Factory */
protected $view;
protected $rendered = false;
protected $orientation;
protected $paper;
protected $showWarnings;
protected $public_path;
/**
* @param Dompdf $dompdf
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Illuminate\Contracts\View\Factory $view
*/
public function __construct(Dompdf $dompdf, ConfigRepository $config, Filesystem $files, ViewFactory $view){
$this->dompdf = $dompdf;
$this->config = $config;
$this->files = $files;
$this->view = $view;
$this->showWarnings = $this->config->get('dompdf.show_warnings', false);
}
/**
* Get the DomPDF instance
*
* @return Dompdf
*/
public function getDomPDF(){
return $this->dompdf;
}
/**
* Set the paper size (default A4)
*
* @param string $paper
* @param string $orientation
* @return $this
*/
public function setPaper($paper, $orientation = 'portrait'){
$this->paper = $paper;
$this->orientation = $orientation;
$this->dompdf->setPaper($paper, $orientation);
return $this;
}
/**
* Show or hide warnings
*
* @param bool $warnings
* @return $this
*/
public function setWarnings($warnings){
$this->showWarnings = $warnings;
return $this;
}
/**
* Load a HTML string
*
* @param string $string
* @param string $encoding Not used yet
* @return static
*/
public function loadHTML($string, $encoding = null){
$string = $this->convertEntities($string);
$this->dompdf->loadHtml($string, $encoding);
$this->rendered = false;
return $this;
}
/**
* Load a HTML file
*
* @param string $file
* @return static
*/
public function loadFile($file){
$this->dompdf->loadHtmlFile($file);
$this->rendered = false;
return $this;
}
/**
* Load a View and convert to HTML
*
* @param string $view
* @param array $data
* @param array $mergeData
* @param string $encoding Not used yet
* @return static
*/
public function loadView($view, $data = array(), $mergeData = array(), $encoding = null){
$html = $this->view->make($view, $data, $mergeData)->render();
return $this->loadHTML($html, $encoding);
}
/**
* Set/Change an option in DomPdf
*
* @param array $options
* @return static
*/
public function setOptions(array $options) {
$options = new Options($options);
$this->dompdf->setOptions($options);
return $this;
}
/**
* Output the PDF as a string.
*
* @return string The rendered PDF as string
*/
public function output(){
if(!$this->rendered){
$this->render();
}
return $this->dompdf->output();
}
/**
* Save the PDF to a file
*
* @param $filename
* @return static
*/
public function save($filename){
$this->files->put($filename, $this->output());
return $this;
}
/**
* Make the PDF downloadable by the user
*
* @param string $filename
* @return \Illuminate\Http\Response
*/
public function download($filename = 'document.pdf' ){
$output = $this->output();
return new Response($output, 200, array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="'.$filename.'"',
'Content-Length' => strlen($output),
));
}
/**
* Return a response with the PDF to show in the browser
*
* @param string $filename
* @return \Illuminate\Http\Response
*/
public function stream($filename = 'document.pdf' ){
$output = $this->output();
return new Response($output, 200, array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'.$filename.'"',
));
}
/**
* Render the PDF
*/
protected function render(){
if(!$this->dompdf){
throw new Exception('DOMPDF not created yet');
}
$this->dompdf->setPaper($this->paper, $this->orientation);
$this->dompdf->render();
if ( $this->showWarnings ) {
global $_dompdf_warnings;
if(!empty($_dompdf_warnings) && count($_dompdf_warnings)){
$warnings = '';
foreach ($_dompdf_warnings as $msg){
$warnings .= $msg . "\n";
}
// $warnings .= $this->dompdf->get_canvas()->get_cpdf()->messages;
if(!empty($warnings)){
throw new Exception($warnings);
}
}
}
$this->rendered = true;
}
protected function setEncryption($password) {
if (!$this->dompdf) {
throw new Exception("DOMPDF not created yet");
}
$this->render();
return $this->dompdf->getCanvas()->get_cpdf()->setEncryption("pass", $password);
}
protected function convertEntities($subject){
$entities = array(
'€' => '€',
'£' => '£',
);
foreach($entities as $search => $replace){
$subject = str_replace($search, $replace, $subject);
}
return $subject;
}
}