Working on refactor for displaying PDFs

This commit is contained in:
David Bomba 2023-06-30 10:26:26 +10:00
parent 5b4232f7e3
commit d009eb48b9
5 changed files with 40 additions and 8 deletions

View File

@ -11,15 +11,17 @@
namespace App\Models; namespace App\Models;
use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use App\Utils\Traits\MakesHash;
use App\Jobs\Entity\CreateRawPdf;
use App\Jobs\Util\WebhookHandler; use App\Jobs\Util\WebhookHandler;
use App\Models\Traits\Excludable; use App\Models\Traits\Excludable;
use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model;
use App\Jobs\Vendor\CreatePurchaseOrderPdf;
use App\Utils\Traits\UserSessionAttributes; use App\Utils\Traits\UserSessionAttributes;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
/** /**
* Class BaseModel * Class BaseModel
@ -255,4 +257,30 @@ class BaseModel extends Model
WebhookHandler::dispatch($event_id, $this, $this->company, $additional_data); WebhookHandler::dispatch($event_id, $this, $this->company, $additional_data);
} }
} }
/**
* Returns the base64 encoded PDF string of the entity
*/
public function fullscreenPdfViewer($invitation = null): string
{
if (! $invitation) {
if ($this->invitations()->exists()) {
$invitation = $this->invitations()->first();
} else {
$this->service()->createInvitations();
$invitation = $this->invitations()->first();
}
}
if (! $invitation) {
throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?');
}
if($this instanceof \App\Models\PurchaseOrder)
return "data:application/pdf;base64,".base64_encode((new CreatePurchaseOrderPdf($invitation, $invitation->company->db))->rawPdf());
return "data:application/pdf;base64,".base64_encode((new CreateRawPdf($invitation, $invitation->company->db))->handle());
}
} }

View File

@ -14,6 +14,7 @@ namespace App\Models;
use App\Utils\Ninja; use App\Utils\Ninja;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesDates;
use App\Jobs\Entity\CreateRawPdf;
use App\Helpers\Invoice\InvoiceSum; use App\Helpers\Invoice\InvoiceSum;
use App\Jobs\Entity\CreateEntityPdf; use App\Jobs\Entity\CreateEntityPdf;
use App\Utils\Traits\MakesReminders; use App\Utils\Traits\MakesReminders;
@ -682,7 +683,8 @@ class Invoice extends BaseModel
} }
public function pdf_file_path($invitation = null, string $type = 'path', bool $portal = false) public function pdf_file_path($invitation = null, string $type = 'path', bool $portal = false)
{ {return "data:application/pdf;base64,".base64_encode((new CreateRawPdf($invitation, $invitation->company->db))->handle());
if (! $invitation) { if (! $invitation) {
if ($this->invitations()->exists()) { if ($this->invitations()->exists()) {
$invitation = $this->invitations()->first(); $invitation = $this->invitations()->first();

View File

@ -19,7 +19,7 @@ use Illuminate\Support\Facades\Storage;
class GetPurchaseOrderPdf extends AbstractService class GetPurchaseOrderPdf extends AbstractService
{ {
public function __construct(PurchaseOrder $purchase_order, VendorContact $contact = null) public function __construct(public PurchaseOrder $purchase_order, public VendorContact $contact = null)
{ {
$this->purchase_order = $purchase_order; $this->purchase_order = $purchase_order;

View File

@ -1,2 +1,2 @@
<iframe src="{{ $invoice->pdf_file_path($invitation, 'url', true) }}" <iframe src="{{ $invoice->fullscreenPdfViewer($invitation) }}" style="position:fixed; top:0; left:0; bottom:0; right:0; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;"></iframe>
style="position:fixed; top:0; left:0; bottom:0; right:0; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;"></iframe> <!-- <iframe src="{{ $invoice->pdf_file_path($invitation, 'url', true) }}" style="position:fixed; top:0; left:0; bottom:0; right:0; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;"></iframe> -->

View File

@ -0,0 +1,2 @@
<iframe src="{{ $purchase_order->fullscreenPdfViewer($invitation) }}" style="position:fixed; top:0; left:0; bottom:0; right:0; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;"></iframe>
<!-- <iframe src="{{ $purchase_order->pdf_file_path($invitation, 'url', true) }}" style="position:fixed; top:0; left:0; bottom:0; right:0; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;"></iframe> -->