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

211 lines
6.8 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
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-08-04 13:00:19 +02:00
*/
namespace App\Utils\PhantomJS;
2021-04-12 06:36:51 +02:00
use App\Exceptions\PhantomPDFFailure;
2021-01-13 00:25:33 +01:00
use App\Jobs\Util\SystemLogger;
2020-08-04 13:00:19 +02:00
use App\Models\CreditInvitation;
use App\Models\Design;
use App\Models\InvoiceInvitation;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoiceInvitation;
2021-01-13 00:25:33 +01:00
use App\Models\SystemLog;
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';
} elseif ($invitation instanceof RecurringInvoiceInvitation) {
$entity = 'recurring_invoice';
$entity_design_id = 'invoice_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') {
2021-06-12 13:50:01 +02:00
$path = $entity_obj->client->invoice_filepath($invitation);
}
2020-08-04 13:00:19 +02:00
if ($entity == 'quote') {
2021-06-12 13:50:01 +02:00
$path = $entity_obj->client->quote_filepath($invitation);
}
2020-08-04 13:00:19 +02:00
if ($entity == 'credit') {
2021-06-12 13:50:01 +02:00
$path = $entity_obj->client->credit_filepath($invitation);
}
2020-08-04 13:00:19 +02:00
if ($entity == 'recurring_invoice') {
2021-06-12 13:50:01 +02:00
$path = $entity_obj->client->recurring_invoice_filepath($invitation);
}
$file_path = $path.$entity_obj->numberFormatter().'.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
2021-01-13 00:25:33 +01:00
$this->checkMime($pdf, $invitation, $entity);
2021-03-06 22:50:14 +01:00
2021-06-13 05:57:08 +02:00
if(!Storage::disk(config('filesystems.default'))->exists($path))
Storage::disk(config('filesystems.default'))->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
}
2021-01-13 00:25:33 +01:00
/* Check if the returning PDF is valid. */
private function checkMime($pdf, $invitation, $entity)
{
$finfo = new \finfo(FILEINFO_MIME);
if($finfo->buffer($pdf) != 'application/pdf; charset=binary')
{
SystemLogger::dispatch(
$pdf,
SystemLog::CATEGORY_PDF,
SystemLog::EVENT_PDF_RESPONSE,
SystemLog::TYPE_PDF_FAILURE,
$invitation->contact->client,
$invitation->company,
2021-01-13 00:25:33 +01:00
);
2021-04-12 06:36:51 +02:00
throw new PhantomPDFFailure('There was an error generating the PDF with Phantom JS');
2021-01-13 00:25:33 +01:00
}
else {
SystemLogger::dispatch(
"Entity PDF generated sucessfully => " . $invitation->{$entity}->number,
SystemLog::CATEGORY_PDF,
SystemLog::EVENT_PDF_RESPONSE,
SystemLog::TYPE_PDF_SUCCESS,
$invitation->contact->client,
$invitation->company,
2021-01-13 00:25:33 +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';
if($entity == 'recurring_invoice')
$entity_design_id = 'invoice_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);
2021-02-02 10:33:02 +01:00
if (config('ninja.log_pdf_html')) {
info($data['html']);
}
2020-08-04 13:00:19 +02:00
return view('pdf.html', $data);
}
2020-08-04 13:00:19 +02:00
}