| 1 |
<?php |
| 2 |
|
| 3 |
namespace LAM\PDF; |
| 4 |
|
| 5 |
use accountContainer; |
| 6 |
use LAM\ImageUtils\ImageManipulationFactory; |
| 7 |
use LAM\TYPES\ConfiguredType; |
| 8 |
use LAMException; |
| 9 |
use LamTemporaryFilesManager; |
| 10 |
|
| 11 |
/* |
| 12 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 13 |
Copyright (C) 2003 - 2004 Michael Duergner |
| 14 |
2003 - 2026 Roland Gruber |
| 15 |
|
| 16 |
This program is free software; you can redistribute it and/or modify |
| 17 |
it under the terms of the GNU General Public License as published by |
| 18 |
the Free Software Foundation; either version 2 of the License, or |
| 19 |
(at your option) any later version. |
| 20 |
|
| 21 |
This program is distributed in the hope that it will be useful, |
| 22 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
GNU General Public License for more details. |
| 25 |
|
| 26 |
You should have received a copy of the GNU General Public License |
| 27 |
along with this program; if not, write to the Free Software |
| 28 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 29 |
*/ |
| 30 |
|
| 31 |
/** |
| 32 |
* LDAP Account Manager PDF printing library. It consists of lamPDF class, |
| 33 |
* the createModulePDF() function that may be called by other pages |
| 34 |
* and furthermore some helper functions. |
| 35 |
* |
| 36 |
* @author Michael Duergner |
| 37 |
* @author Roland Gruber |
| 38 |
* @package PDF |
| 39 |
*/ |
| 40 |
|
| 41 |
/** line width */ |
| 42 |
define('LAMPDF_LINEWIDTH', 190); |
| 43 |
/** line height */ |
| 44 |
define('LAMPDF_LINEHEIGHT', 5); |
| 45 |
/** width of a label */ |
| 46 |
define('LAMPDF_LABELWIDTH', 50); |
| 47 |
/** font size */ |
| 48 |
define('LAMPDF_FONT_SIZE', 7); |
| 49 |
/** font size for bigger text */ |
| 50 |
define('LAMPDF_FONT_SIZE_BIG', 10); |
| 51 |
|
| 52 |
/** access to PDF configuration files */ |
| 53 |
include_once(__DIR__ . '/pdfstruct.inc'); |
| 54 |
|
| 55 |
/** |
| 56 |
* This function creates the PDF output of one or more accounts. |
| 57 |
* |
| 58 |
* @param accountContainer[] $accounts A numbered array containing all accounts the PDF page should |
| 59 |
* be created for. The entries of the array must be AccountContainer objects. |
| 60 |
* @param string $pdf_structure The filename of the structure definition that should be used |
| 61 |
* to create the PDF page. If not submitted the 'default.user' structure definition |
| 62 |
* for the appropriate account type. |
| 63 |
* @param string $font font to use (e.g. DejaVu) |
| 64 |
* @param bool $returnAsString returns the PDF output as String value instead of writing it to a file |
| 65 |
* @return string PDF file name |
| 66 |
* @throws LAMException error creating PDF |
| 67 |
*/ |
| 68 |
function createModulePDF($accounts, $pdf_structure, $font, $returnAsString = false): string { |
| 69 |
$account_type = $accounts[0]->get_type(); |
| 70 |
// Get PDF structure from xml file |
| 71 |
$pdfStructurePersistenceManager = new PdfStructurePersistenceManager(); |
| 72 |
$structure = $pdfStructurePersistenceManager->readPdfStructure($_SESSION['config']->getName(), |
| 73 |
$account_type->getId(), $pdf_structure); |
| 74 |
// get list of PDF keys |
| 75 |
$pdfKeys = []; |
| 76 |
foreach ($structure->getSections() as $section) { |
| 77 |
if (!$section instanceof PDFEntrySection) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
if ($section->isAttributeTitle()) { |
| 81 |
$pdfKeys[] = $section->getPdfKey(); |
| 82 |
} |
| 83 |
foreach ($section->getEntries() as $entry) { |
| 84 |
$pdfKeys[] = $entry->getKey(); |
| 85 |
} |
| 86 |
} |
| 87 |
$pdfKeys = array_unique($pdfKeys); |
| 88 |
return createPdf($structure, $accounts, $pdfKeys, $account_type, $font, $returnAsString); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* This function creates the PDF output of one or more accounts using TCPDF. |
| 93 |
* |
| 94 |
* @param PDFStructure $structure PDF structure |
| 95 |
* @param accountContainer[] $accounts A numbered array containing all accounts the PDF page should |
| 96 |
* be created for. The entries of the array must be AccountContainer objects. |
| 97 |
* @param string[] $pdfKeys list of elements in PDF |
| 98 |
* @param ConfiguredType $account_type account type |
| 99 |
* @param string $font font to use |
| 100 |
* @param boolean $returnAsString returns the PDF output as String value instead of writing it to a file |
| 101 |
* @return string PDF file name |
| 102 |
*/ |
| 103 |
function createPdf($structure, $accounts, $pdfKeys, $account_type, $font, $returnAsString): string { |
| 104 |
if (!in_array($font, getPdfFonts())) { |
| 105 |
logNewMessage(LOG_ERR, 'Invalid font specified: ' . $font); |
| 106 |
} |
| 107 |
/** PDF generator class */ |
| 108 |
include_once(__DIR__ . "/lamtcpdf.inc"); |
| 109 |
$pdf = new LAMTCPDF($structure, $font); |
| 110 |
|
| 111 |
// Loop over each account and add a new page in the PDF file for it |
| 112 |
foreach ($accounts as $account) { |
| 113 |
|
| 114 |
// Start a new page for each account |
| 115 |
$pdf->AddPage(); |
| 116 |
|
| 117 |
// Get PDF entries for the current account |
| 118 |
$entries = $account->get_pdfEntries($pdfKeys, $account_type->getId()); |
| 119 |
|
| 120 |
// Now create the PDF file according to the structure with the submitted values |
| 121 |
foreach ($structure->getSections() as $section) { |
| 122 |
if ($section instanceof PDFTextSection) { |
| 123 |
$pdf->setFont($font, "", LAMPDF_FONT_SIZE); |
| 124 |
$info_string = str_replace("\r", "", $section->getText()); |
| 125 |
$info_string = explode("\n", $info_string); |
| 126 |
foreach ($info_string as $text) { |
| 127 |
$pdf->MultiCell(0, LAMPDF_LINEHEIGHT, trim($text), 0, "L"); |
| 128 |
$pdf->Ln(0); |
| 129 |
} |
| 130 |
$pdf->Ln(LAMPDF_LINEHEIGHT * 2); |
| 131 |
} |
| 132 |
elseif ($section instanceof PDFEntrySection) { |
| 133 |
// section headline |
| 134 |
if ($section->isAttributeTitle()) { |
| 135 |
if (empty($entries[$section->getPdfKey()][0])) { |
| 136 |
continue; |
| 137 |
} |
| 138 |
$section_headline = getSectionHeadline($entries[$section->getPdfKey()][0]); |
| 139 |
} |
| 140 |
else { |
| 141 |
$section_headline = $section->getTitle(); |
| 142 |
} |
| 143 |
$pdf->setFont($font, "B", LAMPDF_FONT_SIZE_BIG); |
| 144 |
$pdf->Write(0, $section_headline, '', false, 'L', true); |
| 145 |
$pdf->Ln(LAMPDF_LINEHEIGHT); |
| 146 |
// entries |
| 147 |
foreach ($section->getEntries() as $entry) { |
| 148 |
// skip non-existent entries |
| 149 |
if (isset($entries[$entry->getKey()])) { |
| 150 |
// Get current entry |
| 151 |
$valueEntries = $entries[$entry->getKey()]; |
| 152 |
// Loop over all rows of this entry (most of the time this will be just one) |
| 153 |
foreach ($valueEntries as $valueEntry) { |
| 154 |
if ($valueEntry instanceof PDFLabelValue) { |
| 155 |
printLabelValue($pdf, $valueEntry, $font); |
| 156 |
} |
| 157 |
elseif ($valueEntry instanceof PDFTable) { |
| 158 |
printTable($pdf, $valueEntry, $font); |
| 159 |
} |
| 160 |
elseif ($valueEntry instanceof PDFImage) { |
| 161 |
printImage($pdf, $valueEntry); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
// section end |
| 167 |
$pdf->Ln(LAMPDF_LINEHEIGHT * 2); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
$out = $pdf->Output('out.pdf', 'S'); |
| 173 |
if (!$returnAsString) { |
| 174 |
$tempFilesManager = new LamTemporaryFilesManager(); |
| 175 |
$fileName = $tempFilesManager->registerTemporaryFile('.pdf'); |
| 176 |
// Save PDF |
| 177 |
$handle = $tempFilesManager->openTemporaryFileForWrite($fileName); |
| 178 |
fwrite($handle, $out); |
| 179 |
fclose($handle); |
| 180 |
// return PDF file name |
| 181 |
return $fileName; |
| 182 |
} |
| 183 |
else { |
| 184 |
return $out; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Creates a section headline. |
| 190 |
* |
| 191 |
* @param PDFEntry $entry content entry |
| 192 |
* |
| 193 |
* @return string headline |
| 194 |
*/ |
| 195 |
function getSectionHeadline($entry) { |
| 196 |
return $entry->getHeadline(); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Prints a PDFLabelValue entry. |
| 201 |
* |
| 202 |
* @param LAMTCPDF $pdf PDF |
| 203 |
* @param PDFLabelValue $valueEntry entry |
| 204 |
* @param string $fontName font name |
| 205 |
*/ |
| 206 |
function printLabelValue(&$pdf, $valueEntry, $fontName): void { |
| 207 |
$pdf->SetFont($fontName, 'B', LAMPDF_FONT_SIZE); |
| 208 |
$pdf->MultiCell(LAMPDF_LABELWIDTH, LAMPDF_LINEHEIGHT, replaceSpecialCharacters($valueEntry->getLabel()) . ':', |
| 209 |
0, 'R', false, 0); |
| 210 |
$pdf->SetFont($fontName, '', LAMPDF_FONT_SIZE); |
| 211 |
$pdf->MultiCell(0, LAMPDF_LINEHEIGHT, replaceSpecialCharacters($valueEntry->getValue()), |
| 212 |
0, 'L', false, 1, null, null, true, 0, false, |
| 213 |
true, 0, 'M'); |
| 214 |
$pdf->Ln(0); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Prints a PDFTable entry. |
| 219 |
* |
| 220 |
* @param LAMTCPDF $pdf PDF |
| 221 |
* @param PDFTable $table entry |
| 222 |
* @param string $fontName font name |
| 223 |
*/ |
| 224 |
function printTable(&$pdf, $table, $fontName): void { |
| 225 |
$headline = $table->getHeadline(); |
| 226 |
if (!empty($headline)) { |
| 227 |
$pdf->SetFont($fontName, 'B', LAMPDF_FONT_SIZE); |
| 228 |
$pdf->Cell(LAMPDF_LABELWIDTH, LAMPDF_LINEHEIGHT, replaceSpecialCharacters($headline) . ':', |
| 229 |
0, 0, 'L'); |
| 230 |
$pdf->SetFont($fontName, '', LAMPDF_FONT_SIZE); |
| 231 |
$pdf->Ln(LAMPDF_LINEHEIGHT); |
| 232 |
} |
| 233 |
foreach ($table->rows as $row) { |
| 234 |
foreach ($row->cells as $cell) { |
| 235 |
$width = $cell->width; |
| 236 |
if (!empty($width) && (str_contains($width, '%'))) { |
| 237 |
$width = ceil(LAMPDF_LINEWIDTH * ((int) substr($width, 0, -1)) / 100); |
| 238 |
} |
| 239 |
if ($cell->bold) { |
| 240 |
$pdf->SetFont($fontName, 'B', LAMPDF_FONT_SIZE); |
| 241 |
} |
| 242 |
$pdf->Cell($width, LAMPDF_LINEHEIGHT, replaceSpecialCharacters($cell->content), 0, |
| 243 |
0, $cell->align, false, '', 1); |
| 244 |
if ($cell->bold) { |
| 245 |
$pdf->SetFont($fontName, '', LAMPDF_FONT_SIZE); |
| 246 |
} |
| 247 |
} |
| 248 |
$pdf->Ln(LAMPDF_LINEHEIGHT); |
| 249 |
} |
| 250 |
$pdf->Ln(LAMPDF_LINEHEIGHT); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Prints an image entry. |
| 255 |
* |
| 256 |
* @param LAMTCPDF $pdf PDF |
| 257 |
* @param PDFImage $imageEntry entry |
| 258 |
*/ |
| 259 |
function printImage(&$pdf, $imageEntry): void { |
| 260 |
include_once __DIR__ . '/imageutils.inc'; |
| 261 |
$imageManipulator = ImageManipulationFactory::getImageManipulator($imageEntry->getImageData()); |
| 262 |
$height = $imageManipulator->getHeight() / 2.9; |
| 263 |
if ($height > 40) { |
| 264 |
$height = 40; |
| 265 |
} |
| 266 |
$pdf->Image('@' . $imageManipulator->getImageData(), null, null, 0, $height, |
| 267 |
'JPG', null, 'T', true, 300, 'R', |
| 268 |
false, false, 0, false, false, false); |
| 269 |
$pdf->Ln($height); |
| 270 |
$pdf->Ln(LAMPDF_LINEHEIGHT); |
| 271 |
$imageManipulator = null; |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Replaces special characters that cannot be printed in PDF. |
| 276 |
* Can be removed once tc-lib-pdf is used. |
| 277 |
* |
| 278 |
* @param string|null $text input text |
| 279 |
* @return string output text |
| 280 |
*/ |
| 281 |
function replaceSpecialCharacters(?string $text): string { |
| 282 |
if (empty($text)) { |
| 283 |
return $text; |
| 284 |
} |
| 285 |
return str_replace('❭', '>', $text); |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Common interface for all PDF entries. |
| 290 |
* |
| 291 |
* @package PDF |
| 292 |
* @author Roland Gruber |
| 293 |
*/ |
| 294 |
interface PDFEntry { |
| 295 |
|
| 296 |
/** |
| 297 |
* Returns the head line of the entry. |
| 298 |
* |
| 299 |
* @return string|null label |
| 300 |
*/ |
| 301 |
public function getHeadline(): ?string; |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Represents a table for PDF export. |
| 307 |
* |
| 308 |
* @package PDF |
| 309 |
* @author Roland Gruber |
| 310 |
*/ |
| 311 |
class PDFTable implements PDFEntry { |
| 312 |
|
| 313 |
/** optional label of table */ |
| 314 |
private ?string $label; |
| 315 |
/** @var PDFTableRow[] $rows list of PDFTableRow elements */ |
| 316 |
public array $rows = []; |
| 317 |
|
| 318 |
/** |
| 319 |
* Constructor |
| 320 |
* |
| 321 |
* @param string|null $label label |
| 322 |
*/ |
| 323 |
public function __construct(?string $label = null) { |
| 324 |
$this->label = $label; |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* {@inheritDoc} |
| 329 |
* @see PDFEntry::getHeadline() |
| 330 |
*/ |
| 331 |
public function getHeadline(): ?string { |
| 332 |
return $this->label; |
| 333 |
} |
| 334 |
|
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Represents a table row for PDF export. |
| 339 |
* |
| 340 |
* @package PDF |
| 341 |
* @author Roland Gruber |
| 342 |
*/ |
| 343 |
class PDFTableRow { |
| 344 |
|
| 345 |
/** @var PDFTableCell[] $cells list of PDFTableCell */ |
| 346 |
public array $cells = []; |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Represents a table cell for PDF export. |
| 352 |
* |
| 353 |
* @package PDF |
| 354 |
* @author Roland Gruber |
| 355 |
*/ |
| 356 |
class PDFTableCell { |
| 357 |
|
| 358 |
public const ALIGN_LEFT = 'L'; |
| 359 |
public const ALIGN_RIGHT = 'R'; |
| 360 |
public const ALIGN_CENTER = 'C'; |
| 361 |
|
| 362 |
/** content text of cell */ |
| 363 |
public string $content = ''; |
| 364 |
/** text alignment */ |
| 365 |
public string $align = self::ALIGN_LEFT; |
| 366 |
/** cell width (e.g. "20%") */ |
| 367 |
public ?string $width; |
| 368 |
/** bold text */ |
| 369 |
public bool $bold = false; |
| 370 |
|
| 371 |
/** |
| 372 |
* Constructor. |
| 373 |
* |
| 374 |
* @param string|null $content cell content |
| 375 |
* @param string|null $width width (e.g. "20%") |
| 376 |
* @param string|null $align cell alignment (default: left) |
| 377 |
* @param boolean $bold print in bold |
| 378 |
*/ |
| 379 |
public function __construct(?string $content, ?string $width = null, ?string $align = null, bool $bold = false) { |
| 380 |
$this->content = empty($content) ? ' ' : $content; |
| 381 |
$this->align = ($align == null) ? self::ALIGN_LEFT : $align; |
| 382 |
$this->width = $width; |
| 383 |
$this->bold = $bold; |
| 384 |
} |
| 385 |
|
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Simple PDF object to print label value entries. |
| 390 |
* |
| 391 |
* @package PDF |
| 392 |
* @author Roland Gruber |
| 393 |
*/ |
| 394 |
class PDFLabelValue implements PDFEntry { |
| 395 |
|
| 396 |
private string $label; |
| 397 |
|
| 398 |
private string $value; |
| 399 |
|
| 400 |
/** |
| 401 |
* Constructor |
| 402 |
* |
| 403 |
* @param string $label label |
| 404 |
* @param string $value value |
| 405 |
*/ |
| 406 |
public function __construct(string $label, string $value) { |
| 407 |
$this->label = $label; |
| 408 |
$this->value = $value; |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Returns the label. |
| 413 |
* |
| 414 |
* @return string $label label |
| 415 |
*/ |
| 416 |
public function getLabel(): string { |
| 417 |
return $this->label; |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Returns the value. |
| 422 |
* |
| 423 |
* @return string $value value |
| 424 |
*/ |
| 425 |
public function getValue(): string { |
| 426 |
return $this->value; |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* {@inheritDoc} |
| 431 |
* @see PDFEntry::getHeadline() |
| 432 |
*/ |
| 433 |
public function getHeadline(): ?string { |
| 434 |
return $this->value; |
| 435 |
} |
| 436 |
|
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Adds an image to the PDF file. |
| 441 |
* |
| 442 |
* @package PDF |
| 443 |
* @author Roland Gruber |
| 444 |
*/ |
| 445 |
class PDFImage implements PDFEntry { |
| 446 |
|
| 447 |
private string $binaryData; |
| 448 |
|
| 449 |
/** |
| 450 |
* Constructor |
| 451 |
* |
| 452 |
* @param string $binaryData image data |
| 453 |
*/ |
| 454 |
public function __construct(string $binaryData) { |
| 455 |
$this->binaryData = $binaryData; |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* {@inheritDoc} |
| 460 |
* @see \LAM\PDF\PDFEntry::getHeadline() |
| 461 |
*/ |
| 462 |
public function getHeadline(): ?string { |
| 463 |
return ''; |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* Returns the image data. |
| 468 |
* |
| 469 |
* @return string image data |
| 470 |
*/ |
| 471 |
public function getImageData(): string { |
| 472 |
return $this->binaryData; |
| 473 |
} |
| 474 |
|
| 475 |
} |