Enhance page layout when exporting to PDF using dompdf

It's still not perfect. But better than previously.
Using the pdfexport module is still recommended though.
This commit is contained in:
Johannes Meyer 2022-03-01 16:11:28 +01:00
parent 6d744e585f
commit 85c42f8400
5 changed files with 75 additions and 108 deletions

View File

@ -1,5 +1,6 @@
<?php <?php
use Icinga\Application\Icinga;
use Icinga\Web\StyleSheet; use Icinga\Web\StyleSheet;
@ -10,6 +11,9 @@ if ($moduleName !== 'default') {
$moduleClass = ''; $moduleClass = '';
} }
$logoPath = Icinga::app()->getBootstrapDirectory() . '/img/icinga-logo-big-dark.png';
$logo = base64_encode(file_get_contents($logoPath));
?><!DOCTYPE html> ?><!DOCTYPE html>
<html> <html>
@ -20,33 +24,21 @@ if ($moduleName !== 'default') {
<base href="<?= $this->serverUrl() ?>"> <base href="<?= $this->serverUrl() ?>">
</head> </head>
<body> <body>
<script type="text/php"> <div id="header">
// This attempt doesn't work :(
if ( isset($pdf) )
{
$w = $pdf->get_width();
$h = $pdf->get_height();
$font = Font_Metrics::get_font("helvetica");
$pdf->page_text($w -100, $h - 40, "Page {PAGE_NUM} of {PAGE_COUNT}", $font, 10, array(0,0,0));
}
</script>
<?= $this->img('img/icinga-logo-big-dark.png', null, array('align' => 'right', 'width' => '75')) ?>
<!--<div id="page-header">
<table> <table>
<tr> <tbody>
<td><?= $this->escape($this->title) ?></td> <tr>
<td style="text-align: right;"></td> <th><?= $this->escape($this->title) ?></th>
</tr> <td style="text-align: right;"><img width="75" src="data:image/png;base64,<?= $logo ?>"></td>
</tr>
</tbody>
</table> </table>
</div>-->
<h1><?= $this->escape($this->title) ?></h1>
<div id="col1" class="container<?= $moduleClass ?>">
<?= $this->render('inline.phtml') ?>
</div> </div>
<div id="footer">
<div id="page-footer">
<div class="page-number"></div> <div class="page-number"></div>
</div> </div>
<div class="<?= $moduleClass ?>">
<?= $this->render('inline.phtml') ?>
</div>
</body> </body>
</html> </html>

View File

@ -126,13 +126,13 @@ class LessCompiler
/** /**
* Set the path to the LESS theme * Set the path to the LESS theme
* *
* @param string $theme Path to the LESS theme * @param ?string $theme Path to the LESS theme
* *
* @return $this * @return $this
*/ */
public function setTheme($theme) public function setTheme($theme)
{ {
if (is_file($theme) && is_readable($theme)) { if ($theme === null || (is_file($theme) && is_readable($theme))) {
$this->theme = $theme; $this->theme = $theme;
} else { } else {
Logger::error('Can\t load theme %s. Make sure that the theme exists and is readable', $theme); Logger::error('Can\t load theme %s. Make sure that the theme exists and is readable', $theme);

View File

@ -187,6 +187,8 @@ class StyleSheet
public static function forPdf() public static function forPdf()
{ {
$styleSheet = new self(); $styleSheet = new self();
$styleSheet->lessCompiler->setTheme(null);
$styleSheet->lessCompiler->setThemeMode($styleSheet->pubPath . '/css/modes/none.less');
$styleSheet->lessCompiler->addLessFile($styleSheet->pubPath . '/css/pdf/pdfprint.less'); $styleSheet->lessCompiler->addLessFile($styleSheet->pubPath . '/css/pdf/pdfprint.less');
// TODO(el): Caching // TODO(el): Caching
return $styleSheet; return $styleSheet;

View File

@ -1,31 +1,9 @@
/*! Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */ /*! Icinga Web 2 | (c) 2015 Icinga GmbH | GPLv2+ */
// Print styles
@media print { @media print {
#fileupload-frame-target, .controls,
#header,
#responsive-debug,
#sidebar,
.controls > .tabs,
.controls .filter,
.controls .limiter-control,
.controls .pagination-control,
.controls .selection-info,
.controls .sort-control,
.dontprint, // Compat only, use dont-print instead .dontprint, // Compat only, use dont-print instead
.dont-print { .dont-print {
display: none; display: none !important;
}
#layout,
#main,
.controls {
position: static;
}
.container {
float: none !important;
width: 100% !important;
} }
} }

View File

@ -1,71 +1,75 @@
/*! Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */ /*! Icinga Web 2 | (c) 2014 Icinga GmbH | GPLv2+ */
.controls, // Ensure styling is light, exports use a white background
.dontprint, // Compat only, use dont-print instead
.dont-print {
display: none !important;
}
table.action img.inlinepie { @gray: #7F7F7F;
width: 50%; @gray-semilight: #A9A9A9;
height: 50%; @gray-light: #C9C9C9;
} @gray-lighter: #EEEEEE;
@gray-lightest: #F7F7F7;
@icinga-blue: #0095BF;
@low-sat-blue: #dae3e6;
@low-sat-blue-dark: #becbcf;
@body-bg-color: #fff;
@text-color: @black;
@text-color-light: @gray;
@tr-active-color: @body-bg-color;
@tr-hover-color: @body-bg-color;
// Page layout
@page { @page {
margin: 2cm; margin: 1cm;
} }
.container * { body {
font-size: 7pt; font-family: sans-serif;
margin: 0;
padding-top: 37px; // ~ logo height in the header
} }
body > h1 { .content {
font-size: 1em; font-size: 9pt;
} }
#page-header, #page-footer { #header,
font-family: fixed; #footer {
font-weight: bold;
position: fixed; position: fixed;
left: 1em; left: 0;
right: 1em; right: 0;
color: #aaa; color: #aaa;
font-size: 0.9em; font-size: 0.9em;
} }
#page-header table, #page-footer table { #header {
top: 0;
border-bottom: 0.1pt solid #aaa;
img {
margin-bottom: 3px;
}
}
#footer {
bottom: 0;
border-top: 0.1pt solid #aaa;
}
#header table,
#footer table {
width: 100%; width: 100%;
border-collapse: collapse; border-collapse: collapse;
border: none; border: none;
} }
#page-header table { #header td,
width: 80%; #header th,
} #footer td,
#footer th {
#page-header td, #page-footer td {
font-family: fixed;
padding: 0; padding: 0;
color: #aaa;
width: 50%; width: 50%;
} }
#page-header {
top: 0.5em;
border-bottom: 0.2pt solid #aaa;
}
#page-footer {
bottom: 2.5em;
border-top: 0.2pt solid #aaa;
}
@page {
@bottom-right {
content: counter(page) " of " counter(pages);
}
}
.page-number { .page-number {
text-align: center; text-align: center;
} }
@ -79,19 +83,10 @@ hr {
border: 0; border: 0;
} }
.content { // General style
margin: 0;
}
.dashboard > div.container { .controls,
width: 100%; .dontprint, // Compat only, use dont-print instead
display: block; .dont-print {
} display: none !important;
h1 form {
display: none;
}
body {
margin: 1cm 1cm 1.5cm 1cm;
} }