Show only mainDetailGrid in pdf and move css fixes to less-directory

refs #4356
This commit is contained in:
Matthias Jentsch 2014-02-12 12:05:19 +01:00
parent a064ef520d
commit 4e5569371f
4 changed files with 156 additions and 69 deletions

View File

@ -5,6 +5,7 @@ namespace Icinga\File;
use \DOMPDF; use \DOMPDF;
use \DOMDocument; use \DOMDocument;
use \DOMXPath; use \DOMXPath;
use \DOMNode;
require_once 'vendor/dompdf/dompdf_config.inc.php'; require_once 'vendor/dompdf/dompdf_config.inc.php';
@ -20,7 +21,14 @@ class Pdf extends DOMPDF
public $rowsPerPage = 10; public $rowsPerPage = 10;
/** /**
* If occuring tables should be split up into smaller tables to avoid errors in the document layout. * If tables should only start at new pages.
*
* @var bool
*/
public $tableInitialPageBreak = false;
/**
* If occurring tables should be split up into smaller tables to avoid errors in the document layout.
* *
* @var bool * @var bool
*/ */
@ -45,7 +53,10 @@ class Pdf extends DOMPDF
. '</body>' . '</body>'
. '</html>'; . '</html>';
if ($this->paginateTable === true) { if ($this->paginateTable === true) {
$html = $this->paginateHtmlTables($html); $doc = new DOMDocument();
@$doc->loadHTML($html);
$this->paginateHtmlTables($doc);
$html = $doc->saveHtml();
} }
$this->load_html($html); $this->load_html($html);
$this->render(); $this->render();
@ -56,22 +67,23 @@ class Pdf extends DOMPDF
* *
* NOTE: This is a workaround to fix the buggy page-break on table-rows in dompdf. * NOTE: This is a workaround to fix the buggy page-break on table-rows in dompdf.
* *
* @param string A html-string. * @param DOMDocument $doc The html document containing the tables.
* *
* @return string The html string with the paginated rows. * @return array All paginated tables from the document.
*/ */
private function paginateHtmlTables($html) private function paginateHtmlTables(DOMDocument $doc)
{ {
$doc = new DOMDocument(); $xpath = new DOMXPath($doc);
@$doc->loadHTML($html); $tables = $xpath->query('.//table');
$xpath = new DOMXPath($doc); $paginated = array();
$j = 0;
$tables = $xpath->query('.//table');
foreach ($tables as $table) { foreach ($tables as $table) {
$containerType = null; $containerType = null;
$rows = $xpath->query('.//tr', $table); $rows = $xpath->query('.//tr', $table);
$rowCnt = $rows->length; $rowCnt = $rows->length;
$tableCnt = (Integer)ceil($rowCnt / $this->rowsPerPage); $tableCnt = (Integer)ceil($rowCnt / $this->rowsPerPage);
$paginated[$j] = array();
if ($rowCnt <= $this->rowsPerPage) { if ($rowCnt <= $this->rowsPerPage) {
continue; continue;
} }
@ -87,22 +99,21 @@ class Pdf extends DOMPDF
$containers = array(); $containers = array();
$pages = array(); $pages = array();
// insert page-break if ($this->tableInitialPageBreak) {
$div = $doc->createElement('div'); $this->pageBreak($doc, $table);
$div->setAttribute('style', 'page-break-before: always;'); }
$table->parentNode->insertBefore($div, $table);
for ($i = 0; $i < $tableCnt; $i++) { for ($i = 0; $i < $tableCnt; $i++) {
// clone table // clone table
$currentPage = $table->cloneNode(true); $currentPage = $table->cloneNode(true);
$pages[$i] = $currentPage; $pages[$i] = $currentPage;
$table->parentNode->insertBefore($currentPage, $table); $table->parentNode->insertBefore($currentPage, $table);
// put it in current paginated table
$paginated[$j] = $currentPage;
// insert page-break // insert page-break
if ($i < $tableCnt - 1) { if ($i < $tableCnt - 1) {
$div = $doc->createElement('div'); $this->pageBreak($doc, $table);
$div->setAttribute('style', 'page-break-before: always;');
$table->parentNode->insertBefore($div, $table);
} }
// fetch row container // fetch row container
@ -114,14 +125,21 @@ class Pdf extends DOMPDF
foreach ($rows as $row) { foreach ($rows as $row) {
$p = (Integer)floor($i / $this->rowsPerPage); $p = (Integer)floor($i / $this->rowsPerPage);
$containers[$p]->appendChild($row); $containers[$p]->appendChild($row);
//echo "Inserting row $i into container $p <br />";
$i++; $i++;
} }
// remove original table // remove original table
$table->parentNode->removeChild($table); $table->parentNode->removeChild($table);
$j++;
} }
return $doc->saveHTML(); return $paginated;
}
private function pageBreak($doc, $before)
{
$div = $doc->createElement('div');
$div->setAttribute('style', 'page-break-before: always;');
$before->parentNode->insertBefore($div, $before);
} }
/** /**
@ -136,40 +154,6 @@ class Pdf extends DOMPDF
{ {
$css = preg_replace('/\*:\s*before\s*,\s*/', '', $css); $css = preg_replace('/\*:\s*before\s*,\s*/', '', $css);
$css = preg_replace('/\*\s*:\s*after\s*\{[^\}]*\}/', '', $css); $css = preg_replace('/\*\s*:\s*after\s*\{[^\}]*\}/', '', $css);
return $css;
// TODO: Move into own .css file that is loaded when requesting a pdf
return $css . "\n"
. '*, html { font-size: 100%; } ' . "\n"
. 'form { display: none; }' . "\n"
// Insert page breaks
. 'div.pdf-page { page-break-before: always; } ' . "\n"
// Don't show any link outline
. 'a { outline: 0; }' . "\n"
// Fix badge positioning
. 'span.badge { float: right; max-width: 5px; }'
// prevent table rows from growing too big on page breaks
. 'tr { max-height: 30px; height: 30px; } ' . "\n"
// Hide buttons
. '*.button { display: none; }' . "\n"
. '*.btn-group { display: none; }' . "\n"
. 'button > i { display: none; }' . "\n"
// Hide navigation
. '*.nav {display: none; }' . "\n"
. '*.nav > li { display: none; }' . "\n"
. '*.nav > li > a { display: none; }' . "\n"
// Hide pagination
. '*.pagination { display: none; }' . "\n"
. '*.pagination > li { display: none; }' . "\n"
. '*.pagination > li > a { display: none; }' . "\n"
. '*.pagination > li > span { display: none; }' . "\n";
} }
} }

