-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDF.php
186 lines (159 loc) · 5.58 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
<?php
declare(strict_types=1);
namespace Tamedevelopers\Support;
use Exception;
use Dompdf\Dompdf;
use Dompdf\Options;
use Tamedevelopers\Support\Env;
use Tamedevelopers\Support\Server;
use Tamedevelopers\Support\Capsule\Manager;
use Tamedevelopers\Support\Capsule\CustomException;
/**
* DOM PDF Wrapper
* Usage: composer require dompdf/dompdf
* @link https://github.com/dompdf/dompdf/blob/v0.8.2/src/Adapter/CPDF.php#L45
* @link https://github.com/dompdf/dompdf
*/
class PDF{
/**
* dompdf
*
* @var mixed
*/
static private $dompdf;
/**
* options
*
* @var array
*/
static private $options = [];
/**
* init
*
* @return void
*/
static private function init()
{
$options = self::isDOMPDFInstalled();
$options->set('defaultMediaType', 'all');
$options->set('chroot', Server::cleanServerPath( public_path('\\') ) );
$options->set('isFontSubsettingEnabled', self::$options['isFontSubsettingEnabled']);
$options->set('isHtml5ParserEnabled', self::$options['isHtml5ParserEnabled']);
$options->set('isRemoteEnabled', self::$options['isRemoteEnabled']);
$options->set('httpContext', [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed'=> true
]
]);
// instantiate and use the \Dompdf\Dompdf() class
self::$dompdf = new Dompdf($options);
}
/**
* Create PDF From HTML STRING
*
* @param array $options
* - Keys [content|paper_size|paper_type|output|destination]
*
* - `paper_type` key value --- [portrait|landscape]
* - `paper_size` key value --- [letter|legal|A4]
* - `output` key value --- [view|save|download]
*
* @return void
*/
static public function create(array $options = [])
{
self::$options = array_merge([
'content' => '',
'paper_size' => 'A4',
'paper_type' => 'portrait',
'destination' => strtotime('now') . '.pdf',
'output' => 'preview',
'title' => null,
'delete' => true,
'isRemoteEnabled' => false,
'isFontSubsettingEnabled' => true,
'isHtml5ParserEnabled' => true,
], $options);
self::init();
// Get the HTML content
$content = self::$options['content'];
// if title is empty, then use the file name as title
if(empty(self::$options['title'])){
self::$options['title'] = pathinfo(self::$options['destination'], PATHINFO_FILENAME);
}
// Add the title tag if <html> or <head> is not present
if (!empty(self::$options['title'])) {
// Check if the content contains <html> or <head> tags
if (strpos($content, '<html>') === false && strpos($content, '<head>') === false) {
$content = '<html><head><title>' . htmlspecialchars(self::$options['title']) . '</title></head><body>' . $content . '</body></html>';
}
}
// pass html content
self::$dompdf->loadHtml($content);
// 'letter', 'legal', 'A4' | array(0,0,609.4488,935.433)
self::$dompdf->setPaper(self::$options['paper_size'], self::$options['paper_type']);
// Render the HTML as PDF
self::$dompdf->render();
// Render PDF output to browser without saving
if(in_array(self::$options['output'], ['preview', 'view']))
{
@file_put_contents(self::$options['destination'], self::$dompdf->output());
// for reading to browser as well, from package
// self::$dompdf->stream(self::$options['destination'], array("Attachment" => false));
// render output to browser
Tame::readPDFToBrowser(self::$options['destination'], self::$options['delete']);
}
// Save PDF to server only
elseif(in_array(self::$options['output'], ['save', 'saves', 'getsave']))
{
@file_put_contents(self::$options['destination'], self::$dompdf->output());
}
// Stream PDF to browser for download
elseif(in_array(self::$options['output'], ['download', 'downloads']))
{
self::$dompdf->stream();
}
}
/**
* READ PDF To Server
*
* @param string $path
* [Absolute path to PDF file]
*
* @return void
*/
static public function read(string $path)
{
Tame::readPDFToBrowser($path);
}
/**
* Check If DOM PDF has been installed
*
* @return mixed
*/
static private function isDOMPDFInstalled()
{
try {
if (class_exists('Dompdf\Options')) {
// instantiate and use the \Dompdf\Options() class
return new Options();
} else {
throw new CustomException(
"Class Dompdf\Options not found: \nRequire the package by running: `composer require dompdf/dompdf`\n" .
(new Exception)->getTraceAsString()
);
}
} catch (CustomException $e) {
// Handle the exception silently (turn off error reporting)
error_reporting(0);
Manager::setHeaders(404, function() use($e){
// create error logger
Env::bootLogger();
// Trigger a custom error
trigger_error($e->getMessage(), E_USER_ERROR);
});
}
}
}