-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPdfConverter.php
160 lines (128 loc) · 4.2 KB
/
PdfConverter.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
<?php
declare(strict_types=1);
namespace App\Services;
use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;
use Spatie\Browsershot\Browsershot;
use Spatie\Browsershot\Exceptions\CouldNotTakeBrowsershot;
/**
* A Wrapper to puppeteer (headless chrome in NodeJs) USING https://github.com/spatie/browsershot
*
* possible combination for Google Chart: https://quickchart.io/documentation/google-charts-image-server/
*/
class PdfConverter
{
protected Browsershot $handler;
protected ?array $errors = [];
/**
* Must be called AFTER Browsershot::html or Browsershot::url
*/
public function setDefaultOptions($printAsScreen = true): Browsershot
{
foreach (config('pdf-converter.report.chrome-options') as $key => $option) {
$this->handler->setOption($key, $option);
}
$this->handler->addChromiumArguments(config('pdf-converter.report.chrome-arguments'));
$this->handler->timeout(360);
$this->handler->waitUntilNetworkIdle();
if ($printAsScreen) {
$this->handler->emulateMedia('screen'); // "screen", "print" (default) or null (passing null disables the emulation).
}
//$this->handler->setDelay(2000); // Not required
return $this->handler;
}
public function landscape(): self
{
$this->handler->landscape();
return $this;
}
/**
* Load a view file as the html to be converted into a pdf.
*/
public function loadView(string $view, array $data = [], array $mergeData = []): PdfConverter
{
$view = View::make($view, $data, $mergeData);
$this->handler = Browsershot::html($view->render());
// Will use the $this->handler saved by Browsershot::html($view);
$this->setDefaultOptions();
return $this;
}
public function hideBackground(): static
{
$this->handler->hideBackground();
return $this;
}
public function showBackground(): static
{
$this->handler->showBackground();
return $this;
}
/**
* Save the file once done converting.
*/
public function save(string $targetPath): Browsershot
{
try {
$this->handler->save($targetPath);
} catch (CouldNotTakeBrowsershot $e) {
throw new Exception('Não foi possível inicar a conversão para PDF');
}
$this->errors = $this->handler->consoleMessages();
$this->logErros($targetPath);
return $this->handler;
}
/**
* @throws Exception
*/
public function saveInTemp(string $fileName = null): string
{
$fileName = $fileName ?? Str::uuid()->toString().Str::random(32).'.pdf';
$targetPath = tempnam(sys_get_temp_dir(), $fileName);
$this->save($targetPath);
return $fileName;
}
/**
* Display the file within the browser itself.
*/
public function inline(string $fileName = 'file.pdf')
{
$this->embed($this->raw(), $fileName);
}
public function raw(): string
{
return $this->handler->pdf();
}
/**
* Force the browser to embed the PDF file.
*
* @throws Exception
*/
public function embed(string $pdfContent, string $fileName = 'arquivo.pdf', bool $exit = true)
{
if (headers_sent()) {
throw new Exception('Headers have already been sent');
}
if (empty($fileName)) {
throw new Exception('Please specify a valid filename');
}
header('Content-type: application/pdf');
header('Cache-control: public, must-revalidate, max-age=0');
header('Pragme: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d m Y H:i:s').' GMT');
header('Content-Length: '.strlen($pdfContent));
header('Content-Disposition: inline; filename="'.basename($fileName).'";');
echo $pdfContent;
if ($exit === true) {
exit;
}
}
protected function logErros($fileName): void
{
if (! empty($this->errors)) {
Log::error("Pdf error - {$fileName}", $this->errors);
}
}
}