WOrking on merging twig inside of custom designs

This commit is contained in:
David Bomba 2023-10-24 16:00:13 +11:00
parent 9294e2c347
commit 89e58b7693
4 changed files with 125 additions and 73 deletions

View File

@ -23,6 +23,7 @@ use App\Models\QuoteInvitation;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use App\Models\CreditInvitation; use App\Models\CreditInvitation;
use App\Models\RecurringInvoice; use App\Models\RecurringInvoice;
use App\Services\Pdf\PdfService;
use App\Utils\PhantomJS\Phantom; use App\Utils\PhantomJS\Phantom;
use App\Models\InvoiceInvitation; use App\Models\InvoiceInvitation;
use App\Utils\HostedPDF\NinjaPdf; use App\Utils\HostedPDF\NinjaPdf;
@ -123,87 +124,96 @@ class CreateRawPdf implements ShouldQueue
$file_path = $path.$this->entity->numberFormatter().'.pdf'; $file_path = $path.$this->entity->numberFormatter().'.pdf';
$entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->entity->client->getSetting($entity_design_id));
$design = Design::query()->withTrashed()->find($entity_design_id); $ps = new PdfService($this->invitation, 'product', [
'client' => $this->entity->client,
"{$this->entity_string}s" => [$this->entity],
]);
/* Catch all in case migration doesn't pass back a valid design */ $pdf = $ps->boot()->getPdf();
if (! $design) {
$design = Design::query()->find(2);
}
$html = new HtmlEngine($this->invitation);
if ($design->is_custom) { // $entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->entity->client->getSetting($entity_design_id));
$options = [
'custom_partials' => json_decode(json_encode($design->design), true),
];
$template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options);
} else {
$template = new PdfMakerDesign(strtolower($design->name));
}
$variables = $html->generateLabelsAndValues(); // $design = Design::query()->withTrashed()->find($entity_design_id);
$state = [ // /* Catch all in case migration doesn't pass back a valid design */
'template' => $template->elements([ // if (! $design) {
'client' => $this->entity->client, // $design = Design::query()->find(2);
'entity' => $this->entity, // }
'pdf_variables' => (array) $this->entity->company->settings->pdf_variables,
'$product' => $design->design->product,
'variables' => $variables,
]),
'variables' => $variables,
'options' => [
'all_pages_header' => $this->entity->client->getSetting('all_pages_header'),
'all_pages_footer' => $this->entity->client->getSetting('all_pages_footer'),
'client' => $this->entity->client,
'entity' => $this->entity,
'variables' => $variables,
],
'process_markdown' => $this->entity->client->company->markdown_enabled,
];
$maker = new PdfMakerService($state); // $html = new HtmlEngine($this->invitation);
$maker // if ($design->is_custom) {
->design($template) // $options = [
->build(); // 'custom_partials' => json_decode(json_encode($design->design), true),
// ];
// $template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options);
// } else {
// $template = new PdfMakerDesign(strtolower($design->name));
// }
$pdf = null; // $variables = $html->generateLabelsAndValues();
try { // $state = [
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') { // 'template' => $template->elements([
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true)); // 'client' => $this->entity->client,
// 'entity' => $this->entity,
// 'pdf_variables' => (array) $this->entity->company->settings->pdf_variables,
// '$product' => $design->design->product,
// 'variables' => $variables,
// ]),
// 'variables' => $variables,
// 'options' => [
// 'all_pages_header' => $this->entity->client->getSetting('all_pages_header'),
// 'all_pages_footer' => $this->entity->client->getSetting('all_pages_footer'),
// 'client' => $this->entity->client,
// 'entity' => $this->entity,
// 'variables' => $variables,
// ],
// 'process_markdown' => $this->entity->client->company->markdown_enabled,
// ];
$finfo = new \finfo(FILEINFO_MIME); // $maker = new PdfMakerService($state);
//fallback in case hosted PDF fails. // $maker
if ($finfo->buffer($pdf) != 'application/pdf; charset=binary') { // ->design($template)
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true)); // ->build();
$numbered_pdf = $this->pageNumbering($pdf, $this->company); // $pdf = null;
if ($numbered_pdf) { // try {
$pdf = $numbered_pdf; // if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
} // $pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
}
} else {
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
$numbered_pdf = $this->pageNumbering($pdf, $this->company); // $finfo = new \finfo(FILEINFO_MIME);
if ($numbered_pdf) { // //fallback in case hosted PDF fails.
$pdf = $numbered_pdf; // if ($finfo->buffer($pdf) != 'application/pdf; charset=binary') {
} // $pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
}
} catch (\Exception $e) {
nlog(print_r($e->getMessage(), 1));
}
if (config('ninja.log_pdf_html')) { // $numbered_pdf = $this->pageNumbering($pdf, $this->company);
info($maker->getCompiledHTML());
} // if ($numbered_pdf) {
// $pdf = $numbered_pdf;
// }
// }
// } else {
// $pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
// $numbered_pdf = $this->pageNumbering($pdf, $this->company);
// if ($numbered_pdf) {
// $pdf = $numbered_pdf;
// }
// }
// } catch (\Exception $e) {
// nlog(print_r($e->getMessage(), 1));
// }
// if (config('ninja.log_pdf_html')) {
// info($maker->getCompiledHTML());
// }
if ($pdf) { if ($pdf) {
$maker =null; $maker =null;

View File

@ -11,13 +11,14 @@
namespace App\Services\Pdf; namespace App\Services\Pdf;
use App\Models\Credit;
use App\Models\Quote;
use App\Utils\Helpers;
use App\Utils\Traits\MakesDates;
use DOMDocument; use DOMDocument;
use Illuminate\Support\Carbon; use App\Models\Quote;
use App\Models\Credit;
use App\Utils\Helpers;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use App\Utils\Traits\MakesDates;
use App\Services\Template\TemplateService;
use League\CommonMark\CommonMarkConverter; use League\CommonMark\CommonMarkConverter;
class PdfBuilder class PdfBuilder
@ -67,6 +68,7 @@ class PdfBuilder
->buildSections() ->buildSections()
->getEmptyElements() ->getEmptyElements()
->updateElementProperties() ->updateElementProperties()
->parseTwigElements()
->updateVariables(); ->updateVariables();
return $this; return $this;
@ -104,6 +106,40 @@ class PdfBuilder
return $this; return $this;
} }
private function parseTwigElements()
{
$replacements = [];
$contents = $this->document->getElementsByTagName('ninja');
$template_service = new TemplateService();
$data = $template_service->processData($this->service->options)->getData();
$twig = $template_service->twig;
foreach ($contents as $content) {
$template = $content->ownerDocument->saveHTML($content);
$template = $twig->createTemplate(html_entity_decode($template));
$template = $template->render($data);
$f = $this->document->createDocumentFragment();
$f->appendXML($template);
$replacements[] = $f;
}
foreach($contents as $key => $content) {
$content->parentNode->replaceChild($replacements[$key], $content);
}
$contents = null;
return $this;
}
public function setDocument($document): self public function setDocument($document): self
{ {
$this->document = $document; $this->document = $document;
@ -1091,7 +1127,8 @@ class PdfBuilder
} elseif (Str::startsWith($variable, '$custom_surcharge')) { } elseif (Str::startsWith($variable, '$custom_surcharge')) {
$_variable = ltrim($variable, '$'); // $custom_surcharge1 -> custom_surcharge1 $_variable = ltrim($variable, '$'); // $custom_surcharge1 -> custom_surcharge1
$visible = intval($this->service->config->entity->{$_variable}) != 0; // $visible = intval($this->service->config->entity->{$_variable}) != 0;
$visible = intval(str_replace(['0','.'], '', $this->service->config->entity->{$_variable})) != 0;
$elements[1]['elements'][] = ['element' => 'div', 'elements' => [ $elements[1]['elements'][] = ['element' => 'div', 'elements' => [
['element' => 'span', 'content' => $variable . '_label', 'properties' => ['hidden' => !$visible, 'data-ref' => 'totals_table-' . substr($variable, 1) . '-label']], ['element' => 'span', 'content' => $variable . '_label', 'properties' => ['hidden' => !$visible, 'data-ref' => 'totals_table-' . substr($variable, 1) . '-label']],

View File

@ -61,7 +61,7 @@ class PdfService
} }
public function boot(): self public function boot(): self
{ {nlog("booties");
$this->init(); $this->init();
return $this; return $this;
@ -104,7 +104,7 @@ class PdfService
$html = $this->builder->getCompiledHTML(); $html = $this->builder->getCompiledHTML();
if (config('ninja.log_pdf_html')) { if (config('ninja.log_pdf_html')) {
info($html); nlog($html);
} }
return $html; return $html;

View File

@ -156,7 +156,12 @@ class TemplateService
} }
private function processData($data): self public function getData(): array
{
return $this->data;
}
public function processData($data): self
{ {
$this->data = $this->preProcessDataBlocks($data); $this->data = $this->preProcessDataBlocks($data);