1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 03:02:34 +01:00
invoiceninja/app/Utils/PhantomJS/Phantom.php

156 lines
4.9 KiB
PHP
Raw Normal View History

2020-08-04 13:00:19 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2020-08-04 13:00:19 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-08-04 13:00:19 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Utils\PhantomJS;
use App\Models\CreditInvitation;
use App\Models\Design;
use App\Models\InvoiceInvitation;
use App\Models\QuoteInvitation;
2020-10-27 05:26:56 +01:00
use App\Services\PdfMaker\Design as PdfDesignModel;
2020-10-27 05:26:04 +01:00
use App\Services\PdfMaker\Design as PdfMakerDesign;
2020-10-27 05:26:56 +01:00
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
2020-10-28 11:10:49 +01:00
use App\Utils\CurlUtils;
2020-08-04 13:00:19 +02:00
use App\Utils\HtmlEngine;
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Facades\App;
2020-11-27 12:08:42 +01:00
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Response;
2020-08-04 13:00:19 +02:00
use Illuminate\Support\Facades\Storage;
2020-10-27 05:26:56 +01:00
use Illuminate\Support\Str;
2020-08-04 13:00:19 +02:00
class Phantom
{
use MakesHash;
/**
* Generate a PDF from the
* Phantom JS API.
*
2020-10-28 11:10:49 +01:00
* @param $invitation
*/
2020-11-25 11:30:00 +01:00
public function generate($invitation)
{
$entity = false;
if ($invitation instanceof InvoiceInvitation) {
$entity = 'invoice';
2020-10-27 04:09:13 +01:00
$entity_design_id = 'invoice_design_id';
} elseif ($invitation instanceof CreditInvitation) {
$entity = 'credit';
2020-10-27 04:09:13 +01:00
$entity_design_id = 'credit_design_id';
} elseif ($invitation instanceof QuoteInvitation) {
$entity = 'quote';
2020-10-27 04:09:13 +01:00
$entity_design_id = 'quote_design_id';
}
2020-08-04 13:00:19 +02:00
$entity_obj = $invitation->{$entity};
2020-08-04 13:00:19 +02:00
if ($entity == 'invoice') {
$path = $entity_obj->client->invoice_filepath();
}
2020-08-04 13:00:19 +02:00
if ($entity == 'quote') {
$path = $entity_obj->client->quote_filepath();
}
2020-08-04 13:00:19 +02:00
if ($entity == 'credit') {
$path = $entity_obj->client->credit_filepath();
}
2020-08-04 13:00:19 +02:00
$file_path = $path.$entity_obj->number.'.pdf';
2020-08-04 13:00:19 +02:00
2020-12-07 21:21:08 +01:00
$url = config('ninja.app_url').'/phantom/'.$entity.'/'.$invitation->key.'?phantomjs_secret='.config('ninja.phantomjs_secret');
2020-11-25 11:30:00 +01:00
info($url);
2020-08-04 13:29:22 +02:00
$key = config('ninja.phantomjs_key');
$secret = config('ninja.phantomjs_key');
2020-08-04 13:29:22 +02:00
$phantom_url = "https://phantomjscloud.com/api/browser/v2/{$key}/?request=%7Burl:%22{$url}%22,renderType:%22pdf%22%7D";
2020-10-28 11:10:49 +01:00
$pdf = CurlUtils::get($phantom_url);
2020-08-04 13:00:19 +02:00
2020-12-03 22:24:34 +01:00
// Storage::makeDirectory($path, 0775);
2020-08-04 13:00:19 +02:00
$instance = Storage::disk(config('filesystems.default'))->put($file_path, $pdf);
return $file_path;
}
2020-08-04 13:00:19 +02:00
2020-11-27 03:02:05 +01:00
public function convertHtmlToPdf($html)
{
2020-11-27 10:14:01 +01:00
$hash = Str::random(32);
Cache::put($hash, $html, 300);
2020-11-27 10:21:18 +01:00
2020-11-27 10:14:01 +01:00
$url = route('tmp_pdf', ['hash' => $hash]);
2020-11-27 12:08:42 +01:00
info($url);
2020-11-27 03:02:05 +01:00
$key = config('ninja.phantomjs_key');
2020-11-27 10:19:23 +01:00
$phantom_url = "https://phantomjscloud.com/api/browser/v2/{$key}/?request=%7Burl:%22{$url}%22,renderType:%22pdf%22%7D";
2020-11-27 03:29:46 +01:00
$pdf = CurlUtils::get($phantom_url);
2020-11-27 10:14:01 +01:00
2020-11-27 03:29:46 +01:00
$response = Response::make($pdf, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
2020-11-27 03:02:05 +01:00
}
public function displayInvitation(string $entity, string $invitation_key)
{
2020-08-04 13:00:19 +02:00
$key = $entity.'_id';
$invitation_instance = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation';
$invitation = $invitation_instance::whereRaw('BINARY `key`= ?', [$invitation_key])->first();
2020-10-28 11:10:49 +01:00
2020-08-04 13:00:19 +02:00
$entity_obj = $invitation->{$entity};
$entity_obj->load('client');
App::setLocale($invitation->contact->preferredLocale());
2020-10-27 04:09:13 +01:00
$entity_design_id = $entity . '_design_id';
2020-10-27 05:26:04 +01:00
$design_id = $entity_obj->design_id ? $entity_obj->design_id : $this->decodePrimaryKey($entity_obj->client->getSetting($entity_design_id));
$design = Design::find($design_id);
2020-10-27 12:57:12 +01:00
$html = new HtmlEngine($invitation);
2020-10-27 05:26:04 +01:00
if ($design->is_custom) {
2020-11-25 11:30:00 +01:00
$options = [
2020-10-27 05:26:04 +01:00
'custom_partials' => json_decode(json_encode($design->design), true)
];
2020-11-25 11:30:00 +01:00
$template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options);
2020-10-27 05:26:04 +01:00
} else {
2020-11-25 11:30:00 +01:00
$template = new PdfMakerDesign(strtolower($design->name));
2020-10-27 05:26:04 +01:00
}
$state = [
'template' => $template->elements([
'client' => $entity_obj->client,
'entity' => $entity_obj,
'pdf_variables' => (array) $entity_obj->company->settings->pdf_variables,
'$product' => $design->design->product,
2020-10-27 05:26:04 +01:00
]),
'variables' => $html->generateLabelsAndValues(),
'options' => [
'all_pages_header' => $entity_obj->client->getSetting('all_pages_header'),
'all_pages_footer' => $entity_obj->client->getSetting('all_pages_footer'),
],
];
$maker = new PdfMakerService($state);
$data['html'] = $maker->design($template)
->build()
->getCompiledHTML(true);
2020-08-04 13:00:19 +02:00
return view('pdf.html', $data);
}
2020-08-04 13:00:19 +02:00
}