Generating PDFs using new templates

This commit is contained in:
Benjamin Beganović 2020-09-04 13:17:30 +02:00
parent 52dea9c665
commit 219666f765
8 changed files with 63 additions and 54 deletions

View File

@ -16,6 +16,7 @@ use App\Designs\Designer;
use App\Factory\InvoiceFactory;
use App\Jobs\Invoice\CreateInvoicePdf;
use App\Jobs\Util\PreviewPdf;
use App\Services\PdfMaker\Design;
use App\Services\PdfMaker\PdfMaker;
use App\Utils\HtmlEngine;
use App\Utils\Traits\MakesHash;
@ -112,13 +113,14 @@ class PreviewController extends BaseController
'variables' => $html->generateLabelsAndValues(),
];
$design = new Design(request()->design['name']);
$maker = new PdfMaker($state);
$maker
->design($design_namespace)
->design($design)
->build();
$file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(), auth()->user()->company());
$file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(true), auth()->user()->company());
return response()->download($file_path)->deleteFileAfterSend(true);
}
@ -171,16 +173,13 @@ class PreviewController extends BaseController
$html = new HtmlEngine(null, $invoice->invitations()->first(), 'invoice');
/** TODO: This request() does not contain design string - making it impossible to update its content. */
$design_namespace = 'App\Services\PdfMaker\Designs\\' . request()->design['name'];
$design_class = new $design_namespace();
$design = new Design(strtolower(request()->design['name']));
// $designer = new Designer($entity_obj, $design_object, $entity_obj->client->getSetting('pdf_variables'), lcfirst($entity));
// $html = $this->generateEntityHtml($designer, $entity_obj);
$state = [
'template' => $design_class->elements([
'template' => $design->elements([
'client' => $invoice->client,
'entity' => $invoice,
'pdf_variables' => (array) $invoice->company->settings->pdf_variables,
@ -191,12 +190,12 @@ class PreviewController extends BaseController
$maker = new PdfMaker($state);
$maker
->design($design_namespace)
->design($design)
->build();
$maker->getCompiledHTML();
info($maker->getCompiledHTML(true));
$file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(), auth()->user()->company());
$file_path = PreviewPdf::dispatchNow($maker->getCompiledHTML(true), auth()->user()->company());
DB::rollBack();

View File

@ -20,6 +20,7 @@ use App\Models\ClientContact;
use App\Models\Company;
use App\Models\Design;
use App\Models\Invoice;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
use App\Utils\HtmlEngine;
use App\Utils\PhantomJS\Phantom;
@ -86,12 +87,10 @@ class CreateInvoicePdf implements ShouldQueue
$html = new HtmlEngine(null, $this->invitation, 'invoice');
$design_namespace = 'App\Services\PdfMaker\Designs\\' . $design->name;
$design_class = new $design_namespace();
$template = new PdfMakerDesign(strtolower($design->name));
$state = [
'template' => $design_class->elements([
'template' => $template->elements([
'client' => $this->invoice->client,
'entity' => $this->invoice,
'pdf_variables' => (array)$this->invoice->company->settings->pdf_variables,
@ -106,15 +105,15 @@ class CreateInvoicePdf implements ShouldQueue
$maker = new PdfMakerService($state);
$maker
->design($design_namespace)
->design($template)
->build();
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
Storage::makeDirectory($path, 0775);
info($maker->getCompiledHTML());
info($maker->getCompiledHTML(true));
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML());
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
$instance = Storage::disk($this->disk)->put($file_path, $pdf);

View File

@ -12,9 +12,10 @@
namespace App\Services\PdfMaker;
use Illuminate\Support\Str;
use App\Utils\Traits\MakesInvoiceValues;
use App\Services\PdfMaker\Designs\Utilities\BaseDesign;
use App\Services\PdfMaker\Designs\Utilities\DesignHelpers;
use App\Utils\Traits\MakesInvoiceValues;
class Design extends BaseDesign
{
@ -35,19 +36,19 @@ class Design extends BaseDesign
/** Construct options */
public $options;
const BOLD = 'bold.html';
const BUSINESS = 'business.html';
const CLEAN = 'clean.html';
const CREATIVE = 'creative.html';
const ELEGANT = 'elegant.html';
const HIPSTER = 'hipster.html';
const MODERN = 'modern.html';
const PLAIN = 'plain.html';
const PLAYFUL = 'playful.html';
const BOLD = 'bold';
const BUSINESS = 'business';
const CLEAN = 'clean';
const CREATIVE = 'creative';
const ELEGANT = 'elegant';
const HIPSTER = 'hipster';
const MODERN = 'modern';
const PLAIN = 'plain';
const PLAYFUL = 'playful';
public function __construct(string $design = null, array $options = [])
{
$this->design = $design;
Str::endsWith('.html', $design) ? $this->design = $design : $this->design = "{$design}.html";
$this->options = $options;
}
@ -216,8 +217,13 @@ class Design extends BaseDesign
];
foreach ($variables as $variable) {
['element' => 'tr', 'properties' => ['hidden' => 'false'], 'elements' => [
['element' => 'td', 'content' => $variable . '_label', 'properties' => ['colspan' => $this->calculateColspan(1)]],
if ($variable == '$total_taxes' || $variable == '$line_taxes') {
continue;
}
$elements[] = ['element' => 'tr', 'elements' => [
['element' => 'td', 'properties' => ['colspan' => $this->calculateColspan(2)]],
['element' => 'td', 'content' => $variable . '_label'],
['element' => 'td', 'content' => $variable],
]];
}

View File

@ -28,6 +28,8 @@ class PdfMaker
private $filters = [
'<![CDATA[' => '',
'<![CDATA[<![CDATA[' => '',
']]]]><![CDATA[>]]>' => '',
']]>' => '',
'<?xml version="1.0" encoding="utf-8" standalone="yes"??>' => '',
];

View File

@ -483,7 +483,8 @@ class HtmlEngine
}
foreach ($this->entity_calc->getTotalTaxMap() as $tax) {
$data .= '<tr class="total_taxes">';
$data .= '<tr>';
$data .= '<td colspan="{ count($this->entity->company->settings->pdf_variables->total_columns) - 2 }"></td>';
$data .= '<td>'. $tax['name'] .'</td>';
$data .= '<td>'. Number::formatMoney($tax['total'], $this->client) .'</td></tr>';
}

View File

@ -2,6 +2,7 @@
use App\Models\Bank;
use App\Models\Design;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use Illuminate\Database\Seeder;
class DesignSeeder extends Seeder
@ -36,17 +37,16 @@ class DesignSeeder extends Seeder
}
foreach (Design::all() as $design) {
$class = 'App\Services\PdfMaker\Designs\\'.$design->name;
$invoice_design = new $class();
$invoice_design->document();
$template = new PdfMakerDesign(strtolower($design->name));
$template->document();
$design_object = new \stdClass;
$design_object->includes = $invoice_design->getSectionHTML('includes');
$design_object->header = $invoice_design->getSectionHTML('head', false);
$design_object->body = $invoice_design->getSectionHTML('body', false);
$design_object->product = $invoice_design->getSectionHTML('product-table');
$design_object->task = $invoice_design->getSectionHTML('task-table');
$design_object->footer = $invoice_design->getSectionHTML('footer', false);
$design_object->includes = $template->getSectionHTML('includes');
$design_object->header = $template->getSectionHTML('head', false);
$design_object->body = $template->getSectionHTML('body', false);
$design_object->product = $template->getSectionHTML('product-table');
$design_object->task = $template->getSectionHTML('task-table');
$design_object->footer = $template->getSectionHTML('footer', false);
$design->design = $design_object;
$design->save();

View File

@ -24,6 +24,8 @@ class ExampleIntegrationTest extends TestCase
public function testExample()
{
$this->markTestIncomplete();
$invoice = $this->invoice;
$invitation = $invoice->invitations()->first();
@ -48,9 +50,9 @@ class ExampleIntegrationTest extends TestCase
->design($design)
->build();
// exec('echo "" > storage/logs/laravel.log');
exec('echo "" > storage/logs/laravel.log');
// info($maker->getCompiledHTML(true));
info($maker->getCompiledHTML(true));
$this->assertTrue(true);
}

View File

@ -18,7 +18,7 @@ class PdfMakerTest extends TestCase
public function testDesignLoadsCorrectly()
{
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($this->state);
$maker->design($design);
@ -28,7 +28,7 @@ class PdfMakerTest extends TestCase
public function testHtmlDesignLoadsCorrectly()
{
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($this->state);
@ -41,7 +41,7 @@ class PdfMakerTest extends TestCase
public function testGetSectionUtility()
{
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($this->state);
@ -71,7 +71,7 @@ class PdfMakerTest extends TestCase
],
];
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($state);
$maker
@ -99,7 +99,7 @@ class PdfMakerTest extends TestCase
],
];
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($state);
$maker
@ -147,7 +147,7 @@ class PdfMakerTest extends TestCase
],
];
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($state);
$maker
@ -161,7 +161,7 @@ class PdfMakerTest extends TestCase
public function testConditionalRenderingOfElements()
{
$design1 = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$design1 = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker1 = new PdfMaker([
'template' => [
@ -180,7 +180,7 @@ class PdfMakerTest extends TestCase
$this->assertStringContainsString('<div id="header">', $output1);
$design2 = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$design2 = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker2 = new PdfMaker([
'template' => [
'header' => [
@ -203,7 +203,7 @@ class PdfMakerTest extends TestCase
public function testOrderingElements()
{
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker([
'template' => [
@ -310,7 +310,7 @@ class PdfMakerTest extends TestCase
]
];
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($state);
$maker
@ -322,7 +322,7 @@ class PdfMakerTest extends TestCase
public function testGetSectionHTMLWorks()
{
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$html = $design
->document()
@ -333,7 +333,7 @@ class PdfMakerTest extends TestCase
public function testWrapperHTMLWorks()
{
$design = new Design('example.html', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$state = [
'template' => [