Remove redundant pdf generator

This commit is contained in:
David Bomba 2025-01-28 16:29:32 +11:00
parent a4686ca234
commit cffb77fe36
3 changed files with 73 additions and 173 deletions

View File

@ -189,8 +189,13 @@ class PreviewController extends BaseController
$pdf = $ps->boot() $pdf = $ps->boot()
->designer ->designer
->buildFromPartials($request->design['design']) ->buildFromPartials($request->design['design']);
->getPdf();
if ($request->query('html') == 'true') {
return $ps->getHtml();
}
$pdf = $ps->getPdf();
return response()->streamDownload(function () use ($pdf) { return response()->streamDownload(function () use ($pdf) {
echo $pdf; echo $pdf;
@ -281,6 +286,8 @@ class PreviewController extends BaseController
private function blankEntity() private function blankEntity()
{ {
nlog("blankEntity");
/** @var \App\Models\User $user */ /** @var \App\Models\User $user */
$user = auth()->user(); $user = auth()->user();
@ -299,192 +306,66 @@ class PreviewController extends BaseController
return $this->mockEntity(); return $this->mockEntity();
} }
$design_object = json_decode(json_encode(request()->input('design'))); $design_object = json_decode(json_encode(request()->input('design')), true);
if (! is_object($design_object)) { if (! is_array($design_object)) {
return response()->json(['message' => 'Invalid custom design object'], 400); return response()->json(['message' => 'Invalid custom design object'], 400);
} }
$html = new HtmlEngine($invitation); $ps = new PdfService($invitation, 'product', [
'client' => $invitation->client ?? false,
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);
$state = [
'template' => $design->elements([
'client' => $invitation->invoice->client,
'entity' => $invitation->invoice,
'pdf_variables' => (array) $invitation->invoice->company->settings->pdf_variables,
'products' => request()->design['design']['product'],
]),
'variables' => $html->generateLabelsAndValues(),
'process_markdown' => $invitation->invoice->client->company->markdown_enabled,
'options' => [
'client' => $invitation->invoice->client,
'invoices' => [$invitation->invoice], 'invoices' => [$invitation->invoice],
] ]);
];
$maker = new PdfMaker($state); $ps->boot()
->designer
$maker ->buildFromPartials($design_object['design'])
->design($design)
->build(); ->build();
if (request()->query('html') == 'true') { if (request()->query('html') == 'true') {
return $maker->getCompiledHTML(); return $ps->getHtml();
} }
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') { $pdf = $ps->getPdf();
return (new Phantom())->convertHtmlToPdf($maker->getCompiledHTML(true));
return response()->streamDownload(function () use ($pdf) {
echo $pdf;
}, 'preview.pdf', [
'Content-Disposition' => 'inline',
'Content-Type' => 'application/pdf',
'Cache-Control:' => 'no-cache',
]);
} }
/** @var \App\Models\User $user */
$user = auth()->user();
/** @var \App\Models\Company $company */
$company = $user->company();
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, $company);
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
return $pdf;
}
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
$response = Response::make($file_path, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
}
private function mockEntity() private function mockEntity()
{ {
/** @var \App\Models\User $user */
$start = microtime(true);
$user = auth()->user(); $user = auth()->user();
/** @var \App\Models\Company $company */ /** @var \App\Models\Company $company */
$company = $user->company(); $company = $user->company();
try { $request = request()->input('design');
DB::connection($company->db)->beginTransaction(); $request['entity_type'] = request()->input('entity', 'invoice');
/** @var \App\Models\Client $client */ $pdf = (new PdfMock($request, $company))->build();
$client = Client::factory()->create([
'user_id' => $user->id,
'company_id' => $company->id,
]);
/** @var \App\Models\ClientContact $contact */
$contact = ClientContact::factory()->create([
'user_id' => $user->id,
'company_id' => $company->id,
'client_id' => $client->id,
'is_primary' => 1,
'send_email' => true,
]);
$settings = $company->settings;
/** @var \App\Models\Invoice $invoice */
$invoice = Invoice::factory()->create([
'user_id' => $user->id,
'company_id' => $company->id,
'client_id' => $client->id,
'terms' => $company->settings->invoice_terms,
'footer' => $company->settings->invoice_footer,
'public_notes' => 'Sample Public Notes',
]);
if ($settings->invoice_number_pattern) {
$invoice->number = $this->getFormattedEntityNumber(
$invoice,
rand(1, 9999),
$settings->counter_padding ?: 4,
$settings->invoice_number_pattern,
);
$invoice->save();
}
$invitation = InvoiceInvitation::factory()->create([
'user_id' => $user->id,
'company_id' => $company->id,
'invoice_id' => $invoice->id,
'client_contact_id' => $contact->id,
]);
$invoice->setRelation('invitations', $invitation);
$invoice->setRelation('client', $client);
$invoice->setRelation('company', $company);
$invoice->load('client.company');
$design_object = json_decode(json_encode(request()->input('design')));
if (! is_object($design_object)) {
return response()->json(['message' => 'Invalid custom design object'], 400);
}
$html = new HtmlEngine($invoice->invitations()->first());
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);
$state = [
'template' => $design->elements([
'client' => $invoice->client,
'entity' => $invoice,
'pdf_variables' => (array) $settings->pdf_variables,
'products' => request()->design['design']['product'],
]),
'variables' => $html->generateLabelsAndValues(),
'process_markdown' => $invoice->client->company->markdown_enabled,
'options' => [
'client' => $invoice->client,
'invoices' => [$invoice],
]
];
$maker = new PdfMaker($state);
$maker
->design($design)
->build();
DB::connection($company->db)->rollBack();
} catch (\Exception $e) {
DB::connection($company->db)->rollBack();
return response()->json(['message' => $e->getMessage()], 400);
}
if (request()->query('html') == 'true') { if (request()->query('html') == 'true') {
return $maker->getCompiledHTML(); return $pdf->getHtml();
} }
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') { $pdf = $pdf->getPdf();
return (new Phantom())->convertHtmlToPdf($maker->getCompiledHTML(true));
}
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') { $response = Response::make($pdf, 200);
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
$numbered_pdf = $this->pageNumbering($pdf, $company);
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
return $pdf;
}
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
$response = Response::make($file_path, 200);
$response->header('Content-Type', 'application/pdf'); $response->header('Content-Type', 'application/pdf');
$response->header('Server-Timing', (string) (microtime(true) - $start));
return $response; return $response;
} }
} }

View File

@ -59,6 +59,8 @@ class PdfDesigner
$this->template = $this->composeFromPartials($partials); $this->template = $this->composeFromPartials($partials);
nlog("template is");
nlog($this->template);
return $this; return $this;
} }

View File

@ -42,18 +42,20 @@ class PdfMock
private string $entity_string = 'invoice'; private string $entity_string = 'invoice';
private PdfService $pdf_service;
public function __construct(public array $request, public Company $company) public function __construct(public array $request, public Company $company)
{ {
} }
public function getPdf(): mixed public function setPdfService(): self
{ {
//need to resolve the pdf type here, ie product / purchase order //need to resolve the pdf type here, ie product / purchase order
$document_type = $this->request['entity_type'] == 'purchase_order' ? 'purchase_order' : 'product'; $document_type = $this->request['entity_type'] == 'purchase_order' ? 'purchase_order' : 'product';
$pdf_service = new PdfService($this->mock->invitation, $document_type); $this->pdf_service = new PdfService($this->mock->invitation, $document_type);
$pdf_config = (new PdfConfiguration($pdf_service)); $pdf_config = (new PdfConfiguration($this->pdf_service));
$pdf_config->entity = $this->mock; $pdf_config->entity = $this->mock;
$pdf_config->entity_string = $this->request['entity_type']; $pdf_config->entity_string = $this->request['entity_type'];
$this->entity_string = $this->request['entity_type']; $this->entity_string = $this->request['entity_type'];
@ -76,30 +78,45 @@ class PdfMock
$pdf_config->design = Design::withTrashed()->find($this->decodePrimaryKey($pdf_config->entity_design_id)); $pdf_config->design = Design::withTrashed()->find($this->decodePrimaryKey($pdf_config->entity_design_id));
} }
$pdf_service->config = $pdf_config; $this->pdf_service->config = $pdf_config;
if (isset($this->request['design'])) { if (isset($this->request['design'])) {
$pdf_designer = (new PdfDesigner($pdf_service))->buildFromPartials($this->request['design']); $pdf_designer = (new PdfDesigner($this->pdf_service))->buildFromPartials($this->request['design']);
} else { } else {
$pdf_designer = (new PdfDesigner($pdf_service))->build(); $pdf_designer = (new PdfDesigner($this->pdf_service))->build();
} }
$pdf_service->designer = $pdf_designer; $this->pdf_service->designer = $pdf_designer;
$pdf_service->html_variables = $document_type == 'purchase_order' ? $this->getVendorStubVariables() : $this->getStubVariables(); $this->pdf_service->html_variables = $document_type == 'purchase_order' ? $this->getVendorStubVariables() : $this->getStubVariables();
$pdf_builder = (new PdfBuilder($pdf_service))->build(); $pdf_builder = (new PdfBuilder($this->pdf_service))->build();
$pdf_service->builder = $pdf_builder; $this->pdf_service->builder = $pdf_builder;
$html = $pdf_service->getHtml(); return $this;
}
public function getPdf(): mixed
{
$html = $this->pdf_service->getHtml();
return $this->pdf_service->resolvePdfEngine($html);
}
public function getHtml(): string
{
return $this->pdf_service->getHtml();
return $pdf_service->resolvePdfEngine($html);
} }
public function build(): self public function build(): self
{ {
$this->mock = $this->initEntity(); $this->mock = $this->initEntity();
$this->setPdfService();
return $this; return $this;
} }