Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3 config option to merge global css into header footer #4

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/BrowsershotConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public function __construct(
public ?string $npmBinaryPath = null,
public ?string $nodeModulesPath = null,
public ?string $chromePath = null,
public bool $mergeGlobalCss = true,
) {
}
}
53 changes: 32 additions & 21 deletions src/BrowsershotRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
$this->options = $options;
$this->config = $config ?? $this->config;

$browsershotConfig = $this->config?->getDriverConfig(BrowsershotConfig::class);

[$paperWidth, $paperHeight] = $this->options->resolvePaperSize(LengthUnit::MILLIMETER_UNIT);

$margin = Margin::fromOptions($this->options)->allRawIn(LengthUnit::MILLIMETER_UNIT);
Expand All @@ -59,31 +61,22 @@
->showBackground($this->options->printBackground)
->scale(1);

if ($this->options->displayHeaderFooter) {
$browsershot->showBrowserHeaderAndFooter()
->headerHtml($this->options->rawHeader ?? '')
->footerHtml($this->options->rawFooter ?? '');
}

$chromePath = $this->config?->getDriverConfig(BrowsershotConfig::class)?->chromePath;
$nodeBinaryPath = $this->config?->getDriverConfig(BrowsershotConfig::class)?->nodeBinaryPath;
$npmBinaryPath = $this->config?->getDriverConfig(BrowsershotConfig::class)?->npmBinaryPath;
$nodeModulesPath = $this->config?->getDriverConfig(BrowsershotConfig::class)?->nodeModulesPath;
$this->headerAndFooter($browsershot);

if (! is_null($chromePath)) {
$browsershot->setChromePath($chromePath);
if (! is_null($browsershotConfig?->chromePath)) {

Check failure on line 66 in src/BrowsershotRenderer.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property object::$chromePath.
$browsershot->setChromePath($browsershotConfig?->chromePath);

Check failure on line 67 in src/BrowsershotRenderer.php

View workflow job for this annotation

GitHub Actions / phpstan

Using nullsafe property access on non-nullable type object. Use -> instead.
}

if (! is_null($nodeBinaryPath)) {
$browsershot->setNodeBinary($nodeBinaryPath);
if (! is_null($browsershotConfig?->nodeBinaryPath)) {

Check failure on line 70 in src/BrowsershotRenderer.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property object::$nodeBinaryPath.
$browsershot->setNodeBinary($browsershotConfig?->nodeBinaryPath);

Check failure on line 71 in src/BrowsershotRenderer.php

View workflow job for this annotation

GitHub Actions / phpstan

Using nullsafe property access on non-nullable type object. Use -> instead.
}

if (! is_null($npmBinaryPath)) {
$browsershot->setNpmBinary($npmBinaryPath);
if (! is_null($browsershotConfig?->npmBinaryPath)) {

Check failure on line 74 in src/BrowsershotRenderer.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property object::$npmBinaryPath.
$browsershot->setNpmBinary($browsershotConfig?->npmBinaryPath);

Check failure on line 75 in src/BrowsershotRenderer.php

View workflow job for this annotation

GitHub Actions / phpstan

Using nullsafe property access on non-nullable type object. Use -> instead.
}

if (! is_null($nodeModulesPath)) {
$browsershot->setNodeModulePath($nodeModulesPath);
if (! is_null($browsershotConfig?->nodeModulesPath)) {

Check failure on line 78 in src/BrowsershotRenderer.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property object::$nodeModulesPath.
$browsershot->setNodeModulePath($browsershotConfig?->nodeModulesPath);

Check failure on line 79 in src/BrowsershotRenderer.php

View workflow job for this annotation

GitHub Actions / phpstan

Using nullsafe property access on non-nullable type object. Use -> instead.
}

$this->rendering = $browsershot->base64pdf();
Expand All @@ -95,8 +88,8 @@
{
return [
'displayHeaderFooter' => true,
'rawHeader' => true,
'rawFooter' => true,
'headerTemplate' => true,
'footerTemplate' => true,
'printBackground' => true,
'preferCssPageSize' => true,
'landscape' => true,
Expand All @@ -112,13 +105,31 @@
];
}

// ------------------------------------------------------------

private function headerAndFooter(Browsershot $browsershot): void
{
if (! $this->options->displayHeaderFooter) {
return;
}

$header = $this->options->headerTemplate ?? '';

if ($this->config?->getDriverConfig(BrowsershotConfig::class)?->mergeGlobalCss) {

Check failure on line 118 in src/BrowsershotRenderer.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property object::$mergeGlobalCss.
$header .= $this->css;
}

$browsershot->showBrowserHeaderAndFooter()
->headerHtml($header)
->footerHtml($this->options->footerTemplate ?? '');
}

// ============================================================
// Html
// ============================================================

private function html(): string
{
// ? same happens in HtmlRenderer
return <<<_HTML
<!DOCTYPE html>
<head>
Expand Down
Loading