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

PrintableHtmlDocument: Provide default header/footer style #38

Merged
merged 1 commit into from
Aug 31, 2021
Merged
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
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)
);
}
}