Merge pull request #4018 from beganovich/v2-repeating-header-and-footer

Repeating header & footer
This commit is contained in:
David Bomba 2020-08-28 07:07:28 +10:00 committed by GitHub
commit c9be746583
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 218 additions and 74 deletions

View File

@ -99,6 +99,10 @@ class CreateInvoicePdf implements ShouldQueue
'product-table-columns' => $pdf_variables['product_columns'],
]),
'variables' => $html->generateLabelsAndValues(),
'options' => [
'all_pages_header' => $this->invoice->client->getSetting('all_pages_header'),
'all_pages_footer' => $this->invoice->client->getSetting('all_pages_footer'),
],
];
$maker = new PdfMakerService($state);
@ -110,6 +114,8 @@ class CreateInvoicePdf implements ShouldQueue
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
Storage::makeDirectory($path, 0775);
info($maker->getCompiledHTML());
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML());
$instance = Storage::disk($this->disk)->put($file_path, $pdf);

View File

@ -97,6 +97,10 @@ class CreateQuotePdf implements ShouldQueue
'product-table-columns' => $pdf_variables['product_columns'],
]),
'variables' => $html->generateLabelsAndValues(),
'options' => [
'all_pages_header' => $this->quote->client->getSetting('all_pages_header'),
'all_pages_footer' => $this->quote->client->getSetting('all_pages_footer'),
],
];
$maker = new PdfMakerService($state);

View File

@ -32,9 +32,15 @@ class PdfMaker
'<?xml version="1.0" encoding="utf-8" standalone="yes"??>' => '',
];
private $options;
public function __construct(array $data)
{
$this->data = $data;
if (array_key_exists('options', $data)) {
$this->options = $data['options'];
}
}
public function design(string $design)
@ -56,6 +62,8 @@ class PdfMaker
$this->updateVariables($this->data['variables']);
}
$this->processOptions();
return $this;
}
@ -63,12 +71,12 @@ class PdfMaker
{
if ($final) {
$html = $this->document->saveXML();
$filtered = strtr($html, $this->filters);
return $filtered;
}
return $this->document->saveXML();
}
}

View File

@ -13,6 +13,7 @@
namespace App\Services\PdfMaker;
use DOMDocument;
use DOMDomError;
use DOMXPath;
trait PdfMakerUtilities
@ -47,7 +48,11 @@ trait PdfMakerUtilities
public function updateElementProperties(array $elements)
{
foreach ($elements as $element) {
$node = $this->document->getElementById($element['id']);
if (isset($element['tag'])) {
$node = $this->document->getElementsByTagName($element['tag'])->item(0);
} else {
$node = $this->document->getElementById($element['id']);
}
if (isset($element['properties'])) {
foreach ($element['properties'] as $property => $value) {
@ -104,7 +109,6 @@ trait PdfMakerUtilities
public function createElementContent($element, $children)
{
foreach ($children as $child) {
$_child = $this->document->createElement($child['element'], $child['content']);
$element->appendChild($_child);
@ -149,4 +153,134 @@ trait PdfMakerUtilities
return $element;
}
public function processOptions()
{
if (!isset($this->options['all_pages_header']) && !isset($this->options['all_pages_footer'])) {
return;
}
$this->insertPrintCSS();
$this->wrapIntoTable();
}
public function insertPrintCSS()
{
$css = <<<EOT
table.page-container {
page-break-after: always;
}
thead.page-header {
display: table-header-group;
}
tfoot.page-footer {
display: table-footer-group;
}
EOT;
$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);
}
}
public function wrapIntoTable()
{
$markup = <<<EOT
<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');
$body = $this->document->getElementsByTagName('body')
->item(0);
$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'
) {
continue;
}
$clone = $element->cloneNode(true);
$element->parentNode->removeChild($element);
$this->document->getElementById('repeat-content')->appendChild($clone);
}
if (
$header = $this->document->getElementById('header') &&
isset($this->data['options']['all_pages_header']) &&
$this->data['options']['all_pages_header']
) {
$header = $this->document->getElementById('header');
$clone = $header->cloneNode(true);
$header->parentNode->removeChild($header);
$this->document->getElementById('repeat-header')->appendChild($clone);
}
if (
$footer = $this->document->getElementById('footer') &&
isset($this->data['options']['all_pages_footer']) &&
$this->data['options']['all_pages_footer']
) {
$footer = $this->document->getElementById('footer');
$clone = $footer->cloneNode(true);
$footer->parentNode->removeChild($footer);
$this->document->getElementById('repeat-footer')->appendChild($clone);
}
}
}

View File

@ -26,7 +26,7 @@
<body class="antialiased break-words bg-white">
<!-- Company logo, company details -->
<div class="$global-padding static bg-gray-800">
<div class="$global-padding static bg-gray-800" id="header">
<div class="grid grid-cols-12">
<div class="absolute col-span-4 p-6 pb-10 bg-white">
<img

