1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Performance improvements for PDFs

This commit is contained in:
David Bomba 2021-09-27 11:40:17 +10:00
parent 768a8bebba
commit 4443b13218
5 changed files with 41 additions and 6 deletions

View File

@ -16,6 +16,7 @@ use App\Events\Invoice\InvoiceWasViewed;
use App\Events\Misc\InvitationWasViewed;
use App\Events\Quote\QuoteWasViewed;
use App\Http\Controllers\Controller;
use App\Jobs\Entity\CreateRawPdf;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Payment;
@ -106,15 +107,12 @@ class InvitationController extends Controller
{
switch ($entity_string) {
case 'invoice':
$invitation->invoice->service()->markSent()->save();
event(new InvoiceWasViewed($invitation, $invitation->company, Ninja::eventVars()));
break;
case 'quote':
$invitation->quote->service()->markSent()->save();
event(new QuoteWasViewed($invitation, $invitation->company, Ninja::eventVars()));
break;
case 'credit':
$invitation->credit->service()->markSent()->save();
event(new CreditWasViewed($invitation, $invitation->company, Ninja::eventVars()));
break;
default:
@ -125,9 +123,43 @@ class InvitationController extends Controller
public function routerForDownload(string $entity, string $invitation_key)
{
if(Ninja::isHosted())
return $this->returnRawPdf($entity, $invitation_key);
return redirect('client/'.$entity.'/'.$invitation_key.'/download_pdf');
}
private function returnRawPdf(string $entity, string $invitation_key)
{
$key = $entity.'_id';
$entity_obj = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation';
$invitation = $entity_obj::whereRaw('BINARY `key`= ?', [$invitation_key])
->with('contact.client')
->firstOrFail();
if(!$invitation)
return response()->json(["message" => "no record found"], 400);
$file_name = $invitation->{$entity}->numberFormatter().'.pdf';
nlog($file_name);
$file = CreateRawPdf::dispatchNow($invitation, $invitation->company->db);
$headers = ['Content-Type' => 'application/pdf'];
if(request()->input('inline') == 'true')
$headers = array_merge($headers, ['Content-Disposition' => 'inline']);
return response()->streamDownload(function () use($file) {
echo $file;
}, $file_name, $headers);
}
public function routerForIframe(string $entity, string $client_hash, string $invitation_key)
{
}

View File

@ -104,9 +104,6 @@ class CreateEntityPdf implements ShouldQueue
/* Set customized translations _NOW_ */
$t->replace(Ninja::transformTranslations($this->entity->client->getMergedSettings()));
/*This line of code hurts... it deletes ALL $entity PDFs... this causes a race condition when trying to send an email*/
// $this->entity->service()->deletePdf();
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
return (new Phantom)->generate($this->invitation);
}

View File

@ -41,6 +41,8 @@ class CreditViewedActivity implements ShouldQueue
{
MultiDB::setDb($event->company->db);
$event->invitation->credit->service()->markSent()->save();
$fields = new stdClass;
$user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->invitation->user_id;

View File

@ -45,6 +45,8 @@ class InvoiceViewedActivity implements ShouldQueue
$user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->invitation->invoice->user_id;
$event->invitation->invoice->service()->markSent()->save();
$fields->user_id = $user_id;
$fields->company_id = $event->invitation->company_id;
$fields->activity_type_id = Activity::VIEW_INVOICE;

View File

@ -41,6 +41,8 @@ class QuoteViewedActivity implements ShouldQueue
{
MultiDB::setDb($event->company->db);
$event->invitation->quote->service()->markSent()->save();
$fields = new stdClass;
$fields->user_id = $event->invitation->quote->user_id;