-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChromePhpRenderer.php
153 lines (121 loc) · 4.47 KB
/
ChromePhpRenderer.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
<?php
namespace RowBloom\ChromePhpRenderer;
use HeadlessChromium\BrowserFactory;
use RowBloom\RowBloom\Config;
use RowBloom\RowBloom\Fs\File;
use RowBloom\RowBloom\Options;
use RowBloom\RowBloom\Renderers\Contract as RenderersContract;
use RowBloom\RowBloom\Types\Css;
use RowBloom\RowBloom\Types\Html;
class ChromePhpRenderer implements RenderersContract
{
public const NAME = 'Chrome';
protected string $rendering;
protected Html $html;
protected Css $css;
protected Options $options;
protected array $phpChromeOptions = [];
public function __construct(protected ?Config $config = null)
{
}
public function get(): string
{
return $this->rendering;
}
public function save(File $file): bool
{
return $file->mustBeExtension('pdf')
->startSaving()
->streamFilterAppend('convert.base64-decode')
->save($this->rendering);
}
public function render(Html $html, Css $css, Options $options, ?Config $config = null): static
{
$this->html = $html;
$this->css = $css;
$this->options = $options;
$this->config = $config ?? $this->config;
$this->phpChromeOptions = [];
$browserFactory = new BrowserFactory($this->getChromePhpConfig()?->chromePath);
$browser = $browserFactory->createBrowser();
$page = $browser->createPage();
$page->navigate('data:text/html;charset=utf8,'.rawurlencode($this->html()))
->waitForNavigation();
$this->setPageFormat();
$this->setMargin();
$this->setHeaderAndFooter();
$this->phpChromeOptions['displayHeaderFooter'] = $this->options->displayHeaderFooter;
$this->phpChromeOptions['printBackground'] = $this->options->printBackground;
$this->phpChromeOptions['preferCSSPageSize'] = $this->options->preferCssPageSize;
$this->rendering = $page->pdf($this->phpChromeOptions)->getBase64();
$browser->close();
return $this;
}
public static function getOptionsSupport(): array
{
return [
'displayHeaderFooter' => true,
'headerTemplate' => true,
'footerTemplate' => true,
'printBackground' => true,
'preferCssPageSize' => true,
'landscape' => true,
'format' => true,
'width' => true,
'height' => true,
'margin' => true,
];
}
private function getChromePhpConfig(): ?ChromePhpConfig
{
return $this->config?->getDriverConfig(ChromePhpConfig::class);
}
// ============================================================
// Html
// ============================================================
private function html(): string
{
return <<<_HTML
<!DOCTYPE html>
<head>
<style>
$this->css
</style>
<title>Row bloom</title>
</head>
<body>
$this->html
</body>
</html>
_HTML;
}
// ============================================================
// Options
// ============================================================
private function setPageFormat(): void
{
$paperSize = $this->options->resolvePaperSize();
$this->phpChromeOptions['paperWidth'] = $paperSize->width->toInFloat();
$this->phpChromeOptions['paperHeight'] = $paperSize->height->toInFloat();
}
private function setHeaderAndFooter(): void
{
$this->phpChromeOptions['displayHeaderFooter'] = $this->options->displayHeaderFooter;
if (! $this->options->displayHeaderFooter) {
return;
}
$headerTemplate = $this->options->headerTemplate ?? '';
if ($this->config?->mergeCssToHeaderFooter) {
$headerTemplate .= "<style>{$this->css}</style>";
}
$this->phpChromeOptions['headerTemplate'] = $headerTemplate;
$this->phpChromeOptions['footerTemplate'] = $this->options->footerTemplate ?? '';
}
private function setMargin(): void
{
$this->phpChromeOptions['marginTop'] = $this->options->margin->top->toInFloat();
$this->phpChromeOptions['marginRight'] = $this->options->margin->right->toInFloat();
$this->phpChromeOptions['marginBottom'] = $this->options->margin->bottom->toInFloat();
$this->phpChromeOptions['marginLeft'] = $this->options->margin->left->toInFloat();
}
}