2020-07-09 16:05:17 +02:00
|
|
|
<?php
|
|
|
|
|
2020-07-22 14:30:55 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-22 14:30:55 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\PdfMaker;
|
2020-07-09 16:05:17 +02:00
|
|
|
|
|
|
|
use DOMDocument;
|
|
|
|
use DOMXPath;
|
|
|
|
|
|
|
|
trait PdfMakerUtilities
|
|
|
|
{
|
|
|
|
private function initializeDomDocument()
|
|
|
|
{
|
|
|
|
$document = new DOMDocument();
|
|
|
|
|
|
|
|
$document->validateOnParse = true;
|
2020-11-03 16:18:07 +01:00
|
|
|
@$document->loadHTML(mb_convert_encoding($this->design->html(), 'HTML-ENTITIES', 'UTF-8'));
|
2020-07-13 14:16:18 +02:00
|
|
|
|
2020-07-09 16:05:17 +02:00
|
|
|
$this->document = $document;
|
|
|
|
$this->xpath = new DOMXPath($document);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSection(string $selector, string $section = null)
|
|
|
|
{
|
|
|
|
$element = $this->document->getElementById($selector);
|
|
|
|
|
|
|
|
if ($section) {
|
|
|
|
return $element->getAttribute($section);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $element->nodeValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSectionNode(string $selector)
|
|
|
|
{
|
|
|
|
return $this->document->getElementById($selector);
|
|
|
|
}
|
|
|
|
|
2020-07-13 17:49:28 +02:00
|
|
|
public function updateElementProperties(array $elements)
|
|
|
|
{
|
|
|
|
foreach ($elements as $element) {
|
2020-08-25 10:51:49 +02:00
|
|
|
if (isset($element['tag'])) {
|
|
|
|
$node = $this->document->getElementsByTagName($element['tag'])->item(0);
|
2020-09-08 14:26:13 +02:00
|
|
|
} elseif (!is_null($this->document->getElementById($element['id']))) {
|
2020-08-25 10:51:49 +02:00
|
|
|
$node = $this->document->getElementById($element['id']);
|
2020-09-04 10:18:41 +02:00
|
|
|
} else {
|
|
|
|
continue;
|
2020-08-25 10:51:49 +02:00
|
|
|
}
|
2020-07-13 17:49:28 +02:00
|
|
|
|
|
|
|
if (isset($element['properties'])) {
|
|
|
|
foreach ($element['properties'] as $property => $value) {
|
2020-07-14 13:50:00 +02:00
|
|
|
$this->updateElementProperty($node, $property, $value);
|
2020-07-13 17:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($element['elements'])) {
|
2020-07-14 14:35:27 +02:00
|
|
|
$sorted = $this->processChildrenOrder($element['elements']);
|
|
|
|
|
|
|
|
$this->createElementContent($node, $sorted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processChildrenOrder(array $children)
|
|
|
|
{
|
|
|
|
$processed = [];
|
|
|
|
|
2020-08-10 16:43:25 +02:00
|
|
|
foreach ($children as $child) {
|
2020-09-08 14:26:13 +02:00
|
|
|
if (!isset($child['order'])) {
|
2020-07-14 14:35:27 +02:00
|
|
|
$child['order'] = 0;
|
2020-07-13 17:49:28 +02:00
|
|
|
}
|
2020-07-14 14:35:27 +02:00
|
|
|
|
|
|
|
$processed[] = $child;
|
2020-07-13 17:49:28 +02:00
|
|
|
}
|
2020-07-14 15:05:29 +02:00
|
|
|
|
2020-07-14 14:35:27 +02:00
|
|
|
usort($processed, function ($a, $b) {
|
|
|
|
return $a['order'] <=> $b['order'];
|
|
|
|
});
|
2020-07-14 15:05:29 +02:00
|
|
|
|
2020-07-14 14:35:27 +02:00
|
|
|
return $processed;
|
2020-07-13 17:49:28 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 13:50:00 +02:00
|
|
|
public function updateElementProperty($element, string $attribute, string $value)
|
2020-07-09 16:05:17 +02:00
|
|
|
{
|
2020-08-10 16:43:25 +02:00
|
|
|
// We have exception for "hidden" property.
|
2020-09-06 11:38:10 +02:00
|
|
|
// hidden="true" or hidden="false" will both hide the element,
|
2020-08-10 16:43:25 +02:00
|
|
|
// that's why we have to create an exception here for this rule.
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($attribute == 'hidden' && ($value == false || $value == 'false')) {
|
2020-08-10 16:43:25 +02:00
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:05:17 +02:00
|
|
|
$element->setAttribute($attribute, $value);
|
|
|
|
|
|
|
|
if ($element->getAttribute($attribute) === $value) {
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
2020-07-13 17:49:28 +02:00
|
|
|
public function createElementContent($element, $children)
|
|
|
|
{
|
|
|
|
foreach ($children as $child) {
|
2020-10-09 15:05:57 +02:00
|
|
|
$contains_html = false;
|
|
|
|
|
|
|
|
// "/\/[a-z]*>/i" -> checks for HTML-like tags:
|
|
|
|
// <my-tag></my-tag> => true
|
|
|
|
// <my-tag /> => true
|
|
|
|
// <my-tag> => false
|
|
|
|
|
2020-10-27 23:51:39 +01:00
|
|
|
if (isset($child['content'])) {
|
|
|
|
if (isset($child['is_empty']) && $child['is_empty'] === true) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$contains_html = preg_match("/\/[a-z]*>/i", $child['content'], $m) != 0;
|
2020-10-09 15:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($contains_html) {
|
|
|
|
// Support for injecting direct HTML into elements.
|
|
|
|
// Example: Without documentFragment(): <b>Hello!</b> will result: <b>Hello!</b>
|
|
|
|
// With document fragment we can evaluate HTML directly.
|
|
|
|
|
|
|
|
$_child = $this->document->createElement($child['element'], '');
|
|
|
|
|
|
|
|
$fragment = $this->document->createDocumentFragment();
|
|
|
|
$fragment->appendXML($child['content']);
|
|
|
|
|
|
|
|
$_child->appendChild($fragment);
|
|
|
|
} else {
|
|
|
|
// .. in case string doesn't contain any HTML, we'll just return
|
|
|
|
// raw $content.
|
|
|
|
|
2020-11-16 00:49:44 +01:00
|
|
|
$_child = $this->document->createElement($child['element'], isset($child['content']) ? htmlspecialchars($child['content']) : '');
|
2020-10-09 15:05:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-13 17:49:28 +02:00
|
|
|
$element->appendChild($_child);
|
2020-07-14 13:50:00 +02:00
|
|
|
|
|
|
|
if (isset($child['properties'])) {
|
|
|
|
foreach ($child['properties'] as $property => $value) {
|
|
|
|
$this->updateElementProperty($_child, $property, $value);
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 14:35:27 +02:00
|
|
|
|
2020-07-13 17:49:28 +02:00
|
|
|
if (isset($child['elements'])) {
|
2020-07-14 15:05:29 +02:00
|
|
|
$sorted = $this->processChildrenOrder($child['elements']);
|
|
|
|
|
|
|
|
$this->createElementContent($_child, $sorted);
|
2020-07-13 17:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 14:16:18 +02:00
|
|
|
public function updateVariables(array $variables)
|
|
|
|
{
|
2020-07-29 13:37:05 +02:00
|
|
|
$html = strtr($this->getCompiledHTML(), $variables['labels']);
|
2020-08-10 16:43:25 +02:00
|
|
|
|
2020-07-30 16:43:57 +02:00
|
|
|
$html = strtr($html, $variables['values']);
|
2020-07-13 14:16:18 +02:00
|
|
|
|
2020-10-17 02:24:02 +02:00
|
|
|
@$this->document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
|
2020-07-13 14:16:18 +02:00
|
|
|
|
|
|
|
$this->document->saveHTML();
|
|
|
|
}
|
|
|
|
|
2020-07-09 16:05:17 +02:00
|
|
|
public function updateVariable(string $element, string $variable, string $value)
|
|
|
|
{
|
|
|
|
$element = $this->document->getElementById($element);
|
|
|
|
|
|
|
|
$original = $element->nodeValue;
|
|
|
|
|
|
|
|
$element->nodeValue = '';
|
|
|
|
|
|
|
|
$replaced = strtr($original, [$variable => $value]);
|
|
|
|
|
|
|
|
$element->appendChild(
|
|
|
|
$this->document->createTextNode($replaced)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
}
|
2020-08-25 10:51:49 +02:00
|
|
|
|
|
|
|
public function processOptions()
|
|
|
|
{
|
2020-09-08 14:26:13 +02:00
|
|
|
if (!isset($this->options['all_pages_header']) || $this->options['all_pages_header'] == false) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-09 15:05:57 +02:00
|
|
|
|
2020-09-08 14:26:13 +02:00
|
|
|
if (!isset($this->options['all_pages_footer']) || $this->options['all_pages_footer'] == false) {
|
2020-08-27 08:39:14 +02:00
|
|
|
return;
|
2020-08-25 10:51:49 +02:00
|
|
|
}
|
2020-08-27 08:39:14 +02:00
|
|
|
|
|
|
|
$this->insertPrintCSS();
|
|
|
|
$this->wrapIntoTable();
|
2020-08-25 10:51:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function insertPrintCSS()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
$css = <<<'EOT'
|
2020-08-26 09:02:38 +02:00
|
|
|
table.page-container {
|
|
|
|
page-break-after: always;
|
|
|
|
}
|
2020-10-09 15:05:57 +02:00
|
|
|
|
2020-08-26 09:02:38 +02:00
|
|
|
thead.page-header {
|
|
|
|
display: table-header-group;
|
|
|
|
}
|
|
|
|
|
|
|
|
tfoot.page-footer {
|
|
|
|
display: table-footer-group;
|
|
|
|
}
|
|
|
|
EOT;
|
2020-08-25 10:51:49 +02:00
|
|
|
|
|
|
|
$css_node = $this->document->createTextNode($css);
|
|
|
|
|
|
|
|
$style = $this->document->getElementsByTagName('style')->item(0);
|
|
|
|
|
|
|
|
if ($style) {
|
|
|
|
return $style->appendChild($css_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
$head = $this->document->getElementsByTagName('head')->item(0);
|
|
|
|
|
|
|
|
if ($head) {
|
|
|
|
$style_node = $this->document->createElement('style', $css);
|
|
|
|
|
|
|
|
return $head->appendChild($style_node);
|
|
|
|
}
|
2020-10-09 15:05:57 +02:00
|
|
|
|
|
|
|
return $this;
|
2020-08-25 10:51:49 +02:00
|
|
|
}
|
2020-08-26 09:02:38 +02:00
|
|
|
|
|
|
|
public function wrapIntoTable()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
$markup = <<<'EOT'
|
2020-08-26 09:02:38 +02:00
|
|
|
<table class="page-container" id="page-container">
|
|
|
|
<thead class="page-report">
|
|
|
|
<tr>
|
|
|
|
<th class="page-report-cell" id="repeat-header">
|
|
|
|
<!-- Repeating header goes here.. -->
|
|
|
|
</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tfoot class="report-footer">
|
|
|
|
<tr>
|
|
|
|
<td class="report-footer-cell" id="repeat-footer">
|
|
|
|
<!-- Repeating footer goes here -->
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</tfoot>
|
|
|
|
<tbody class="report-content">
|
|
|
|
<tr>
|
|
|
|
<td class="report-content-cell" id="repeat-content">
|
|
|
|
<!-- Rest of the content goes here -->
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
EOT;
|
|
|
|
|
|
|
|
$document = new DOMDocument();
|
|
|
|
$document->loadHTML($markup);
|
|
|
|
|
|
|
|
$table = $document->getElementById('page-container');
|
|
|
|
|
2020-08-26 12:52:20 +02:00
|
|
|
$body = $this->document->getElementsByTagName('body')
|
|
|
|
->item(0);
|
2020-08-26 09:02:38 +02:00
|
|
|
|
2020-08-26 12:52:20 +02:00
|
|
|
$body->appendChild(
|
|
|
|
$this->document->importNode($table, true)
|
|
|
|
);
|
|
|
|
|
|
|
|
for ($i = 0; $i < $body->childNodes->length; $i++) {
|
|
|
|
$element = $body->childNodes->item($i);
|
|
|
|
|
|
|
|
if ($element->nodeType !== 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
$element->getAttribute('id') == 'header' ||
|
|
|
|
$element->getAttribute('id') == 'footer' ||
|
|
|
|
$element->getAttribute('id') === 'page-container'
|
|
|
|
) {
|
2020-08-26 09:02:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-26 12:52:20 +02:00
|
|
|
$clone = $element->cloneNode(true);
|
|
|
|
$element->parentNode->removeChild($element);
|
2020-08-26 09:02:38 +02:00
|
|
|
|
2020-08-26 12:52:20 +02:00
|
|
|
$this->document->getElementById('repeat-content')->appendChild($clone);
|
2020-08-26 09:02:38 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 12:14:14 +02:00
|
|
|
// info($this->data['options']);
|
2020-09-08 14:26:13 +02:00
|
|
|
|
2020-08-27 08:39:14 +02:00
|
|
|
if (
|
2020-09-04 10:18:41 +02:00
|
|
|
$header = $this->document->getElementById('header') &&
|
2020-08-27 08:47:51 +02:00
|
|
|
isset($this->data['options']['all_pages_header']) &&
|
|
|
|
$this->data['options']['all_pages_header']
|
2020-08-27 08:39:14 +02:00
|
|
|
) {
|
2020-08-26 09:02:38 +02:00
|
|
|
$header = $this->document->getElementById('header');
|
2020-08-26 12:52:20 +02:00
|
|
|
$clone = $header->cloneNode(true);
|
2020-08-26 09:02:38 +02:00
|
|
|
|
2020-08-26 12:52:20 +02:00
|
|
|
$header->parentNode->removeChild($header);
|
|
|
|
$this->document->getElementById('repeat-header')->appendChild($clone);
|
2020-08-26 09:02:38 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 08:39:14 +02:00
|
|
|
if (
|
|
|
|
$footer = $this->document->getElementById('footer') &&
|
2020-08-27 08:47:51 +02:00
|
|
|
isset($this->data['options']['all_pages_footer']) &&
|
|
|
|
$this->data['options']['all_pages_footer']
|
2020-08-27 08:39:14 +02:00
|
|
|
) {
|
2020-08-26 09:02:38 +02:00
|
|
|
$footer = $this->document->getElementById('footer');
|
2020-08-26 12:52:20 +02:00
|
|
|
$clone = $footer->cloneNode(true);
|
2020-08-26 09:02:38 +02:00
|
|
|
|
2020-08-26 12:52:20 +02:00
|
|
|
$footer->parentNode->removeChild($footer);
|
|
|
|
$this->document->getElementById('repeat-footer')->appendChild($clone);
|
2020-08-26 09:02:38 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-27 23:51:39 +01:00
|
|
|
|
|
|
|
public function getEmptyElements(array &$elements, array $variables) {
|
|
|
|
foreach ($elements as &$element) {
|
|
|
|
if (isset($element['elements'])) {
|
|
|
|
$this->getEmptyChildrens($element['elements'], $variables);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEmptyChildrens(array &$children, array $variables) {
|
|
|
|
foreach ($children as $key => &$child) {
|
|
|
|
if (isset($child['content']) && isset($child['show_empty']) && $child['show_empty'] === false) {
|
|
|
|
$value = strtr($child['content'], $variables['values']);
|
|
|
|
if ($value === '' || $value === ' ') {
|
|
|
|
$child['is_empty'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($child['elements'])) {
|
|
|
|
$this->getEmptyChildrens($child['elements'], $variables);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-13 14:16:18 +02:00
|
|
|
}
|