1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
|
<?php
/* Icinga PDF Export | (c) 2019 Icinga GmbH | GPLv2 */
namespace Icinga\Module\Pdfexport;
use Icinga\Application\Icinga;
use Icinga\Web\UrlParams;
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;
/**
* Paper orientation
*
* Defaults to false.
*
* @var bool
*/
protected $landscape;
/**
* Print background graphics
*
* Defaults to false.
*
* @var bool
*/
protected $printBackground;
/**
* Scale of the webpage rendering
*
* Defaults to 1.
*
* @var float
*/
protected $scale;
/**
* Paper width in inches
*
* Defaults to 8.5 inches.
*
* @var float
*/
protected $paperWidth;
/**
* Paper height in inches
*
* Defaults to 11 inches.
*
* @var float
*/
protected $paperHeight;
/**
* Top margin in inches
*
* Defaults to 1cm (~0.4 inches).
*
* @var float
*/
protected $marginTop;
/**
* Bottom margin in inches
*
* Defaults to 1cm (~0.4 inches).
*
* @var float
*/
protected $marginBottom;
/**
* Left margin in inches
*
* Defaults to 1cm (~0.4 inches).
*
* @var float
*/
protected $marginLeft;
/**
* Right margin in inches
*
* Defaults to 1cm (~0.4 inches).
*
* @var float
*/
protected $marginRight;
/**
* Paper ranges to print, e.g., '1-5, 8, 11-13'
*
* Defaults to the empty string, which means print all pages
*
* @var string
*/
protected $pageRanges;
/**
* Page height in pixels
*
* Minus the default vertical margins, this is 1035.
* If the vertical margins are zero, it's 1160.
* Whether there's a header or footer doesn't matter in any case.
*
* @var int
*/
protected $pagePixelHeight = 1035;
/**
* HTML template for the print header
*
* Should be valid HTML markup with following classes used to inject printing values into them:
* * date: formatted print date
* * title: document title
* * url: document location
* * pageNumber: current page number
* * totalPages: total pages in the document
*
* 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;
/**
* HTML template for the print footer
*
* Should be valid HTML markup with following classes used to inject printing values into them:
* * date: formatted print date
* * title: document title
* * url: document location
* * pageNumber: current page number
* * totalPages: total pages in the document
*
* 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;
/**
* HTML for the cover page
*
* @var ValidHtml
*/
protected $coverPage;
/**
* Whether or not to prefer page size as defined by css
*
* Defaults to false, in which case the content will be scaled to fit the paper size.
*
* @var bool
*/
protected $preferCSSPageSize;
protected $tag = 'body';
/**
* Get the document title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set the document title
*
* @param string $title
*
* @return $this
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Set page header
*
* @param ValidHtml $header
*
* @return $this
*/
public function setHeader(ValidHtml $header)
{
$this->headerTemplate = $header;
return $this;
}
/**
* Set page footer
*
* @param ValidHtml $footer
*
* @return $this
*/
public function setFooter(ValidHtml $footer)
{
$this->footerTemplate = $footer;
return $this;
}
/**
* Get the cover page
*
* @return ValidHtml|null
*/
public function getCoverPage()
{
return $this->coverPage;
}
/**
* Set cover page
*
* @param ValidHtml $coverPage
*
* @return $this
*/
public function setCoverPage(ValidHtml $coverPage)
{
$this->coverPage = $coverPage;
return $this;
}
/**
* Remove page margins
*
* @return $this
*/
public function removeMargins()
{
$this->marginBottom = 0;
$this->marginLeft = 0;
$this->marginRight = 0;
$this->marginTop = 0;
return $this;
}
/**
* Finalize document to be printed
*/
protected function assemble()
{
$this->setWrapper(new HtmlElement(
'html',
null,
new HtmlElement(
'head',
null,
new HtmlElement(
'title',
null,
Text::create($this->title)
),
$this->createStylesheet(),
$this->createLayoutScript()
)
));
$this->getAttributes()->registerAttributeCallback('data-content-height', function () {
return $this->pagePixelHeight;
});
$this->getAttributes()->registerAttributeCallback('style', function () {
return sprintf('width: %sin;', $this->paperWidth ?: 8.5);
});
}
/**
* Get the parameters for Page.printToPDF
*
* @return array
*/
public function getPrintParameters()
{
$parameters = [];
if (isset($this->landscape)) {
$parameters['landscape'] = $this->landscape;
}
if (isset($this->printBackground)) {
$parameters['printBackground'] = $this->printBackground;
}
if (isset($this->scale)) {
$parameters['scale'] = $this->scale;
}
if (isset($this->paperWidth)) {
$parameters['paperWidth'] = $this->paperWidth;
}
if (isset($this->paperHeight)) {
$parameters['paperHeight'] = $this->paperHeight;
}
if (isset($this->marginTop)) {
$parameters['marginTop'] = $this->marginTop;
}
if (isset($this->marginBottom)) {
$parameters['marginBottom'] = $this->marginBottom;
}
if (isset($this->marginLeft)) {
$parameters['marginLeft'] = $this->marginLeft;
}
if (isset($this->marginRight)) {
$parameters['marginRight'] = $this->marginRight;
}
if (isset($this->pageRanges)) {
$parameters['pageRanges'] = $this->pageRanges;
}
if (isset($this->headerTemplate)) {
$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->createFooter()->render();
$parameters['displayHeaderFooter'] = true;
if (! isset($parameters['marginBottom'])) {
$parameters['marginBottom'] = 0.6;
}
} else {
$parameters['footerTemplate'] = ' '; // An empty string is ignored
}
if (isset($this->preferCSSPageSize)) {
$parameters['preferCSSPageSize'] = $this->preferCSSPageSize;
}
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?') {
list($_, $query) = explode('?', $matches[1], 2);
$params = UrlParams::fromQueryString($query);
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 PrintStyleSheet())->render(true)
);
return new HtmlElement('style', null, HtmlString::create($css));
}
/**
* Create layout javascript
*
* @return ValidHtml
*/
protected function createLayoutScript(): ValidHtml
{
$module = Icinga::app()->getModuleManager()->getModule('pdfexport');
if (! method_exists($module, 'getJsDir')) {
$jsPath = join(DIRECTORY_SEPARATOR, [$module->getBaseDir(), 'public', 'js']);
} else {
$jsPath = $module->getJsDir();
}
$layoutJS = file_get_contents($jsPath . '/layout.js') . "\n\n\n";
$layoutJS .= file_get_contents($jsPath . '/layout-plugins/page-breaker.js') . "\n\n\n";
return new HtmlElement(
'script',
Attributes::create(['type' => 'application/javascript']),
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)
);
}
}
|