View File

@ -37,7 +37,7 @@
<body class="$global-margin bg-white break-words antialiased">
<!-- Logo, company details & company address -->
<div class="flex grid items-start grid-cols-12 gap-4">
<div class="flex grid items-start grid-cols-12 gap-4" id="header">
<img
src="$company.logo"
alt="$company.name"

View File

@ -27,7 +27,7 @@
<body class="$global-margin bg-white break-words antialiased">
<!-- Company logo, company details -->
<div class="grid grid-cols-12 px-2">
<div class="grid grid-cols-12 px-2" id="header">
<img
src="$company_logo"
alt="$company_name logo"

View File

@ -31,7 +31,7 @@
<body class="$global-margin antialiased bg-white break-words">
<!-- Client details, company details, company logo -->
<div class="grid grid-cols-12 gap-4">
<div class="grid grid-cols-12 gap-4" id="header">
<!-- Client details -->
<div class="col-span-4">
<div id="client-details"></div>

View File

@ -23,7 +23,7 @@
<body class="$global-margin antialiased bg-white break-words">
<!-- Company logo, entity details -->
<div class="grid grid-cols-12 gap-4 pb-6 border-b-4 border-black">
<div class="grid grid-cols-12 gap-4 pb-6 border-b-4 border-black" id="header">
<div class="col-span-6">
<img
src="$company.logo"
@ -34,7 +34,10 @@
<div class="flex flex-col items-end col-span-6">
<table id="entity-details"></table>
</div>
<div class="col-span-6">
</div>
<div class="grid grid-cols-12">
<div class="col-span-6 py-6">
<p class="text-xl font-semibold uppercase">
$your_entity_label
</p>

View File

@ -23,7 +23,7 @@
<body class="$global-margin antialiased break-words bg-white">
<!-- Company details, address, client details, company logo -->
<div class="grid grid-cols-12 gap-4">
<div class="grid grid-cols-12 gap-4" id="header">
<div class="col-span-4 pl-4 border-l border-black">
<div id="company-details">
<p class="font-semibold text-yellow-600 uppercase">

View File

@ -13,7 +13,7 @@
<body class="antialiased break-words bg-white">
<!-- Company name, entity details -->
<div class="bg-orange-600">
<div class="bg-orange-600" id="header">
<div class="$global-padding">
<div class="grid grid-cols-12">
<div class="col-span-4">

View File

@ -23,7 +23,7 @@
<body class="$global-margin antialiased break-words bg-white">
<!-- Company name, company address, company logo -->
<div class="grid grid-cols-12 gap-4">
<div class="grid grid-cols-12 gap-4" id="header">
<div class="col-span-4">$company.name</div>
<div class="col-span-4" id="company-address"></div>
<div class="col-span-4">

View File

@ -32,7 +32,7 @@
<body class="$global-margin antialiased bg-white break-words">
<!-- Company logo, entity details -->
<div class="grid grid-cols-12 gap-4">
<div class="grid grid-cols-12 gap-4" id="header">
<div class="col-span-4">
<img
src="$company.logo"

View File

