mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-03 06:14:36 -04:00
Generating PDFs using new templates
This commit is contained in:
parent
52dea9c665
commit
219666f765
@ -16,6 +16,7 @@ use App\Designs\Designer;
|
|||||||
use App\Factory\InvoiceFactory;
|
use App\Factory\InvoiceFactory;
|
||||||
use App\Jobs\Invoice\CreateInvoicePdf;
|
use App\Jobs\Invoice\CreateInvoicePdf;
|
||||||
use App\Jobs\Util\PreviewPdf;
|
use App\Jobs\Util\PreviewPdf;
|
||||||
|
use App\Services\PdfMaker\Design;
|
||||||
use App\Services\PdfMaker\PdfMaker;
|
use App\Services\PdfMaker\PdfMaker;
|
||||||
use App\Utils\HtmlEngine;
|
use App\Utils\HtmlEngine;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
@ -112,13 +113,14 @@ class PreviewController extends BaseController
|
|||||||
'variables' => $html->generateLabelsAndValues(),
|
'variables' => $html->generateLabelsAndValues(),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
$design = new Design(request()->design['name']);
|
||||||
$maker = new PdfMaker($state);
|
$maker = new PdfMaker($state);
|
||||||
|
|
||||||
$maker
|
$maker
|
||||||
->design($design_namespace)
|
->design($design)
|
||||||
->build();
|
->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);
|
return response()->download($file_path)->deleteFileAfterSend(true);
|
||||||
}
|
}
|
||||||
@ -171,16 +173,13 @@ class PreviewController extends BaseController
|
|||||||
|
|
||||||
$html = new HtmlEngine(null, $invoice->invitations()->first(), 'invoice');
|
$html = new HtmlEngine(null, $invoice->invitations()->first(), 'invoice');
|
||||||
|
|
||||||
/** TODO: This request() does not contain design string - making it impossible to update its content. */
|
$design = new Design(strtolower(request()->design['name']));
|
||||||
$design_namespace = 'App\Services\PdfMaker\Designs\\' . request()->design['name'];
|
|
||||||
|
|
||||||
$design_class = new $design_namespace();
|
|
||||||
|
|
||||||
// $designer = new Designer($entity_obj, $design_object, $entity_obj->client->getSetting('pdf_variables'), lcfirst($entity));
|
// $designer = new Designer($entity_obj, $design_object, $entity_obj->client->getSetting('pdf_variables'), lcfirst($entity));
|
||||||
// $html = $this->generateEntityHtml($designer, $entity_obj);
|
// $html = $this->generateEntityHtml($designer, $entity_obj);
|
||||||
|
|
||||||
$state = [
|
$state = [
|
||||||
'template' => $design_class->elements([
|
'template' => $design->elements([
|
||||||
'client' => $invoice->client,
|
'client' => $invoice->client,
|
||||||
'entity' => $invoice,
|
'entity' => $invoice,
|
||||||
'pdf_variables' => (array) $invoice->company->settings->pdf_variables,
|
'pdf_variables' => (array) $invoice->company->settings->pdf_variables,
|
||||||
@ -191,12 +190,12 @@ class PreviewController extends BaseController
|
|||||||
$maker = new PdfMaker($state);
|
$maker = new PdfMaker($state);
|
||||||
|
|
||||||
$maker
|
$maker
|
||||||
->design($design_namespace)
|
->design($design)
|
||||||
->build();
|
->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();
|
DB::rollBack();
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ use App\Models\ClientContact;
|
|||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
use App\Models\Design;
|
use App\Models\Design;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
|
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
||||||
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
|
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
|
||||||
use App\Utils\HtmlEngine;
|
use App\Utils\HtmlEngine;
|
||||||
use App\Utils\PhantomJS\Phantom;
|
use App\Utils\PhantomJS\Phantom;
|
||||||
@ -86,12 +87,10 @@ class CreateInvoicePdf implements ShouldQueue
|
|||||||
|
|
||||||
$html = new HtmlEngine(null, $this->invitation, 'invoice');
|
$html = new HtmlEngine(null, $this->invitation, 'invoice');
|
||||||
|
|
||||||
$design_namespace = 'App\Services\PdfMaker\Designs\\' . $design->name;
|
$template = new PdfMakerDesign(strtolower($design->name));
|
||||||
|
|
||||||
$design_class = new $design_namespace();
|
|
||||||
|
|
||||||
$state = [
|
$state = [
|
||||||
'template' => $design_class->elements([
|
'template' => $template->elements([
|
||||||
'client' => $this->invoice->client,
|
'client' => $this->invoice->client,
|
||||||
'entity' => $this->invoice,
|
'entity' => $this->invoice,
|
||||||
'pdf_variables' => (array)$this->invoice->company->settings->pdf_variables,
|
'pdf_variables' => (array)$this->invoice->company->settings->pdf_variables,
|
||||||
@ -106,15 +105,15 @@ class CreateInvoicePdf implements ShouldQueue
|
|||||||
$maker = new PdfMakerService($state);
|
$maker = new PdfMakerService($state);
|
||||||
|
|
||||||
$maker
|
$maker
|
||||||
->design($design_namespace)
|
->design($template)
|
||||||
->build();
|
->build();
|
||||||
|
|
||||||
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
|
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
|
||||||
Storage::makeDirectory($path, 0775);
|
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);
|
$instance = Storage::disk($this->disk)->put($file_path, $pdf);
|
||||||
|
|
||||||
|
@ -12,9 +12,10 @@
|
|||||||
|
|
||||||
namespace App\Services\PdfMaker;
|
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\BaseDesign;
|
||||||
use App\Services\PdfMaker\Designs\Utilities\DesignHelpers;
|
use App\Services\PdfMaker\Designs\Utilities\DesignHelpers;
|
||||||
use App\Utils\Traits\MakesInvoiceValues;
|
|
||||||
|
|
||||||
class Design extends BaseDesign
|
class Design extends BaseDesign
|
||||||
{
|
{
|
||||||
@ -35,19 +36,19 @@ class Design extends BaseDesign
|
|||||||
/** Construct options */
|
/** Construct options */
|
||||||
public $options;
|
public $options;
|
||||||
|
|
||||||
const BOLD = 'bold.html';
|
const BOLD = 'bold';
|
||||||
const BUSINESS = 'business.html';
|
const BUSINESS = 'business';
|
||||||
const CLEAN = 'clean.html';
|
const CLEAN = 'clean';
|
||||||
const CREATIVE = 'creative.html';
|
const CREATIVE = 'creative';
|
||||||
const ELEGANT = 'elegant.html';
|
const ELEGANT = 'elegant';
|
||||||
const HIPSTER = 'hipster.html';
|
const HIPSTER = 'hipster';
|
||||||
const MODERN = 'modern.html';
|
const MODERN = 'modern';
|
||||||
const PLAIN = 'plain.html';
|
const PLAIN = 'plain';
|
||||||
const PLAYFUL = 'playful.html';
|
const PLAYFUL = 'playful';
|
||||||
|
|
||||||
public function __construct(string $design = null, array $options = [])
|
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;
|
$this->options = $options;
|
||||||
}
|
}
|
||||||
@ -216,8 +217,13 @@ class Design extends BaseDesign
|
|||||||
];
|
];
|
||||||
|
|
||||||
foreach ($variables as $variable) {
|
foreach ($variables as $variable) {
|
||||||
['element' => 'tr', 'properties' => ['hidden' => 'false'], 'elements' => [
|
if ($variable == '$total_taxes' || $variable == '$line_taxes') {
|
||||||
['element' => 'td', 'content' => $variable . '_label', 'properties' => ['colspan' => $this->calculateColspan(1)]],
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$elements[] = ['element' => 'tr', 'elements' => [
|
||||||
|
['element' => 'td', 'properties' => ['colspan' => $this->calculateColspan(2)]],
|
||||||
|
['element' => 'td', 'content' => $variable . '_label'],
|
||||||
['element' => 'td', 'content' => $variable],
|
['element' => 'td', 'content' => $variable],
|
||||||
]];
|
]];
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,8 @@ class PdfMaker
|
|||||||
|
|
||||||
private $filters = [
|
private $filters = [
|
||||||
'<![CDATA[' => '',
|
'<![CDATA[' => '',
|
||||||
|
'<![CDATA[<![CDATA[' => '',
|
||||||
|
']]]]><![CDATA[>]]>' => '',
|
||||||
']]>' => '',
|
']]>' => '',
|
||||||
'<?xml version="1.0" encoding="utf-8" standalone="yes"??>' => '',
|
'<?xml version="1.0" encoding="utf-8" standalone="yes"??>' => '',
|
||||||
];
|
];
|
||||||
|
@ -483,7 +483,8 @@ class HtmlEngine
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->entity_calc->getTotalTaxMap() as $tax) {
|
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>'. $tax['name'] .'</td>';
|
||||||
$data .= '<td>'. Number::formatMoney($tax['total'], $this->client) .'</td></tr>';
|
$data .= '<td>'. Number::formatMoney($tax['total'], $this->client) .'</td></tr>';
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use App\Models\Bank;
|
use App\Models\Bank;
|
||||||
use App\Models\Design;
|
use App\Models\Design;
|
||||||
|
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
class DesignSeeder extends Seeder
|
class DesignSeeder extends Seeder
|
||||||
@ -36,17 +37,16 @@ class DesignSeeder extends Seeder
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach (Design::all() as $design) {
|
foreach (Design::all() as $design) {
|
||||||
$class = 'App\Services\PdfMaker\Designs\\'.$design->name;
|
$template = new PdfMakerDesign(strtolower($design->name));
|
||||||
$invoice_design = new $class();
|
$template->document();
|
||||||
$invoice_design->document();
|
|
||||||
|
|
||||||
$design_object = new \stdClass;
|
$design_object = new \stdClass;
|
||||||
$design_object->includes = $invoice_design->getSectionHTML('includes');
|
$design_object->includes = $template->getSectionHTML('includes');
|
||||||
$design_object->header = $invoice_design->getSectionHTML('head', false);
|
$design_object->header = $template->getSectionHTML('head', false);
|
||||||
$design_object->body = $invoice_design->getSectionHTML('body', false);
|
$design_object->body = $template->getSectionHTML('body', false);
|
||||||
$design_object->product = $invoice_design->getSectionHTML('product-table');
|
$design_object->product = $template->getSectionHTML('product-table');
|
||||||
$design_object->task = $invoice_design->getSectionHTML('task-table');
|
$design_object->task = $template->getSectionHTML('task-table');
|
||||||
$design_object->footer = $invoice_design->getSectionHTML('footer', false);
|
$design_object->footer = $template->getSectionHTML('footer', false);
|
||||||
|
|
||||||
$design->design = $design_object;
|
$design->design = $design_object;
|
||||||
$design->save();
|
$design->save();
|
||||||
|
@ -24,6 +24,8 @@ class ExampleIntegrationTest extends TestCase
|
|||||||
|
|
||||||
public function testExample()
|
public function testExample()
|
||||||
{
|
{
|
||||||
|
$this->markTestIncomplete();
|
||||||
|
|
||||||
$invoice = $this->invoice;
|
$invoice = $this->invoice;
|
||||||
$invitation = $invoice->invitations()->first();
|
$invitation = $invoice->invitations()->first();
|
||||||
|
|
||||||
@ -48,9 +50,9 @@ class ExampleIntegrationTest extends TestCase
|
|||||||
->design($design)
|
->design($design)
|
||||||
->build();
|
->build();
|
||||||
|
|
||||||
// exec('echo "" > storage/logs/laravel.log');
|
exec('echo "" > storage/logs/laravel.log');
|
||||||
|
|
||||||
// info($maker->getCompiledHTML(true));
|
info($maker->getCompiledHTML(true));
|
||||||
|
|
||||||
$this->assertTrue(true);
|
$this->assertTrue(true);
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ class PdfMakerTest extends TestCase
|
|||||||
|
|
||||||
public function testDesignLoadsCorrectly()
|
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 = new PdfMaker($this->state);
|
||||||
|
|
||||||
$maker->design($design);
|
$maker->design($design);
|
||||||
@ -28,7 +28,7 @@ class PdfMakerTest extends TestCase
|
|||||||
|
|
||||||
public function testHtmlDesignLoadsCorrectly()
|
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);
|
$maker = new PdfMaker($this->state);
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ class PdfMakerTest extends TestCase
|
|||||||
|
|
||||||
public function testGetSectionUtility()
|
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);
|
$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 = new PdfMaker($state);
|
||||||
|
|
||||||
$maker
|
$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 = new PdfMaker($state);
|
||||||
|
|
||||||
$maker
|
$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 = new PdfMaker($state);
|
||||||
|
|
||||||
$maker
|
$maker
|
||||||
@ -161,7 +161,7 @@ class PdfMakerTest extends TestCase
|
|||||||
|
|
||||||
public function testConditionalRenderingOfElements()
|
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([
|
$maker1 = new PdfMaker([
|
||||||
'template' => [
|
'template' => [
|
||||||
@ -180,7 +180,7 @@ class PdfMakerTest extends TestCase
|
|||||||
|
|
||||||
$this->assertStringContainsString('<div id="header">', $output1);
|
$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([
|
$maker2 = new PdfMaker([
|
||||||
'template' => [
|
'template' => [
|
||||||
'header' => [
|
'header' => [
|
||||||
@ -203,7 +203,7 @@ class PdfMakerTest extends TestCase
|
|||||||
|
|
||||||
public function testOrderingElements()
|
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([
|
$maker = new PdfMaker([
|
||||||
'template' => [
|
'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 = new PdfMaker($state);
|
||||||
|
|
||||||
$maker
|
$maker
|
||||||
@ -322,7 +322,7 @@ class PdfMakerTest extends TestCase
|
|||||||
|
|
||||||
public function testGetSectionHTMLWorks()
|
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
|
$html = $design
|
||||||
->document()
|
->document()
|
||||||
@ -333,7 +333,7 @@ class PdfMakerTest extends TestCase
|
|||||||
|
|
||||||
public function testWrapperHTMLWorks()
|
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 = [
|
$state = [
|
||||||
'template' => [
|
'template' => [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user