Skip to content

Commit 9d1fc0b

Browse files
committed
Allow adding save handlers to views
1 parent 4792811 commit 9d1fc0b

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/Pdf/View.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,20 @@ public function __construct(HtmlView $view, Typesetsh $pdf, bool $debug = false)
4444
$this->debug = $debug;
4545
}
4646

47-
public function debug(bool $flag = true)
47+
/**
48+
* Save handler are called prior to save and can manipulate
49+
* or append or validate the PDF document.
50+
*
51+
* @param callable(Pdf\Document) $saveHandler
52+
*/
53+
public function with(callable $saveHandler): self
54+
{
55+
$this->pdf = $this->pdf->with($saveHandler);
56+
57+
return $this;
58+
}
59+
60+
public function debug(bool $flag = true): self
4861
{
4962
$this->debug = $flag;
5063

src/Typesetsh.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,30 @@ class Typesetsh
2525
/** @var string */
2626
private $version;
2727

28-
public function __construct(callable $uriResolver = null, HtmlToPdf $html2pdf = null, string $version = '1.6')
28+
public function __construct(?callable $uriResolver = null, ?HtmlToPdf $html2pdf = null, string $version = '1.6')
2929
{
3030
$this->uriResolver = $uriResolver ?? UriResolver::httpOnly();
3131
$this->html2pdf = $html2pdf ?? new HtmlToPdf();
3232
$this->version = $version;
3333
}
3434

35+
/**
36+
* Save handler are called prior to save and can manipulate
37+
* or append or validate the PDF document.
38+
*
39+
* @param callable(Pdf\Document) $saveHandler
40+
*/
41+
public function with(callable $saveHandler): self
42+
{
43+
$name = "_with_". count($this->html2pdf->saveHandler);
44+
45+
$self = clone $this;
46+
$self->html2pdf->saveHandler[$name] = $saveHandler;
47+
48+
return $self;
49+
50+
}
51+
3552
public function render(string $html): Result
3653
{
3754
$result = $this->html2pdf->render($html, $this->uriResolver);
@@ -62,4 +79,10 @@ public function renderMultiple(array $html): Result
6279

6380
return $result;
6481
}
82+
83+
public function __clone(): void
84+
{
85+
$this->html2pdf = clone $this->html2pdf;
86+
$this->uriResolver = clone $this->uriResolver;
87+
}
6588
}

0 commit comments

Comments
 (0)