Skip to content

Commit

Permalink
PrintableHtmlDocument: Use a self-contained stylesheet
Browse files Browse the repository at this point in the history
Fonts and resolvable images now get integrated as base64
encoded data urls. This reduces the need for network access
for them.

refs #2
  • Loading branch information
nilmerg committed Aug 31, 2021
1 parent 493ed7d commit a34de99
Showing 1 changed file with 70 additions and 5 deletions.
75 changes: 70 additions & 5 deletions library/Pdfexport/PrintableHtmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,7 @@ protected function assemble()
null,
Text::create($this->title)
),
new HtmlElement(
'style',
null,
HtmlString::create(new StyleSheet())
),
$this->createStylesheet(),
$this->createLayoutScript()
)
));
Expand Down Expand Up @@ -358,6 +354,75 @@ public function getPrintParameters()
return $parameters;
}

/**
* Create CSS stylesheet
*
* @return ValidHtml
*/
protected function createStylesheet(): ValidHtml
{
$app = Icinga::app();

$css = preg_replace_callback(
'~(?<=url\()[\'"]?([^(\'"]*)[\'"]?(?=\))~',
function ($matches) use ($app) {
if (substr($matches[1], 0, 3) !== '../') {
return $matches[1];
}

$path = substr($matches[1], 3);
if (substr($path, 0, 4) === 'lib/') {
$assetPath = substr($path, 4);

$library = null;
foreach ($app->getLibraries() as $candidate) {
if (substr($assetPath, 0, strlen($candidate->getName())) === $candidate->getName()) {
$library = $candidate;
$assetPath = ltrim(substr($assetPath, strlen($candidate->getName())), '/');
break;
}
}

if ($library === null) {
return $matches[1];
}

$path = $library->getStaticAssetPath() . DIRECTORY_SEPARATOR . $assetPath;
} elseif (substr($matches[1], 0, 14) === '../static/img?') {
$params = Url::fromPath($matches[1])->getParams();
if (! $app->getModuleManager()->hasEnabled($params->get('module_name'))) {
return $matches[1];
}

$module = $app->getModuleManager()->getModule($params->get('module_name'));
$imgRoot = $module->getBaseDir() . '/public/img/';
$path = realpath($imgRoot . $params->get('file'));
} else {
$path = $app->getBootstrapDirectory() . '/' . $path;
}

if (! $path || ! file_exists($path) || ! is_file($path)) {
return $matches[1];
}

$mimeType = @mime_content_type($path);
if ($mimeType === false) {
return $matches[1];
}

$fileContent = @file_get_contents($path);
if ($fileContent === false) {
return $matches[1];
}

return "'data:$mimeType; base64, " . base64_encode($fileContent) . "'";
},
(new StyleSheet())->render(true)
);

return new HtmlElement('style', null, HtmlString::create($css));
}

/**
* Create layout javascript
*
Expand Down

0 comments on commit a34de99

Please sign in to comment.