Improvements for twig

This commit is contained in:
David Bomba 2023-10-24 16:26:16 +11:00
parent 89e58b7693
commit d5339d0864
2 changed files with 86 additions and 74 deletions

View File

@ -90,6 +90,17 @@ class CreateRawPdf implements ShouldQueue
public function handle()
{
/** Testing this override to improve PDF generation performance */
$ps = new PdfService($this->invitation, 'product', [
'client' => $this->entity->client,
"{$this->entity_string}s" => [$this->entity],
]);
$pdf = $ps->boot()->getPdf();
nlog("pdf timer = ". $ps->execution_time);
/* Forget the singleton*/
App::forgetInstance('translator');
@ -124,96 +135,87 @@ 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));
$ps = new PdfService($this->invitation, 'product', [
$design = Design::query()->withTrashed()->find($entity_design_id);
/* Catch all in case migration doesn't pass back a valid design */
if (! $design) {
$design = Design::query()->find(2);
}
$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));
}
$variables = $html->generateLabelsAndValues();
$state = [
'template' => $template->elements([
'client' => $this->entity->client,
"{$this->entity_string}s" => [$this->entity],
]);
'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,
];
$pdf = $ps->boot()->getPdf();
$maker = new PdfMakerService($state);
$maker
->design($template)
->build();
// $entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->entity->client->getSetting($entity_design_id));
$pdf = null;
// $design = Design::query()->withTrashed()->find($entity_design_id);
try {
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
// /* Catch all in case migration doesn't pass back a valid design */
// if (! $design) {
// $design = Design::query()->find(2);
// }
$finfo = new \finfo(FILEINFO_MIME);
// $html = new HtmlEngine($this->invitation);
//fallback in case hosted PDF fails.
if ($finfo->buffer($pdf) != 'application/pdf; charset=binary') {
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
// 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));
// }
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
// $variables = $html->generateLabelsAndValues();
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
}
} else {
$pdf = $this->makePdf(null, null, $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,
// ];
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
// $maker = new PdfMakerService($state);
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
}
} catch (\Exception $e) {
nlog(print_r($e->getMessage(), 1));
}
// $maker
// ->design($template)
// ->build();
// $pdf = null;
// try {
// if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
// $pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
// $finfo = new \finfo(FILEINFO_MIME);
// //fallback in case hosted PDF fails.
// if ($finfo->buffer($pdf) != 'application/pdf; charset=binary') {
// $pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
// $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 (config('ninja.log_pdf_html')) {
info($maker->getCompiledHTML());
}
if ($pdf) {
$maker =null;

View File

@ -44,6 +44,10 @@ class PdfService
public array $options;
private float $start_time;
public float $execution_time;
const DELIVERY_NOTE = 'delivery_note';
const STATEMENT = 'statement';
const PURCHASE_ORDER = 'purchase_order';
@ -61,7 +65,9 @@ class PdfService
}
public function boot(): self
{nlog("booties");
{
$this->start_time = microtime(true);
$this->init();
return $this;
@ -90,6 +96,8 @@ class PdfService
throw new \Exception($e->getMessage(), $e->getCode());
}
$this->execution_time = microtime(true) - $this->start_time;
return $pdf;
}
@ -107,6 +115,8 @@ class PdfService
nlog($html);
}
$this->execution_time = microtime(true) - $this->start_time;
return $html;
}