Mess with PDF generation code

* Works more or less
* Rewrite image tags on the fly
* Sample header image (still ugly)
* Trying hard to find a way for CORRECT page footers and line numbers
This commit is contained in:
Thomas Gelf 2014-03-06 11:21:11 +00:00
parent 9971030965
commit 6ed72e1565
5 changed files with 147 additions and 54 deletions

View File

@ -0,0 +1,43 @@
<?php
use Icinga\Web\StyleSheet;
?><!DOCTYPE html>
<html>
<head>
<style>
<?= StyleSheet::compileForPdf() ?>
</style>
</head>
<body>
<script type="text/php">
// 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/logo_icinga_big_dark.png', array('align' => 'right', 'width' => '150')) ?>
<!--<div id="page-header">
<table>
<tr>
<td><?= $this->escape($this->title) ?></td>
<td style="text-align: right;"></td>
</tr>
</table>
</div>-->
<h1><?= $this->escape($this->title) ?></h1>
<div id="col1" class="container">
<?= $this->render('inline.phtml') ?>
</div>
<div id="page-footer">
<div class="page-number"></div>
</div>
</body>
</html>

View File

@ -5,6 +5,11 @@ namespace Icinga\File;
use DOMPDF;
use DOMDocument;
use DOMXPath;
use Font_Metrics;
use Icinga\Application\Icinga;
use Icinga\Web\StyleSheet;
use Icinga\Web\Url;
use Icinga\Exception\ProgrammingError;
require_once 'vendor/dompdf/dompdf_config.inc.php';
@ -36,20 +41,71 @@ class Pdf extends DOMPDF
public function __construct()
{
$this->set_paper(DOMPDF_DEFAULT_PAPER_SIZE, "portrait");
$this->set_paper('A4', 'portrait');
parent::__construct();
}
protected function assertNoHeadersSent()
{
if (headers_sent()) {
throw new ProgrammingError(
'Could not send pdf-response, content already written to output.'
);
}
}
public function renderControllerAction($controller)
{
$this->assertNoHeadersSent();
ini_set('memory_limit', '384M');
ini_set('max_execution_time', 300);
$request = $controller->getRequest();
$layout = $controller->getHelper('layout')->setLayout('pdf');
$controller->render();
$layout->content = $controller->getResponse();
$html = $layout->render();
$imgDir = Url::fromPath('img');
$html = preg_replace('~src="' . $imgDir . '/~', 'src="' . Icinga::app()->getBootstrapDirecory() . $imgDir . '/', $html);
//echo $html; exit;
$this->load_html($html);
/*
// TODO: We need to find a solution for page footers
$font = Font_Metrics::get_font("helvetica", "bold");
$canvas = $this->get_canvas();
$canvas->page_text(555, 750, "{PAGE_NUM}/{PAGE_COUNT}", $font, 10, array(0,0,0));
$dompdf->page_script('
// $pdf is the variable containing a reference to the canvas object provided by dompdf
$pdf->line(10,730,800,730,array(0,0,0),1);
');
*/
$this->render();
$this->stream(
sprintf(
'%s-%s-%d.pdf',
$request->getControllerName(),
$request->getActionName(),
time()
)
);
}
/**
* @param $body
* @param $css
*/
public function renderPage($body, $css)
/* public function renderPage($body, $css)
{
$html =
'<html><head></head>'
. '<body>'
. '<style>' . Pdf::prepareCss($css) . '</style>'
. '<style>' . $css
// . Pdf::prepareCss($css)
. '</style>'
. $body
. '</body>'
. '</html>';
@ -61,7 +117,7 @@ class Pdf extends DOMPDF
}
$this->load_html($html);
$this->render();
}
}*/
/**
* Split up tables into multiple elements that each contain $rowsPerPage of all original rows
@ -72,7 +128,7 @@ class Pdf extends DOMPDF
*
* @return array All paginated tables from the document.
*/
private function paginateHtmlTables(DOMDocument $doc)
/* private function paginateHtmlTables(DOMDocument $doc)
{
$xpath = new DOMXPath($doc);
$tables = $xpath->query('.//table');
@ -142,7 +198,7 @@ class Pdf extends DOMPDF
$div->setAttribute('style', 'page-break-before: always;');
$before->parentNode->insertBefore($div, $before);
}
*/
/**
* Prepare the given css for rendering with DOMPDF, by removing or hiding all incompatible
* styles
@ -151,10 +207,12 @@ class Pdf extends DOMPDF
*
* @return string A css-string that is ready to use for DOMPDF
*/
public static function prepareCss($css)
{
$css = preg_replace('/\*:\s*before\s*,\s*/', '', $css);
$css = preg_replace('/\*\s*:\s*after\s*\{[^\}]*\}/', '', $css);
return $css;
}
// public static function prepareCss($css)
// {
// $css = preg_replace('/\*:\s*before\s*,\s*/', '', $css);
// $css = preg_replace('/\*\s*:\s*after\s*\{[^\}]*\}/', '', $css);
// return $css;
// }
}

