Skip to content

Commit

Permalink
PrintableHtmlDocument: Provide default header/footer style
Browse files Browse the repository at this point in the history
refs #2
  • Loading branch information
nilmerg committed Aug 31, 2021
1 parent a34de99 commit 84d93cb
Showing 1 changed file with 94 additions and 2 deletions.
96 changes: 94 additions & 2 deletions library/Pdfexport/PrintableHtmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,55 @@

use Icinga\Application\Icinga;
use Icinga\Web\StyleSheet;
use Icinga\Web\Url;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;
use ipl\Html\Text;
use ipl\Html\ValidHtml;

class PrintableHtmlDocument extends BaseHtmlElement
{
/** @var string */
const DEFAULT_HEADER_FOOTER_STYLE = <<<'CSS'
@font-face {
font-family: "Helvetica Neue", "Helvetica", "Arial", sans-serif;
}
header, footer {
width: 100%;
height: 21px;
font-size: 7px;
display: flex;
justify-content: space-between;
margin-left: 0.75cm;
margin-right: 0.75cm;
}
header > *:not(:last-child),
footer > *:not(:last-child) {
margin-right: 10px;
}
span {
line-height: 7px;
white-space: nowrap;
}
p {
margin: 0;
line-height: 7px;
word-break: break-word;
text-overflow: ellipsis;
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
CSS;

/** @var string Document title */
protected $title;

Expand Down Expand Up @@ -129,6 +170,10 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* For example, `<span class=title></span>` would generate span containing the title.
*
* Note that the header cannot exceed a height of 21px regardless of the margin's height or document's scale.
* With the default style, this height is separated by three lines, each accommodating 7px.
* Use `span`'s for single line text and `p`'s for multiline text.
*
* @var ValidHtml
*/
protected $headerTemplate;
Expand All @@ -145,6 +190,10 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* For example, `<span class=title></span>` would generate span containing the title.
*
* Note that the footer cannot exceed a height of 21px regardless of the margin's height or document's scale.
* With the default style, this height is separated by three lines, each accommodating 7px.
* Use `span`'s for single line text and `p`'s for multiline text.
*
* @var ValidHtml
*/
protected $footerTemplate;
Expand Down Expand Up @@ -195,6 +244,7 @@ public function setTitle($title)
* Set page header
*
* @param ValidHtml $header
*
* @return $this
*/
public function setHeader(ValidHtml $header)
Expand All @@ -208,6 +258,7 @@ public function setHeader(ValidHtml $header)
* Set page footer
*
* @param ValidHtml $footer
*
* @return $this
*/
public function setFooter(ValidHtml $footer)
Expand All @@ -231,6 +282,7 @@ public function getCoverPage()
* Set cover page
*
* @param ValidHtml $coverPage
*
* @return $this
*/
public function setCoverPage(ValidHtml $coverPage)
Expand Down Expand Up @@ -334,15 +386,23 @@ public function getPrintParameters()
}

if (isset($this->headerTemplate)) {
$parameters['headerTemplate'] = $this->headerTemplate->render();
$parameters['headerTemplate'] = $this->createHeader()->render();
$parameters['displayHeaderFooter'] = true;

if (! isset($parameters['marginTop'])) {
$parameters['marginTop'] = 0.6;
}
} else {
$parameters['headerTemplate'] = ' '; // An empty string is ignored
}

if (isset($this->footerTemplate)) {
$parameters['footerTemplate'] = $this->footerTemplate->render();
$parameters['footerTemplate'] = $this->createFooter()->render();
$parameters['displayHeaderFooter'] = true;

if (! isset($parameters['marginBottom'])) {
$parameters['marginBottom'] = 0.6;
}
} else {
$parameters['footerTemplate'] = ' '; // An empty string is ignored
}
Expand Down Expand Up @@ -440,4 +500,36 @@ protected function createLayoutScript(): ValidHtml
HtmlString::create($layoutJS)
);
}

/**
* Create document header
*
* @return ValidHtml
*/
protected function createHeader(): ValidHtml
{
return (new HtmlDocument())
->addHtml(
new HtmlElement('style', null, HtmlString::create(
static::DEFAULT_HEADER_FOOTER_STYLE
)),
new HtmlElement('header', null, $this->headerTemplate)
);
}

/**
* Create document footer
*
* @return ValidHtml
*/
protected function createFooter(): ValidHtml
{
return (new HtmlDocument())
->addHtml(
new HtmlElement('style', null, HtmlString::create(
static::DEFAULT_HEADER_FOOTER_STYLE
)),
new HtmlElement('footer', null, $this->footerTemplate)
);
}
}

0 comments on commit 84d93cb

Please sign in to comment.