mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
WOrking on merging twig inside of custom designs
This commit is contained in:
parent
9294e2c347
commit
89e58b7693
@ -23,6 +23,7 @@ use App\Models\QuoteInvitation;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Models\CreditInvitation;
|
||||
use App\Models\RecurringInvoice;
|
||||
use App\Services\Pdf\PdfService;
|
||||
use App\Utils\PhantomJS\Phantom;
|
||||
use App\Models\InvoiceInvitation;
|
||||
use App\Utils\HostedPDF\NinjaPdf;
|
||||
@ -123,87 +124,96 @@ class CreateRawPdf implements ShouldQueue
|
||||
|
||||
$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 */
|
||||
if (! $design) {
|
||||
$design = Design::query()->find(2);
|
||||
}
|
||||
$pdf = $ps->boot()->getPdf();
|
||||
|
||||
$html = new HtmlEngine($this->invitation);
|
||||
|
||||
if ($design->is_custom) {
|
||||
$options = [
|
||||
'custom_partials' => json_decode(json_encode($design->design), true),
|
||||
];
|
||||
$template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options);
|
||||
} else {
|
||||
$template = new PdfMakerDesign(strtolower($design->name));
|
||||
}
|
||||
// $entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->entity->client->getSetting($entity_design_id));
|
||||
|
||||
$variables = $html->generateLabelsAndValues();
|
||||
// $design = Design::query()->withTrashed()->find($entity_design_id);
|
||||
|
||||
$state = [
|
||||
'template' => $template->elements([
|
||||
'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,
|
||||
];
|
||||
// /* Catch all in case migration doesn't pass back a valid design */
|
||||
// if (! $design) {
|
||||
// $design = Design::query()->find(2);
|
||||
// }
|
||||
|
||||
$maker = new PdfMakerService($state);
|
||||
// $html = new HtmlEngine($this->invitation);
|
||||
|
||||
$maker
|
||||
->design($template)
|
||||
->build();
|
||||
// if ($design->is_custom) {
|
||||
// $options = [
|
||||
// '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 {
|
||||
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
|
||||
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
|
||||
// $state = [
|
||||
// 'template' => $template->elements([
|
||||
// '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.
|
||||
if ($finfo->buffer($pdf) != 'application/pdf; charset=binary') {
|
||||
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
|
||||
// $maker
|
||||
// ->design($template)
|
||||
// ->build();
|
||||
|
||||
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
|
||||
// $pdf = null;
|
||||
|
||||
if ($numbered_pdf) {
|
||||
$pdf = $numbered_pdf;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
|
||||
// try {
|
||||
// if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
|
||||
// $pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
|
||||
|
||||
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
|
||||
// $finfo = new \finfo(FILEINFO_MIME);
|
||||
|
||||
if ($numbered_pdf) {
|
||||
$pdf = $numbered_pdf;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
nlog(print_r($e->getMessage(), 1));
|
||||
}
|
||||
// //fallback in case hosted PDF fails.
|
||||
// if ($finfo->buffer($pdf) != 'application/pdf; charset=binary') {
|
||||
// $pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
|
||||
|
||||
if (config('ninja.log_pdf_html')) {
|
||||
info($maker->getCompiledHTML());
|
||||
}
|
||||
// $numbered_pdf = $this->pageNumbering($pdf, $this->company);
|
||||
|
||||
// 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) {
|
||||
$maker =null;
|
||||
|
@ -11,13 +11,14 @@
|
||||
|
||||
namespace App\Services\Pdf;
|
||||
|
||||
use App\Models\Credit;
|
||||
use App\Models\Quote;
|
||||
use App\Utils\Helpers;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
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\Carbon;
|
||||
use App\Utils\Traits\MakesDates;
|
||||
use App\Services\Template\TemplateService;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
|
||||
class PdfBuilder
|
||||
@ -67,6 +68,7 @@ class PdfBuilder
|
||||
->buildSections()
|
||||
->getEmptyElements()
|
||||
->updateElementProperties()
|
||||
->parseTwigElements()
|
||||
->updateVariables();
|
||||
|
||||
return $this;
|
||||
@ -104,6 +106,40 @@ class PdfBuilder
|
||||
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
|
||||
{
|
||||
$this->document = $document;
|
||||
@ -1091,7 +1127,8 @@ class PdfBuilder
|
||||
} elseif (Str::startsWith($variable, '$custom_surcharge')) {
|
||||
$_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' => [
|
||||
['element' => 'span', 'content' => $variable . '_label', 'properties' => ['hidden' => !$visible, 'data-ref' => 'totals_table-' . substr($variable, 1) . '-label']],
|
||||
|
@ -61,7 +61,7 @@ class PdfService
|
||||
}
|
||||
|
||||
public function boot(): self
|
||||
{
|
||||
{nlog("booties");
|
||||
$this->init();
|
||||
|
||||
return $this;
|
||||
@ -104,7 +104,7 @@ class PdfService
|
||||
$html = $this->builder->getCompiledHTML();
|
||||
|
||||
if (config('ninja.log_pdf_html')) {
|
||||
info($html);
|
||||
nlog($html);
|
||||
}
|
||||
|
||||
return $html;
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user