Skip to content

Commit bc08568

Browse files
committed
feat: added setMutateContentBeforeLoadHtml hook
1 parent 8ba68c1 commit bc08568

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/PdfManager.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Collection;
77
use Illuminate\Support\Facades\Storage;
88
use Illuminate\Support\Str;
9+
use Throwable;
910

1011
class PdfManager
1112
{
@@ -33,6 +34,7 @@ class PdfManager
3334
PDF_DOWNLOAD = 'pdf_download',
3435
PDF_CONTENT = 'pdf_content',
3536
PDF_DEBUG_VIEW = 'debug_view';
37+
private ?\Closure $mutateContentBeforeLoadHtml = null;
3638

3739
public function __construct()
3840
{
@@ -213,6 +215,9 @@ public function getMarginLeft(): float
213215
return (float) ($this->marginLeft ?? config('pdf-manager.margin.left', 1));
214216
}
215217

218+
/**
219+
* @throws Throwable
220+
*/
216221
public function make(string $type = null)
217222
{
218223
$view = $this->getViewContent();
@@ -221,7 +226,7 @@ public function make(string $type = null)
221226
return $view;
222227
}
223228

224-
$pdf = $this->parseView($view);
229+
$pdf = $this->parseView();
225230

226231
switch ($type) {
227232
case self::PDF_STREAM:
@@ -236,10 +241,12 @@ public function make(string $type = null)
236241
}
237242
}
238243

244+
/**
245+
* @throws Throwable
246+
*/
239247
public function save(?string $path = null, ?string $disk = null): string
240248
{
241-
$view = $this->getViewContent();
242-
$pdf = $this->parseView($view);
249+
$pdf = $this->parseView();
243250
$disk ??= config('filesystems.default');
244251
$path = (Str::endsWith($path, '/') ? $path : $path . '/') . Str::uuid()->getHex() . '.pdf';
245252

@@ -248,6 +255,9 @@ public function save(?string $path = null, ?string $disk = null): string
248255
return $path;
249256
}
250257

258+
/**
259+
* @throws Throwable
260+
*/
251261
private function getViewContent(): string
252262
{
253263
return view($this->layout, [
@@ -294,8 +304,17 @@ private function pageCounterCentered(\Barryvdh\DomPDF\PDF $pdf, \Dompdf\FontMetr
294304
$this->pageCounterX = ($pdfWidth - $textWidth) / 2;
295305
}
296306

297-
private function parseView(string $view): \Barryvdh\DomPDF\PDF
307+
/**
308+
* @throws Throwable
309+
*/
310+
private function parseView(): \Barryvdh\DomPDF\PDF
298311
{
312+
$mutateContentBeforeLoadHtml = $this->mutateContentBeforeLoadHtml;
313+
314+
$view = $this->mutateContentBeforeLoadHtml instanceof \Closure
315+
? $mutateContentBeforeLoadHtml($this->getViewContent())
316+
: $this->getViewContent();
317+
299318
$pdf = Pdf::loadHTML($view)
300319
->setPaper(
301320
$this->getPaperSize(),
@@ -318,4 +337,11 @@ private function replaces(?string $text): ?string
318337
$text ?? ''
319338
);
320339
}
340+
341+
public function setMutateContentBeforeLoadHtml(\Closure $callback): PdfManager
342+
{
343+
$this->mutateContentBeforeLoadHtml = $callback;
344+
345+
return $this;
346+
}
321347
}

0 commit comments

Comments
 (0)