@ -50,6 +50,8 @@ class PdfMakerDesignsTest extends TestCase
],
],
];
exec('echo "" > storage/logs/laravel.log');
}
public function testBusiness()
@ -137,16 +139,13 @@ class PdfMakerDesignsTest extends TestCase
], $this->state['variables']),
];
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker($state);
$maker
->design(Business::class)
->build();
//exec('echo "" > storage/logs/laravel.log');
exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML());
@ -250,7 +249,6 @@ class PdfMakerDesignsTest extends TestCase
],
'variables' => array_merge([], $this->state['variables']),
];
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker($state);
@ -258,8 +256,6 @@ class PdfMakerDesignsTest extends TestCase
->design(Clean::class)
->build();
//exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
@ -366,7 +362,6 @@ class PdfMakerDesignsTest extends TestCase
],
'variables' => array_merge([], $this->state['variables']),
];
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker($state);
@ -374,6 +369,8 @@ class PdfMakerDesignsTest extends TestCase
->design(Modern::class)
->build();
info($maker->getCompiledHTML());
//exec('echo "" > storage/logs/laravel.log');
//info($maker->getCompiledHTML(true));
@ -381,7 +378,6 @@ class PdfMakerDesignsTest extends TestCase
$this->assertTrue(true);
}
public function testBold()
{
$state = [
@ -483,7 +479,6 @@ class PdfMakerDesignsTest extends TestCase
],
'variables' => array_merge([], $this->state['variables']),
];
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker($state);
@ -491,11 +486,7 @@ class PdfMakerDesignsTest extends TestCase
->design(Bold::class)
->build();
//exec('echo "" > storage/logs/laravel.log');
//info($maker->getCompiledHTML(true));
info($maker->getCompiledHTML());
$this->assertTrue(true);
}
@ -593,7 +584,6 @@ class PdfMakerDesignsTest extends TestCase
],
'variables' => array_merge([], $this->state['variables']),
];
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker($state);
@ -601,11 +591,9 @@ class PdfMakerDesignsTest extends TestCase
->design(Plain::class)
->build();
// exec('echo "" > storage/logs/laravel.log');
// info($maker->getCompiledHTML(true));
exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
@ -707,7 +695,6 @@ class PdfMakerDesignsTest extends TestCase
],
'variables' => array_merge([], $this->state['variables']),
];
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker($state);
@ -715,10 +702,7 @@ class PdfMakerDesignsTest extends TestCase
->design(Hipster::class)
->build();
// exec('echo "" > storage/logs/laravel.log');
// info($maker->getCompiledHTML(true));
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
@ -824,7 +808,6 @@ class PdfMakerDesignsTest extends TestCase
],
'variables' => array_merge([], $this->state['variables']),
];
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker($state);
@ -832,10 +815,7 @@ class PdfMakerDesignsTest extends TestCase
->design(Elegant::class)
->build();
// exec('echo "" > storage/logs/laravel.log');
// info($maker->getCompiledHTML(true));
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
@ -941,7 +921,6 @@ class PdfMakerDesignsTest extends TestCase
],
'variables' => array_merge([], $this->state['variables']),
];
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker($state);
@ -949,11 +928,8 @@ class PdfMakerDesignsTest extends TestCase
->design(Creative::class)
->build();
// exec('echo "" > storage/logs/laravel.log');
// info($maker->getCompiledHTML(true));
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
@ -1052,7 +1028,6 @@ class PdfMakerDesignsTest extends TestCase
],
'variables' => array_merge([], $this->state['variables']),
];
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker($state);
@ -1060,11 +1035,7 @@ class PdfMakerDesignsTest extends TestCase
->design(Playful::class)
->build();
// exec('echo "" > storage/logs/laravel.log');
// info($maker->getCompiledHTML(true));
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}

View File

@ -18,8 +18,6 @@ class PdfMakerTest extends TestCase
public function testDesignLoadsCorrectly()
{
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker($this->state);
$maker->design(ExampleDesign::class);
@ -29,8 +27,6 @@ class PdfMakerTest extends TestCase
public function testHtmlDesignLoadsCorrectly()
{
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker($this->state);
$maker
@ -42,8 +38,6 @@ class PdfMakerTest extends TestCase
public function testGetSectionUtility()
{
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker($this->state);
$maker
@ -55,8 +49,6 @@ class PdfMakerTest extends TestCase
public function testTableAttributesAreInjected()
{
$this->markTestSkipped('STUB broken tests');
$state = [
'template' => [
'product-table' => [
@ -93,8 +85,6 @@ class PdfMakerTest extends TestCase
public function testVariablesAreReplaced()
{
$this->markTestSkipped('STUB broken tests');
$state = [
'template' => [
@ -133,8 +123,6 @@ class PdfMakerTest extends TestCase
public function testElementContentIsGenerated()
{
$this->markTestSkipped('STUB broken tests');
$state = [
'template' => [
@ -184,8 +172,6 @@ class PdfMakerTest extends TestCase
public function testConditionalRenderingOfElements()
{
$this->markTestSkipped('STUB broken tests');
$maker1 = new PdfMaker([
'template' => [
@ -226,8 +212,6 @@ class PdfMakerTest extends TestCase
public function testOrderingElements()
{
$this->markTestSkipped('STUB broken tests');
$maker = new PdfMaker([
'template' => [
@ -286,8 +270,6 @@ class PdfMakerTest extends TestCase
public function testGeneratingPdf()
{
$this->markTestSkipped('STUB broken tests');
$state = [
'template' => [
@ -356,4 +338,39 @@ class PdfMakerTest extends TestCase
$this->assertStringContainsString('id="product-table"', $html);
}
public function testWrapperHTMLWorks()
{
$design = new ExampleDesign();
$state = [
'template' => [
'product-table' => [
'id' => 'product-table',
'elements' => [
['element' => 'p', 'content' => 'Example paragraph'],
],
],
],
'variables' => [
'labels' => [],
'values' => [],
],
'options' => [
'all_pages_footer' => true,
],
];
$maker = new PdfMaker($state);
$maker
->design(ExampleDesign::class)
->build();
exec('echo "" > storage/logs/laravel.log');
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
}

View File

@ -4,4 +4,5 @@
<body class="m-10">
<div id="header">$title</div>
<table id="product-table"></table>
<div id="footer">My awesome footer</div>
</body>