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

View File

@ -1,8 +1,6 @@
<?php
namespace LAM\PDF;
/*
$Id$
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2003 - 2004 Michael Duergner
2003 - 2017 Roland Gruber
@ -71,20 +69,19 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString =
$account_type = $accounts[0]->get_type();
// Get PDF structure from xml file
$load = \LAM\PDF\loadPDFStructure($account_type->getId(), $pdf_structure);
$structure = $load['structure'];
$reader = new PDFStructureReader();
$structure = $reader->read($account_type->getId(), $pdf_structure);
// get list of PDF keys
$pdfKeys = array();
foreach($structure as $entry) {
if ($entry['tag'] == "SECTION" && $entry['type'] == "open") {
$key = $entry['attributes']['NAME'];
// only values with a starting "_" are keys
if (strpos($key, '_') === 0) {
$pdfKeys[] = substr($key, 1);
foreach($structure->getSections() as $section) {
if (!$section instanceof PDFEntrySection) {
continue;
}
if ($section->isAttributeTitle()) {
$pdfKeys[] = $section->getPdfKey();
}
if ($entry['tag'] == "ENTRY") {
$pdfKeys[] = $entry['attributes']['NAME'];
foreach ($section->getEntries() as $entry) {
$pdfKeys[] = $entry->getKey();
}
}
$pdfKeys = array_unique($pdfKeys);
@ -95,7 +92,7 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString =
$fontName = "Dejavu";
// 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
foreach($accounts as $account) {
@ -107,28 +104,10 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString =
$entries = $account->get_pdfEntries($pdfKeys, $account_type->getId());
// Now create the PDF file acording to the structure with the submitted values
foreach($structure as $entry) {
// We have a new section to start
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") {
foreach ($structure->getSections() as $section) {
if ($section instanceof PDFTextSection) {
$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);
foreach ($info_string as $text) {
$pdf->MultiCell(0, LAMPDF_LINEHEIGHT, trim($text), 0, "L", 0);
@ -136,15 +115,23 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString =
}
$pdf->Ln(LAMPDF_LINEHEIGHT * 2);
}
// We have to include an entry from the account
elseif($entry['tag'] == "ENTRY") {
// Get name of current entry
$name = $entry['attributes']['NAME'];
elseif ($section instanceof PDFEntrySection) {
// section headline
if($section->isAttributeTitle()) {
$section_headline = getSectionHeadline($entries[$section->getPdfKey()][0]);
}
else {
$section_headline = $section->getTitle();
}
$pdf->setFont($fontName, "B", LAMPDF_FONT_SIZE_BIG);
$pdf->Write(0,$section_headline . ":");
$pdf->Ln(LAMPDF_LINEHEIGHT);
// entries
foreach ($section->getEntries() as $entry) {
// skip non-existent entries
if (isset($entries[$name])) {
if (isset($entries[$entry->getKey()])) {
// Get current entry
$valueEntries = $entries[$name];
$valueEntries = $entries[$entry->getKey()];
// 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)
@ -159,9 +146,11 @@ function createModulePDF($accounts, $pdf_structure="default", $returnAsString =
}
}
}
// section end
$pdf->Ln(LAMPDF_LINEHEIGHT * 2);
}
}
}
// Close PDF
$pdf->Close();
if (!$returnAsString) {

View File

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

View File

@ -2,6 +2,7 @@
use LAM\PDF\PDFTextSection;
use LAM\PDF\PDFEntrySection;
use LAM\PDF\PDFStructureReader;
use LAM\PDF\PDFStructure;
/*
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');
$this->assertEquals('printLogo.jpg', $structure->getLogo());
$this->assertEquals('User information', $structure->getTitle());
$this->assertEquals(true, $structure->getFoldingMarks());
$this->assertEquals(PDFStructure::FOLDING_STANDARD, $structure->getFoldingMarks());
$sections = $structure->getSections();
$this->assertEquals(3, sizeof($sections));
// 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">
<entry name="inetOrgPerson_givenName" />
<entry name="inetOrgPerson_sn">