-
Notifications
You must be signed in to change notification settings - Fork 3
/
PdfBuilder.php
197 lines (172 loc) · 5.46 KB
/
PdfBuilder.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
<?php
namespace Omaralalwi\Gpdf\Builders;
use ArPHP\I18N\Arabic;
use Dompdf\Dompdf;
use Aws\Exception\AwsException;
use Omaralalwi\Gpdf\Clients\S3Client;
use Omaralalwi\Gpdf\Services\{S3Service, LocalFileService};
use Omaralalwi\Gpdf\Enums\GpdfSettingKeys;
use Omaralalwi\Gpdf\Traits\HasGpdfLog;
use Omaralalwi\Gpdf\Traits\HasFile;
class PdfBuilder
{
use HasGpdfLog, HasFile;
protected Dompdf $dompdf;
public function __construct(Dompdf $dompdf)
{
$this->dompdf = $dompdf;
}
/**
* Load HTML content into the Dompdf instance.
*
* @param string $htmlContent
* @return void
*/
public function load(string $htmlContent): void
{
$this->dompdf->loadHtml($htmlContent);
}
/**
* Render the PDF from the loaded HTML content.
*
* @return void
*/
public function render(): void
{
$this->dompdf->render();
}
/**
* Generate the PDF content as a string.
*
* @return string
*/
public function output(): string
{
return $this->dompdf->output();
}
/**
* Stream the generated PDF directly to the browser.
*
* @param string $fileName
* @param bool $attachment
* @param bool $newTab
* @return void
*/
public function stream(string $fileName, bool $attachment = false, bool $newTab = false): void
{
$options = [];
if ($attachment) {
$options['Attachment'] = true;
}
if ($newTab) {
$options['newtab'] = true;
}
$this->dompdf->stream($fileName, $options);
}
/**
* Load, render, and return the PDF content as a string.
*
* @param string $htmlContent
* @return string
*/
public function build(string $htmlContent): string
{
try {
$this->preparePdf($htmlContent);
return $this->output();
} catch (\Exception $e) {
return 'An error occurred while creating the PDF: ' . $e->getMessage();
}
}
/**
* Load, render, and stream the PDF directly to the browser.
*
* @param string $htmlContent
* @param string $fileName
* @param bool $attachment
* @return void
*/
public function buildAndStream(string $htmlContent, string $fileName, bool $attachment = false): void
{
try {
$this->preparePdf($htmlContent);
$this->stream($fileName, $attachment);
} catch (\Exception $e) {
echo 'An error occurred while streaming the PDF: ' . $e->getMessage();
}
}
public function buildAndStore(S3Service|LocalFileService $storageService, string $htmlContent, string $filePath, string $fileName, bool $withStream = false, bool $verifySsl=true)
{
try {
$this->preparePdf($htmlContent);
$pdfContent = $this->dompdf->output();
$generatedFile = $this->storeFile($storageService, $pdfContent, $filePath, $fileName);
$formattedGeneratedFile = $this->appendObjectURLToGeneratedFile($storageService, $generatedFile);
if($withStream) {
$storageService->streamFromUrl($formattedGeneratedFile['ObjectURL'], $verifySsl);
}
return $formattedGeneratedFile;
// stream from url not available with local storage driver.
} catch (\Exception $e) {
echo 'An error occurred while saving the PDF: ' . $e->getMessage();
}
}
public function appendObjectURLToGeneratedFile($storageService, $generatedFile)
{
$fileUrl = $storageService->getFileUrl($generatedFile);
$generatedFile['ObjectURL'] = $fileUrl;
return $generatedFile;
}
/**
* Prepare the PDF by formatting Arabic content, loading it into Dompdf, and rendering it.
*
* @param string $htmlContent
* @return void
*/
private function preparePdf(string $htmlContent): void
{
$formattedContent = $this->formatArabic($htmlContent);
$this->load($formattedContent);
$this->render();
}
public function formatArabic($htmlContent)
{
$Arabic = new Arabic();
$p = $Arabic->arIdentify($htmlContent);
for ($i = count($p)-1; $i >= 0; $i-=2) {
$utf8ar = $Arabic->utf8Glyphs(substr($htmlContent, $p[$i-1], $p[$i] - $p[$i-1]));
$htmlContent = substr_replace($htmlContent, $utf8ar, $p[$i-1], $p[$i] - $p[$i-1]);
}
return $this->convertEntities($htmlContent);
}
protected function convertEntities(string $subject): string
{
// if (false === $this->config->get('convert_entities', true)) {
// return $subject;
// }
$entities = [
'€' => '€',
'£' => '£',
];
foreach ($entities as $search => $replace) {
$subject = str_replace($search, $replace, $subject);
}
return $subject;
}
protected function storeFile(S3Service|LocalFileService $storageService, $pdfFile, $filePath, $fileName)
{
try {
return $storageService->store($pdfFile, $filePath, $fileName);
} catch (\Exception $e) {
echo $e->getMessage() . "\n";
}
}
protected function streamFromUrl(S3Service|LocalFileService $storageService, $fileUrl)
{
try {
return $storageService->streamFromUrl($fileUrl);
} catch (\Exception $e) {
echo $e->getMessage() . "\n";
}
}
}