View File

@ -241,44 +241,49 @@ class ActionController extends Zend_Controller_Action
} }
} }
if ($this->_request->getParam('format') === 'pdf' && $this->_request->getControllerName() !== 'static') { if ($this->_request->getParam('format') === 'pdf' && $this->_request->getControllerName() !== 'static') {
$this->sendAsPdf(); $this->sendAsPdfAndDie();
die();
} }
} }
protected function sendAsPdf() protected function sendAsPdfAndDie()
{ {
$this->_helper->layout()->setLayout('inline');
$body = $this->view->render( $body = $this->view->render(
$this->_request->getControllerName() . '/' . $this->_request->getActionName() . '.phtml' $this->_request->getControllerName() . '/' . $this->_request->getActionName() . '.phtml'
); );
if (!headers_sent()) { if (!headers_sent()) {
$css = $this->view->getHelper('action')->action('stylesheet', 'static', 'application'); $css = $this->view->getHelper('action')->action('stylesheet', 'static', 'application');
$pdf = new PDF();
// fix css for pdf
require_once 'vendor/lessphp/lessc.inc.php';
$lessc = new \lessc();
$publicDir = realpath(dirname($_SERVER['SCRIPT_FILENAME']));
$css .= $lessc->compile($publicDir . '/css/icinga/pdf.less');
$pdf = new PDF();
if ($this->_request->getControllerName() === 'list') { if ($this->_request->getControllerName() === 'list') {
switch ($this->_request->getActionName()) { switch ($this->_request->getActionName()) {
case 'notifications': case 'notifications':
$pdf->rowsPerPage = 8; $pdf->rowsPerPage = 7;
break; break;
case 'comments': case 'comments':
$pdf->rowsPerPage = 8; $pdf->rowsPerPage = 7;
break; break;
default: default:
$pdf->rowsPerPage = 12; $pdf->rowsPerPage = 11;
break; break;
} }
} else { } else {
$pdf->paginateTable = false; $pdf->paginateTable = false;
} }
$pdf->renderPage($body, $css); $pdf->renderPage($body, $css);
$pdf->stream( $pdf->stream(
$this->_request->getControllerName() . '-' . $this->_request->getActionName() . '-' . time() . '.pdf' $this->_request->getControllerName() . '-' . $this->_request->getActionName() . '-' . time() . '.pdf'
); );
} else { } else {
Logger::error('Could not send pdf-response, content already written to output.'); Logger::error('Could not send pdf-response, content already written to output.');
die();
} }
die();
} }
/** /**

View File

@ -13,13 +13,11 @@ class Zend_View_Helper_SelectionToolbar extends Zend_View_Helper_Abstract
public function selectionToolbar($type, $target = null) public function selectionToolbar($type, $target = null)
{ {
if ($type == 'multi') { if ($type == 'multi') {
return 'Select return '<div class="selection-toolbar"> Select '
<a href="' . $target . '"> All </a> . '<a href="' . $target . '"> All </a>'
<a href="#"> None </a>'; . '<a href="#"> None </a> </div>';
} else if ($type == 'single') { } else if ($type == 'single') {
return 'Select <a href="#"> None </a>'; return '<div class="selection-toolbar"> Select <a href="#"> None </a> </div>';
} else { } else {
return ''; return '';
} }

100
public/css/icinga/pdf.less Normal file
View File

@ -0,0 +1,100 @@
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{ICINGA_LICENSE_HEADER}}}
*, html {
font-size: 100%;
}
form {
display: none;
}
// Insert page breaks
div.pdf-page {
page-break-before: always;
}
div.selection-toolbar {
display: none;
}
div.selection-toolbar > a {
display: none;
}
// Dont show any link outline
a {
outline: 0;
}
// Fix badge positioning
span.badge {
float: right;
max-width: 5px;
}
// prevent table rows from growing too big on page breaks
tr {
max-height: 30px;
height: 30px;
}
// Hide buttons
*.button {
display: none;
}
*.btn-group, *.btn-group > a {
display: none;
}
button > i {
display: none;
}
// Hide navigation
*.nav {
display: none;
}
*.nav > li {
display: none;
}
*.nav > li > a {
display: none;
}
// Hide pagination
*.pagination {
display: none;
}
*.pagination > li {
display: none;
}
*.pagination > li > a {
display: none;
}
*.pagination > li > span {
display: none;
}