Skip to content

Commit

Permalink
headerTemplate footerTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
medilies committed Nov 18, 2023
1 parent 9b6f90a commit 5a8264f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
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,
) {
}
}
49 changes: 30 additions & 19 deletions src/BrowsershotRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function render(Html $html, Css $css, Options $options, Config $config =
$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 @@ public function render(Html $html, Css $css, Options $options, Config $config =
->showBackground($this->options->printBackground)
->scale(1);

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

$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 Down Expand Up @@ -112,13 +105,31 @@ public static function getOptionsSupport(): array
];
}

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

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

0 comments on commit 5a8264f

Please sign in to comment.