View File

@ -42,9 +42,7 @@ use Icinga\Web\Widget\Tabs;
use Icinga\Web\Url;
use Icinga\Logger\Logger;
use Icinga\Web\Request;
use Icinga\File\Pdf;
use \DOMDocument;
use Icinga\Exception\ProgrammingError;
/**
@ -332,40 +330,8 @@ class ActionController extends Zend_Controller_Action
protected function sendAsPdf()
{
$this->_helper->layout()->setLayout('inline');
$body = $this->view->render(
$this->_request->getControllerName() . '/' . $this->_request->getActionName() . '.phtml'
);
if (!headers_sent()) {
$css = $this->view->getHelper('action')->action('stylesheet', 'static', 'application');
// load css fixes for pdf formatting mode
$publicDir = realpath(dirname($_SERVER['SCRIPT_FILENAME']));
$css .= file_get_contents($publicDir . '/css/pdf/pdf.css');
$pdf = new PDF();
if ($this->_request->getControllerName() === 'list') {
switch ($this->_request->getActionName()) {
case 'notifications':
$pdf->rowsPerPage = 7;
break;
case 'comments':
$pdf->rowsPerPage = 7;
break;
default:
$pdf->rowsPerPage = 11;
break;
}
} else {
$pdf->paginateTable = false;
}
$pdf->renderPage($body, $css);
$pdf->stream(
$this->_request->getControllerName() . '-' . $this->_request->getActionName() . '-' . time() . '.pdf'
);
} else {
Logger::error('Could not send pdf-response, content already written to output.');
}
$pdf = new Pdf();
$pdf->renderControllerAction($this);
}
/**

View File

@ -6,10 +6,16 @@
margin: 2cm;
}
body > h1 {
font-size: 1em;
}
#page-header, #page-footer {
font-family: fixed;
font-weight: bold;
position: fixed;
left: 0;
right: 0;
left: 1em;
right: 1em;
color: #aaa;
font-size: 0.9em;
}
@ -20,19 +26,31 @@
border: none;
}
#page-header table {
width: 80%;
}
#page-header td, #page-footer td {
font-family: fixed;
padding: 0;
color: #aaa;
width: 50%;
}
#page-header {
top: 0;
border-bottom: 0.1pt solid #aaa;
top: 0.5em;
border-bottom: 0.2pt solid #aaa;
}
#page-footer {
bottom: 0;
border-top: 0.1pt solid #aaa;
bottom: 2.5em;
border-top: 0.2pt solid #aaa;
}
@page {
@bottom-right {
content: counter(page) " of " counter(pages);
}
}
.page-number {
@ -48,3 +66,11 @@ hr {
border: 0;
}
.content {
margin: 0;
}
body {
margin: 1cm 1cm 1.5cm 1cm;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB