1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Http/Controllers/PreviewController.php

636 lines
21 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers;
2023-10-27 12:09:53 +02:00
use App\Utils\Ninja;
2023-10-26 04:57:44 +02:00
use App\Models\Client;
use App\Models\Invoice;
2023-10-27 12:09:53 +02:00
use App\Utils\HtmlEngine;
use Twig\Error\SyntaxError;
use App\Jobs\Util\PreviewPdf;
use App\Models\ClientContact;
2023-10-26 04:57:44 +02:00
use App\Services\Pdf\PdfMock;
2023-10-27 12:09:53 +02:00
use App\Utils\Traits\MakesHash;
use App\Services\Pdf\PdfService;
use App\Utils\PhantomJS\Phantom;
use App\Models\InvoiceInvitation;
2023-02-16 02:36:09 +01:00
use App\Services\PdfMaker\Design;
2023-10-26 04:57:44 +02:00
use App\Utils\HostedPDF\NinjaPdf;
2023-10-27 12:09:53 +02:00
use Illuminate\Support\Facades\DB;
use App\Services\PdfMaker\PdfMaker;
2023-10-26 04:57:44 +02:00
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
2023-10-27 12:09:53 +02:00
use App\Utils\Traits\MakesInvoiceHtml;
2023-10-26 04:57:44 +02:00
use Turbo124\Beacon\Facades\LightLogs;
2023-10-27 12:09:53 +02:00
use App\Utils\Traits\Pdf\PageNumbering;
use Illuminate\Support\Facades\Response;
use App\DataMapper\Analytics\LivePreview;
use App\Services\Template\TemplateService;
use App\Http\Requests\Preview\DesignPreviewRequest;
use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Http\Requests\Preview\PreviewInvoiceRequest;
class PreviewController extends BaseController
{
use MakesHash;
use MakesInvoiceHtml;
2022-05-26 23:32:13 +02:00
use PageNumbering;
public function __construct()
{
2023-10-26 04:57:44 +02:00
parent::__construct();
2023-10-17 12:28:18 +02:00
}
2023-10-27 12:09:53 +02:00
public function live(PreviewInvoiceRequest $request): mixed
2023-10-26 04:57:44 +02:00
{
2023-10-27 12:09:53 +02:00
if (Ninja::isHosted() && !in_array($request->getHost(), ['preview.invoicing.co','staging.invoicing.co'])) {
return response()->json(['message' => 'This server cannot handle this request.'], 400);
}
$start = microtime(true);
/** Build models */
$invitation = $request->resolveInvitation();
$client = $request->getClient();
$settings = $client->getMergedSettings();
$entity_prop = str_replace("recurring_", "", $request->entity);
$entity_obj = $invitation->{$request->entity};
$entity_obj->fill($request->all());
if(!$entity_obj->id) {
$entity_obj->design_id = intval($this->decodePrimaryKey($settings->{$entity_prop."_design_id"}));
$entity_obj->footer = empty($entity_obj->footer) ? $settings->{$entity_prop."_footer"} : $entity_obj->footer;
$entity_obj->terms = empty($entity_obj->terms) ? $settings->{$entity_prop."_terms"} : $entity_obj->terms;
$entity_obj->public_notes = empty($entity_obj->public_notes) ? $request->getClient()->public_notes : $entity_obj->public_notes;
$invitation->setRelation($request->entity, $entity_obj);
}
$ps = new PdfService($invitation, 'product', [
'client' => $client ?? false,
// 'vendor' => $vendor ?? false,
"{$entity_prop}s" => [$entity_obj],
]);
$pdf = $ps->boot()->getPdf();
if (Ninja::isHosted()) {
LightLogs::create(new LivePreview())
->increment()
->batch();
}
/** Return PDF */
return response()->streamDownload(function () use ($pdf) {
echo $pdf;
}, 'preview.pdf', [
'Content-Disposition' => 'inline',
'Content-Type' => 'application/pdf',
'Cache-Control:' => 'no-cache',
'Server-Timing' => microtime(true)-$start
]);
2023-10-17 12:28:18 +02:00
}
2023-10-27 12:09:53 +02:00
2023-10-19 01:44:19 +02:00
/**
* Refactor - 2023-10-19
2023-10-26 04:57:44 +02:00
*
2023-10-19 01:44:19 +02:00
* New method does not require Transactions.
*
* @param PreviewInvoiceRequest $request
* @return mixed
*/
2023-10-27 12:09:53 +02:00
public function livexx(PreviewInvoiceRequest $request): mixed
2023-10-26 04:57:44 +02:00
{
2023-10-17 12:28:18 +02:00
2023-10-19 01:44:19 +02:00
if (Ninja::isHosted() && !in_array($request->getHost(), ['preview.invoicing.co','staging.invoicing.co'])) {
return response()->json(['message' => 'This server cannot handle this request.'], 400);
}
2023-10-18 23:53:28 +02:00
$start = microtime(true);
2023-10-17 12:28:18 +02:00
2023-10-19 01:44:19 +02:00
/** Build models */
2023-10-17 12:28:18 +02:00
$invitation = $request->resolveInvitation();
2023-10-18 23:53:28 +02:00
$client = $request->getClient();
$settings = $client->getMergedSettings();
2023-10-17 12:28:18 +02:00
2023-10-19 01:44:19 +02:00
/** Set translations */
2023-10-17 12:28:18 +02:00
App::forgetInstance('translator');
$t = app('translator');
App::setLocale($invitation->contact->preferredLocale());
2023-10-18 23:53:28 +02:00
$t->replace(Ninja::transformTranslations($settings));
2023-10-17 12:28:18 +02:00
2023-10-18 23:53:28 +02:00
$entity_prop = str_replace("recurring_", "", $request->entity);
2023-10-17 12:28:18 +02:00
$entity_obj = $invitation->{$request->entity};
2023-10-19 01:50:25 +02:00
$entity_obj->fill($request->all());
2023-10-17 12:28:18 +02:00
2023-10-19 01:44:19 +02:00
/** Update necessary objecty props */
2023-10-18 23:53:28 +02:00
if(!$entity_obj->id) {
$entity_obj->design_id = intval($this->decodePrimaryKey($settings->{$entity_prop."_design_id"}));
2023-10-25 01:47:54 +02:00
$entity_obj->footer = empty($entity_obj->footer) ? $settings->{$entity_prop."_footer"} : $entity_obj->footer;
2023-10-25 10:04:27 +02:00
$entity_obj->terms = empty($entity_obj->terms) ? $settings->{$entity_prop."_terms"} : $entity_obj->terms;
2023-10-25 01:47:54 +02:00
$entity_obj->public_notes = empty($entity_obj->public_notes) ? $request->getClient()->public_notes : $entity_obj->public_notes;
2023-10-18 23:53:28 +02:00
$invitation->{$request->entity} = $entity_obj;
}
2023-10-26 04:57:44 +02:00
if(empty($entity_obj->design_id)) {
2023-10-25 01:08:36 +02:00
$entity_obj->design_id = intval($this->decodePrimaryKey($settings->{$entity_prop."_design_id"}));
2023-10-26 04:57:44 +02:00
}
2023-10-25 01:08:36 +02:00
2023-10-19 01:44:19 +02:00
/** Generate variables */
2023-10-18 23:53:28 +02:00
$html = new HtmlEngine($invitation);
$html->settings = $settings;
$variables = $html->generateLabelsAndValues();
2023-10-17 12:28:18 +02:00
2023-10-19 22:46:45 +02:00
$design = \App\Models\Design::query()->withTrashed()->find($entity_obj->design_id ?? 2);
2023-10-17 12:28:18 +02:00
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));
}
$state = [
'template' => $template->elements([
2023-10-18 23:53:28 +02:00
'client' => $client,
2023-10-17 12:28:18 +02:00
'entity' => $entity_obj,
2023-10-18 23:53:28 +02:00
'pdf_variables' => (array) $settings->pdf_variables,
2023-10-17 12:28:18 +02:00
'$product' => $design->design->product,
'variables' => $variables,
]),
'variables' => $variables,
'options' => [
2023-10-18 23:53:28 +02:00
'all_pages_header' => $client->getSetting('all_pages_header'),
'all_pages_footer' => $client->getSetting('all_pages_footer'),
2023-10-26 08:13:06 +02:00
'client' => $entity_obj->client ?? [],
'vendor' => $entity_obj->vendor ?? [],
$request->input('entity')."s" => [$entity_obj],
2023-10-17 12:28:18 +02:00
],
2023-10-18 23:53:28 +02:00
'process_markdown' => $client->company->markdown_enabled,
2023-10-17 12:28:18 +02:00
];
$maker = new PdfMaker($state);
$maker
->design($template)
->build();
2023-10-19 01:44:19 +02:00
/** Generate HTML */
$html = $maker->getCompiledHTML(true);
2023-10-18 23:53:28 +02:00
2023-10-26 04:57:44 +02:00
if (request()->query('html') == 'true') {
2023-10-19 01:44:19 +02:00
return $html;
2023-10-26 04:57:44 +02:00
}
2023-10-17 12:28:18 +02:00
//if phantom js...... inject here..
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2023-10-19 01:44:19 +02:00
return (new Phantom)->convertHtmlToPdf($html);
2023-10-17 12:28:18 +02:00
}
/** @var \App\Models\User $user */
$user = auth()->user();
$company = $user->company();
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
2023-10-19 01:44:19 +02:00
$pdf = (new NinjaPdf())->build($html);
2023-10-17 12:28:18 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $company);
2023-10-26 04:57:44 +02:00
if ($numbered_pdf) {
2023-10-17 12:28:18 +02:00
$pdf = $numbered_pdf;
2023-10-26 04:57:44 +02:00
}
2023-10-17 12:28:18 +02:00
return $pdf;
}
2023-10-19 01:44:19 +02:00
$pdf = (new PreviewPdf($html, $company))->handle();
2023-10-17 12:28:18 +02:00
2023-10-19 01:44:19 +02:00
if (Ninja::isHosted()) {
LightLogs::create(new LivePreview())
->increment()
->batch();
}
2023-10-18 23:53:28 +02:00
2023-10-19 01:44:19 +02:00
/** Return PDF */
2023-10-17 12:28:18 +02:00
return response()->streamDownload(function () use ($pdf) {
echo $pdf;
2023-10-19 01:44:19 +02:00
}, 'preview.pdf', [
2023-10-26 04:57:44 +02:00
'Content-Disposition' => 'inline',
'Content-Type' => 'application/pdf',
'Cache-Control:' => 'no-cache',
2023-10-19 01:44:19 +02:00
'Server-Timing' => microtime(true)-$start
]);
2023-10-17 12:28:18 +02:00
}
2023-10-19 01:44:19 +02:00
/**
2023-10-19 01:44:19 +02:00
* Returns the mocked PDF for the invoice design preview.
*
2023-10-19 01:44:19 +02:00
* Only used in Settings > Invoice Design as a general overview
2023-10-26 04:57:44 +02:00
*
2023-10-19 01:44:19 +02:00
* @param DesignPreviewRequest $request
2023-04-26 14:17:40 +02:00
* @return mixed
*/
2023-10-19 01:44:19 +02:00
public function design(DesignPreviewRequest $request): mixed
{
$start = microtime(true);
/** @var \App\Models\User $user */
$user = auth()->user();
/** @var \App\Models\Company $company */
$company = $user->company();
$pdf = (new PdfMock($request->all(), $company))->build()->getPdf();
$response = Response::make($pdf, 200);
$response->header('Content-Type', 'application/pdf');
$response->header('Server-Timing', microtime(true)-$start);
return $response;
}
2023-04-26 14:17:40 +02:00
2023-10-19 01:44:19 +02:00
/**
* Returns a template filled with entity variables.
2023-10-26 04:57:44 +02:00
*
2023-10-19 01:44:19 +02:00
* Used in the Custom Designer to preview design changes
* @return mixed
*/
public function show()
{
2023-10-26 04:57:44 +02:00
if(request()->has('template')) {
2023-09-25 07:56:32 +02:00
return $this->template();
2023-10-26 04:57:44 +02:00
}
2023-09-25 07:56:32 +02:00
if (request()->has('entity') &&
request()->has('entity_id') &&
! empty(request()->input('entity')) &&
! empty(request()->input('entity_id')) &&
request()->has('body')) {
2023-10-19 01:44:19 +02:00
$design_object = json_decode(json_encode(request()->input('design')));
2020-03-09 12:12:45 +01:00
if (! is_object($design_object)) {
2021-01-25 00:04:50 +01:00
return response()->json(['message' => ctrans('texts.invalid_design_object')], 400);
}
2020-03-09 12:12:45 +01:00
$entity = ucfirst(request()->input('entity'));
$class = "App\Models\\$entity";
$entity_obj = $class::whereId($this->decodePrimaryKey(request()->input('entity_id')))->company()->first();
if (! $entity_obj) {
return $this->blankEntity();
}
$entity_obj->load('client');
App::forgetInstance('translator');
2021-05-31 12:40:34 +02:00
$t = app('translator');
App::setLocale($entity_obj->client->primary_contact()->preferredLocale());
$t->replace(Ninja::transformTranslations($entity_obj->client->getMergedSettings()));
2020-10-27 12:57:12 +01:00
$html = new HtmlEngine($entity_obj->invitations()->first());
$design_namespace = 'App\Services\PdfMaker\Designs\\'.request()->design['name'];
2020-08-21 16:47:17 +02:00
$design_class = new $design_namespace();
$state = [
'template' => $design_class->elements([
'client' => $entity_obj->client,
'entity' => $entity_obj,
'pdf_variables' => (array) $entity_obj->company->settings->pdf_variables,
'products' => request()->design['design']['product'],
2020-08-21 16:47:17 +02:00
]),
'variables' => $html->generateLabelsAndValues(),
'process_markdown' => $entity_obj->client->company->markdown_enabled,
2023-09-19 02:05:13 +02:00
'options' => [
2023-10-26 08:13:06 +02:00
'client' => $entity_obj->client ?? [],
'vendor' => $entity_obj->vendor ?? [],
request()->input('entity_type', 'invoice')."s" => [$entity_obj],
2023-09-19 02:05:13 +02:00
]
2020-08-21 16:47:17 +02:00
];
2020-09-04 13:17:30 +02:00
$design = new Design(request()->design['name']);
2020-08-21 16:47:17 +02:00
$maker = new PdfMaker($state);
$maker
2020-09-04 13:17:30 +02:00
->design($design)
2020-08-21 16:47:17 +02:00
->build();
2023-04-02 13:42:14 +02:00
if (request()->query('html') == 'true') {
2022-03-16 21:57:06 +01:00
return $maker->getCompiledHTML();
2020-12-16 12:52:40 +01:00
}
2020-12-11 21:51:10 +01:00
2020-11-27 03:02:05 +01:00
//if phantom js...... inject here..
2021-05-09 13:30:31 +02:00
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2020-11-27 03:02:05 +01:00
return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
}
2023-04-27 00:05:57 +02:00
/** @var \App\Models\User $user */
2023-04-26 16:16:07 +02:00
$user = auth()->user();
$company = $user->company();
2023-04-26 16:11:31 +02:00
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
2023-04-26 16:11:31 +02:00
2022-05-26 23:32:13 +02:00
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
2023-04-26 16:11:31 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $company);
2023-10-26 04:57:44 +02:00
if ($numbered_pdf) {
2022-05-26 23:32:13 +02:00
$pdf = $numbered_pdf;
2023-10-26 04:57:44 +02:00
}
2022-05-26 23:32:13 +02:00
return $pdf;
2020-11-27 03:02:05 +01:00
2023-10-19 01:44:19 +02:00
}
2023-10-19 01:44:19 +02:00
$pdf = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
2023-04-27 00:05:57 +02:00
2023-10-19 01:44:19 +02:00
return response()->streamDownload(function () use ($pdf) {
echo $pdf;
}, 'preview.pdf', [
'Content-Disposition' => 'inline',
'Content-Type' => 'application/pdf',
'Cache-Control:' => 'no-cache',
]);
2023-04-26 14:17:40 +02:00
2023-02-21 10:04:45 +01:00
2023-10-19 01:44:19 +02:00
}
2023-02-21 10:04:45 +01:00
2023-10-19 01:44:19 +02:00
return $this->blankEntity();
2023-02-21 10:04:45 +01:00
}
2023-09-25 07:56:32 +02:00
private function template()
{
/** @var \App\Models\User $user */
$user = auth()->user();
/** @var \App\Models\Company $company */
$company = $user->company();
2023-10-26 04:57:44 +02:00
$design_object = json_decode(json_encode(request()->input('design')), 1);
2023-09-25 07:56:32 +02:00
$ts = (new TemplateService());
2023-10-02 02:06:24 +02:00
try {
2023-10-26 04:57:44 +02:00
$ts->setCompany($company)
->setTemplate($design_object)
->mock();
} catch(SyntaxError $e) {
2023-10-02 02:06:24 +02:00
// return response()->json(['message' => 'Twig syntax is invalid.', 'errors' => new \stdClass], 422);
}
2023-09-25 07:56:32 +02:00
$html = $ts->getHtml();
if (request()->query('html') == 'true') {
return $html;
}
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
return (new Phantom)->convertHtmlToPdf($html);
}
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
$pdf = (new NinjaPdf())->build($html);
$numbered_pdf = $this->pageNumbering($pdf, $company);
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
return $pdf;
}
$file_path = (new PreviewPdf($html, $company))->handle();
$response = Response::make($file_path, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
}
2023-09-27 10:10:22 +02:00
private function stubTemplateData()
{
}
private function blankEntity()
{
2023-04-26 16:11:31 +02:00
2023-04-27 00:05:57 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-04-26 16:11:31 +02:00
/** @var \App\Models\Company $company */
2023-04-27 00:05:57 +02:00
$company = $user->company();
2023-04-26 16:11:31 +02:00
App::forgetInstance('translator');
2021-05-31 12:40:34 +02:00
$t = app('translator');
2023-04-26 16:11:31 +02:00
$t->replace(Ninja::transformTranslations($company->settings));
2023-08-04 09:12:21 +02:00
/** @var \App\Models\InvoiceInvitation $invitation */
2023-04-26 16:11:31 +02:00
$invitation = InvoiceInvitation::where('company_id', $company->id)->orderBy('id', 'desc')->first();
2021-12-19 21:16:44 +01:00
/* If we don't have a valid invitation in the system - create a mock using transactions */
if (! $invitation) {
2021-12-19 21:16:44 +01:00
return $this->mockEntity();
}
2021-12-19 21:16:44 +01:00
$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($invitation);
$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,
2023-09-19 02:05:13 +02:00
'options' => [
'client' => $invitation->invoice->client,
'invoices' => [$invitation->invoice],
2023-09-19 02:05:13 +02:00
]
2021-12-19 21:16:44 +01:00
];
$maker = new PdfMaker($state);
$maker
->design($design)
->build();
if (request()->query('html') == 'true') {
return $maker->getCompiledHTML();
}
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
}
2023-04-27 00:05:57 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-04-26 16:11:31 +02:00
/** @var \App\Models\Company $company */
2023-04-27 00:05:57 +02:00
$company = $user->company();
2023-04-26 16:11:31 +02:00
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
2022-05-26 23:32:13 +02:00
2023-04-26 16:11:31 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $company);
2022-05-26 23:32:13 +02:00
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
2022-05-26 23:32:13 +02:00
return $pdf;
2021-12-19 21:16:44 +01:00
}
2023-04-26 16:11:31 +02:00
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
2021-12-19 21:16:44 +01:00
$response = Response::make($file_path, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
}
private function mockEntity()
{
2023-04-27 00:05:57 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2023-04-26 14:17:40 +02:00
/** @var \App\Models\Company $company */
2023-04-27 00:05:57 +02:00
$company = $user->company();
try {
DB::connection($company->db)->beginTransaction();
2020-07-05 13:39:59 +02:00
/** @var \App\Models\Client $client */
$client = Client::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => $company->id,
]);
/** @var \App\Models\ClientContact $contact */
$contact = ClientContact::factory()->create([
'user_id' => auth()->user()->id,
'company_id' => $company->id,
'client_id' => $client->id,
'is_primary' => 1,
'send_email' => true,
]);
/** @var \App\Models\Invoice $invoice */
2023-04-26 14:17:40 +02:00
$invoice = Invoice::factory()->create([
'user_id' => auth()->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',
]);
2020-08-04 13:00:19 +02:00
$invitation = InvoiceInvitation::factory()->create([
'user_id' => auth()->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());
2020-08-28 09:51:02 +02:00
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);
2020-08-28 09:51:02 +02:00
$state = [
'template' => $design->elements([
'client' => $invoice->client,
'entity' => $invoice,
'pdf_variables' => (array) $invoice->company->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);
2020-08-28 09:51:02 +02:00
$maker
->design($design)
->build();
2020-08-28 09:51:02 +02:00
DB::connection($company->db)->rollBack();
2023-10-26 04:57:44 +02:00
} catch(\Exception $e) {
DB::connection($company->db)->rollBack();
return response()->json(['message' => $e->getMessage()], 400);
}
2021-12-19 21:16:44 +01:00
if (request()->query('html') == 'true') {
return $maker->getCompiledHTML();
}
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
2020-11-27 12:08:42 +01:00
return (new Phantom)->convertHtmlToPdf($maker->getCompiledHTML(true));
}
2021-03-18 10:57:55 +01:00
if (config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja') {
2022-05-26 23:32:13 +02:00
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
2023-04-26 14:17:40 +02:00
$numbered_pdf = $this->pageNumbering($pdf, $company);
2022-05-26 23:32:13 +02:00
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
2022-05-26 23:32:13 +02:00
return $pdf;
2021-03-18 10:57:55 +01:00
}
2023-04-26 14:17:40 +02:00
$file_path = (new PreviewPdf($maker->getCompiledHTML(true), $company))->handle();
2020-07-05 12:58:30 +02:00
$response = Response::make($file_path, 200);
$response->header('Content-Type', 'application/pdf');
2020-08-28 09:51:02 +02:00
return $response;
}
}