new PDF API

This commit is contained in:
Roland Gruber 2017-06-25 14:47:33 +02:00
parent e4eed3c55d
commit ba3782b685
5 changed files with 67 additions and 73 deletions

View File

@ -1,4 +1,6 @@
<?PHP <?PHP
use \LAM\PDF\PDFStructure;
/* /*
$Id$ $Id$
@ -41,9 +43,9 @@ $Id$
class lamPDF extends UFPDF { class lamPDF extends UFPDF {
/** /**
* format settings for page layout * PDF structure
*/ */
private $page_definitions; private $structure;
/** /**
* current active font name * current active font name
@ -61,10 +63,10 @@ class lamPDF extends UFPDF {
/** /**
* Creates a new lamPDF object. * Creates a new lamPDF object.
* *
* @param array $page_definitions page settings * @param PDFStructure structure
* @param String $fontName font name * @param String $fontName font name
*/ */
function __construct($page_definitions = array(),$fontName) { function __construct($structure, $fontName) {
$this->fontName = $fontName; $this->fontName = $fontName;
if (!defined('FPDF_FONTPATH')) { if (!defined('FPDF_FONTPATH')) {
define('FPDF_FONTPATH', dirname(__FILE__) . '/font/'); define('FPDF_FONTPATH', dirname(__FILE__) . '/font/');
@ -72,7 +74,7 @@ class lamPDF extends UFPDF {
// Call constructor of superclass // Call constructor of superclass
parent::__construct('P','mm','A4'); parent::__construct('P','mm','A4');
$this->page_definitions = $page_definitions; $this->structure = $structure;
// Open PDF file and write some basic information // Open PDF file and write some basic information
$this->Open(); $this->Open();
@ -81,7 +83,7 @@ class lamPDF extends UFPDF {
$this->AddFont($this->fontName, 'I', $this->fontList[$this->fontName][2]); $this->AddFont($this->fontName, 'I', $this->fontList[$this->fontName][2]);
$this->AddFont($this->fontName, 'BI', $this->fontList[$this->fontName][3]); $this->AddFont($this->fontName, 'BI', $this->fontList[$this->fontName][3]);
$this->setFont($this->fontName,"",12); $this->setFont($this->fontName,"",12);
$this->setTitle($this->page_definitions['headline']); $this->setTitle($structure->getTitle());
$this->setCreator("LDAP Account Manager"); $this->setCreator("LDAP Account Manager");
$this->setMargins('10.0','10.0','10.0'); $this->setMargins('10.0','10.0','10.0');
$this->setAutoPageBreak(true,'20.0'); $this->setAutoPageBreak(true,'20.0');
@ -91,21 +93,21 @@ class lamPDF extends UFPDF {
* Creates the PDF page header. * Creates the PDF page header.
*/ */
function header() { function header() {
if($this->page_definitions['filename'] != 'none') { if($this->structure->getLogo() != 'none') {
$imageFile = substr(__FILE__,0,strlen(__FILE__)- 14) . "config/pdf/" . $_SESSION['config']->getName() . "/logos/" . $this->page_definitions['filename']; $imageFile = substr(__FILE__,0,strlen(__FILE__)- 14) . "config/pdf/" . $_SESSION['config']->getName() . "/logos/" . $this->structure->getLogo();
$imgProperties = getimagesize($imageFile); $imgProperties = getimagesize($imageFile);
$this->Image($imageFile,10,15,($imgProperties[0] / $this->k) / 5, ($imgProperties[1] / $this->k) / 5,0,"JPG"); $this->Image($imageFile,10,15,($imgProperties[0] / $this->k) / 5, ($imgProperties[1] / $this->k) / 5,0,"JPG");
} }
$this->SetY(23); $this->SetY(23);
$this->SetFont($this->fontName,"B",18); $this->SetFont($this->fontName,"B",18);
$this->Cell(170,5,$this->page_definitions['headline'],0,1,"R",0); $this->Cell(170,5,$this->structure->getTitle(), 0, 1, "R", 0);
$this->Ln(3); $this->Ln(3);
$this->SetLineWidth(0.4); $this->SetLineWidth(0.4);
$this->Line(10,38,200,38); $this->Line(10,38,200,38);
$this->Line(10,40,200,40); $this->Line(10,40,200,40);
$this->SetY(50); $this->SetY(50);
//set folding marks //set folding marks
if (isset($this->page_definitions['foldingmarks']) && ($this->page_definitions['foldingmarks'] == 'standard')) { if ($this->structure->getFoldingMarks() == PDFStructure::FOLDING_STANDARD) {
$this->SetLineWidth(0.2); $this->SetLineWidth(0.2);
$foldingMarks = array(97, 202); $foldingMarks = array(97, 202);
foreach ($foldingMarks as $mark) { foreach ($foldingMarks as $mark) {

View File

@ -1,8 +1,6 @@
<?php <?php
namespace LAM\PDF; namespace LAM\PDF;
/* /*
$Id$
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2003 - 2004 Michael Duergner Copyright (C) 2003 - 2004 Michael Duergner
2003 - 2017 Roland Gruber 2003 - 2017 Roland Gruber
@ -71,20 +69,19 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString =
$account_type = $accounts[0]->get_type(); $account_type = $accounts[0]->get_type();
// Get PDF structure from xml file // Get PDF structure from xml file
$load = \LAM\PDF\loadPDFStructure($account_type->getId(), $pdf_structure); $reader = new PDFStructureReader();
$structure = $load['structure']; $structure = $reader->read($account_type->getId(), $pdf_structure);
// get list of PDF keys // get list of PDF keys
$pdfKeys = array(); $pdfKeys = array();
foreach($structure as $entry) { foreach($structure->getSections() as $section) {
if ($entry['tag'] == "SECTION" && $entry['type'] == "open") { if (!$section instanceof PDFEntrySection) {
$key = $entry['attributes']['NAME']; continue;
// only values with a starting "_" are keys
if (strpos($key, '_') === 0) {
$pdfKeys[] = substr($key, 1);
}
} }
if ($entry['tag'] == "ENTRY") { if ($section->isAttributeTitle()) {
$pdfKeys[] = $entry['attributes']['NAME']; $pdfKeys[] = $section->getPdfKey();
}
foreach ($section->getEntries() as $entry) {
$pdfKeys[] = $entry->getKey();
} }
} }
$pdfKeys = array_unique($pdfKeys); $pdfKeys = array_unique($pdfKeys);
@ -95,7 +92,7 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString =
$fontName = "Dejavu"; $fontName = "Dejavu";
// Create a new PDF file acording to the account type // Create a new PDF file acording to the account type
$pdf = new \lamPDF($load['page_definitions'],$fontName); $pdf = new \lamPDF($structure, $fontName);
// Loop over each account and add a new page in the PDF file for it // Loop over each account and add a new page in the PDF file for it
foreach($accounts as $account) { foreach($accounts as $account) {
@ -107,28 +104,10 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString =
$entries = $account->get_pdfEntries($pdfKeys, $account_type->getId()); $entries = $account->get_pdfEntries($pdfKeys, $account_type->getId());
// Now create the PDF file acording to the structure with the submitted values // Now create the PDF file acording to the structure with the submitted values
foreach($structure as $entry) { foreach ($structure->getSections() as $section) {
// We have a new section to start if ($section instanceof PDFTextSection) {
if($entry['tag'] == "SECTION" && $entry['type'] == "open") {
$name = $entry['attributes']['NAME'];
if(preg_match("/^_[a-zA-Z_]+/",$name)) {
$section_headline = getSectionHeadline($entries[substr($name,1)][0]);
}
else {
$section_headline = $name;
}
$pdf->setFont($fontName, "B", LAMPDF_FONT_SIZE_BIG);
$pdf->Write(0,$section_headline . ":");
$pdf->Ln(LAMPDF_LINEHEIGHT);
}
// We have a section to end
elseif($entry['tag'] == "SECTION" && $entry['type'] == "close") {
$pdf->Ln(LAMPDF_LINEHEIGHT * 2);
}
// We have to include a static text.
elseif($entry['tag'] == "TEXT") {
$pdf->setFont($fontName, "", LAMPDF_FONT_SIZE); $pdf->setFont($fontName, "", LAMPDF_FONT_SIZE);
$info_string = str_replace("\r", "", $entry['value']); $info_string = str_replace("\r", "", $section->getText());
$info_string = explode("\n", $info_string); $info_string = explode("\n", $info_string);
foreach ($info_string as $text) { foreach ($info_string as $text) {
$pdf->MultiCell(0, LAMPDF_LINEHEIGHT, trim($text), 0, "L", 0); $pdf->MultiCell(0, LAMPDF_LINEHEIGHT, trim($text), 0, "L", 0);
@ -136,32 +115,42 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString =
} }
$pdf->Ln(LAMPDF_LINEHEIGHT * 2); $pdf->Ln(LAMPDF_LINEHEIGHT * 2);
} }
// We have to include an entry from the account elseif ($section instanceof PDFEntrySection) {
elseif($entry['tag'] == "ENTRY") { // section headline
// Get name of current entry if($section->isAttributeTitle()) {
$name = $entry['attributes']['NAME']; $section_headline = getSectionHeadline($entries[$section->getPdfKey()][0]);
// skip non-existent entries }
if (isset($entries[$name])) { else {
// Get current entry $section_headline = $section->getTitle();
$valueEntries = $entries[$name]; }
$pdf->setFont($fontName, "B", LAMPDF_FONT_SIZE_BIG);
// Print entry only when module sumitted values for it $pdf->Write(0,$section_headline . ":");
if(is_array($valueEntries)) { $pdf->Ln(LAMPDF_LINEHEIGHT);
// Loop over all rows of this entry (most of the time this will be just one) // entries
foreach($valueEntries as $valueEntry) { foreach ($section->getEntries() as $entry) {
if ($valueEntry instanceof PDFLabelValue) { // skip non-existent entries
printLabelValue($pdf, $valueEntry, $fontName); if (isset($entries[$entry->getKey()])) {
} // Get current entry
else if ($valueEntry instanceof PDFTable) { $valueEntries = $entries[$entry->getKey()];
printTable($pdf, $valueEntry, $fontName); // Print entry only when module sumitted values for it
if(is_array($valueEntries)) {
// Loop over all rows of this entry (most of the time this will be just one)
foreach($valueEntries as $valueEntry) {
if ($valueEntry instanceof PDFLabelValue) {
printLabelValue($pdf, $valueEntry, $fontName);
}
else if ($valueEntry instanceof PDFTable) {
printTable($pdf, $valueEntry, $fontName);
}
} }
} }
} }
} }
// section end
$pdf->Ln(LAMPDF_LINEHEIGHT * 2);
} }
} }
} }
// Close PDF // Close PDF
$pdf->Close(); $pdf->Close();
if (!$returnAsString) { if (!$returnAsString) {

View File

@ -401,10 +401,7 @@ class PDFStructureReader {
} }
$structure->setLogo($xml->getAttribute('filename')); $structure->setLogo($xml->getAttribute('filename'));
$structure->setTitle($xml->getAttribute('headline')); $structure->setTitle($xml->getAttribute('headline'));
$foldingMarks = $xml->getAttribute('foldingmarks'); $structure->setFoldingMarks($xml->getAttribute('foldingmarks'));
if ($foldingMarks === 'yes') {
$structure->setFoldingMarks(true);
}
$sections = array(); $sections = array();
while ($xml->read()) { while ($xml->read()) {
if ($xml->nodeType === \XMLReader::SIGNIFICANT_WHITESPACE) { if ($xml->nodeType === \XMLReader::SIGNIFICANT_WHITESPACE) {
@ -474,11 +471,16 @@ class PDFStructureReader {
*/ */
class PDFStructure { class PDFStructure {
/** no folding marks */
const FOLDING_NONE = 'no';
/** standard folding marks */
const FOLDING_STANDARD = 'standard';
private $logo = null; private $logo = null;
private $title = null; private $title = null;
private $foldingMarks = false; private $foldingMarks = 'no';
private $sections = array(); private $sections = array();
@ -521,7 +523,7 @@ class PDFStructure {
/** /**
* Returns if to print folding marks. * Returns if to print folding marks.
* *
* @return boolean print folding marks * @return string print folding marks
*/ */
public function getFoldingMarks() { public function getFoldingMarks() {
return $this->foldingMarks; return $this->foldingMarks;
@ -530,7 +532,7 @@ class PDFStructure {
/** /**
* Sets if to print folding marks. * Sets if to print folding marks.
* *
* @param boolean $foldingMarks print folding marks * @param string $foldingMarks print folding marks
*/ */
public function setFoldingMarks($foldingMarks) { public function setFoldingMarks($foldingMarks) {
$this->foldingMarks = $foldingMarks; $this->foldingMarks = $foldingMarks;

View File

@ -2,6 +2,7 @@
use LAM\PDF\PDFTextSection; use LAM\PDF\PDFTextSection;
use LAM\PDF\PDFEntrySection; use LAM\PDF\PDFEntrySection;
use LAM\PDF\PDFStructureReader; use LAM\PDF\PDFStructureReader;
use LAM\PDF\PDFStructure;
/* /*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
@ -45,7 +46,7 @@ class ReadStructureTest extends PHPUnit_Framework_TestCase {
$structure = $reader->read('type', 'name'); $structure = $reader->read('type', 'name');
$this->assertEquals('printLogo.jpg', $structure->getLogo()); $this->assertEquals('printLogo.jpg', $structure->getLogo());
$this->assertEquals('User information', $structure->getTitle()); $this->assertEquals('User information', $structure->getTitle());
$this->assertEquals(true, $structure->getFoldingMarks()); $this->assertEquals(PDFStructure::FOLDING_STANDARD, $structure->getFoldingMarks());
$sections = $structure->getSections(); $sections = $structure->getSections();
$this->assertEquals(3, sizeof($sections)); $this->assertEquals(3, sizeof($sections));
// check first section // check first section

View File

@ -1,4 +1,4 @@
<pdf filename="printLogo.jpg" headline="User information" type="user" foldingmarks="yes"> <pdf filename="printLogo.jpg" headline="User information" foldingmarks="standard">
<section name="Personal user information"> <section name="Personal user information">
<entry name="inetOrgPerson_givenName" /> <entry name="inetOrgPerson_givenName" />
<entry name="inetOrgPerson_sn"> <entry name="inetOrgPerson_sn">