2020-08-04 13:00:19 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-08-04 13:00:19 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. 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;
|
2021-01-30 01:19:43 +01:00
|
|
|
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;
|
2022-05-25 14:00:17 +02:00
|
|
|
use App\Utils\Traits\Pdf\PageNumbering;
|
2020-08-04 13:00:19 +02:00
|
|
|
use Illuminate\Support\Facades\App;
|
2020-11-27 12:08:42 +01:00
|
|
|
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
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
use MakesHash;
|
|
|
|
use PageNumbering;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a PDF from the
|
|
|
|
* Phantom JS API.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param $invitation
|
2020-09-06 11:38:10 +02:00
|
|
|
*/
|
2022-11-30 22:49:59 +01:00
|
|
|
public function generate($invitation, $return_pdf = false)
|
2020-09-06 11:38:10 +02:00
|
|
|
{
|
|
|
|
$entity = false;
|
2023-08-07 10:57:25 +02:00
|
|
|
$path = '';
|
2020-09-06 11:38:10 +02:00
|
|
|
|
|
|
|
if ($invitation instanceof InvoiceInvitation) {
|
|
|
|
$entity = 'invoice';
|
2020-10-27 04:09:13 +01:00
|
|
|
$entity_design_id = 'invoice_design_id';
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ($invitation instanceof CreditInvitation) {
|
|
|
|
$entity = 'credit';
|
2020-10-27 04:09:13 +01:00
|
|
|
$entity_design_id = 'credit_design_id';
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ($invitation instanceof QuoteInvitation) {
|
|
|
|
$entity = 'quote';
|
2020-10-27 04:09:13 +01:00
|
|
|
$entity_design_id = 'quote_design_id';
|
2021-01-30 02:23:43 +01:00
|
|
|
} elseif ($invitation instanceof RecurringInvoiceInvitation) {
|
2021-01-30 01:19:43 +01:00
|
|
|
$entity = 'recurring_invoice';
|
|
|
|
$entity_design_id = 'invoice_design_id';
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$entity_obj = $invitation->{$entity};
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($entity == 'invoice') {
|
2021-06-12 13:50:01 +02:00
|
|
|
$path = $entity_obj->client->invoice_filepath($invitation);
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($entity == 'quote') {
|
2021-06-12 13:50:01 +02:00
|
|
|
$path = $entity_obj->client->quote_filepath($invitation);
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($entity == 'credit') {
|
2021-06-12 13:50:01 +02:00
|
|
|
$path = $entity_obj->client->credit_filepath($invitation);
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2021-01-30 01:19:43 +01:00
|
|
|
if ($entity == 'recurring_invoice') {
|
2021-06-12 13:50:01 +02:00
|
|
|
$path = $entity_obj->client->recurring_invoice_filepath($invitation);
|
2021-01-30 01:19:43 +01:00
|
|
|
}
|
|
|
|
|
2021-03-17 12:29:20 +01:00
|
|
|
$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
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$key = config('ninja.phantomjs_key');
|
2021-10-06 01:34:53 +02:00
|
|
|
$phantom_url = "https://phantomjscloud.com/api/browser/v2/{$key}/";
|
2022-06-21 11:57:17 +02:00
|
|
|
$pdf = CurlUtils::post($phantom_url, json_encode([
|
2021-10-06 01:34:53 +02:00
|
|
|
'url' => $url,
|
|
|
|
'renderType' => 'pdf',
|
|
|
|
'outputAsJson' => false,
|
|
|
|
'renderSettings' => [
|
|
|
|
'emulateMedia' => 'print',
|
|
|
|
'pdfOptions' => [
|
|
|
|
'preferCSSPageSize' => true,
|
|
|
|
'printBackground' => true,
|
|
|
|
],
|
|
|
|
],
|
2022-06-21 11:57:17 +02:00
|
|
|
]));
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2021-01-13 00:25:33 +01:00
|
|
|
$this->checkMime($pdf, $invitation, $entity);
|
2022-05-25 14:00:17 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$numbered_pdf = $this->pageNumbering($pdf, $invitation->company);
|
|
|
|
|
|
|
|
if ($numbered_pdf) {
|
|
|
|
$pdf = $numbered_pdf;
|
|
|
|
}
|
2022-05-25 14:00:17 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! Storage::disk(config('filesystems.default'))->exists($path)) {
|
2023-08-07 10:57:25 +02:00
|
|
|
Storage::disk(config('filesystems.default'))->makeDirectory($path);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2020-08-04 13:00:19 +02:00
|
|
|
$instance = Storage::disk(config('filesystems.default'))->put($file_path, $pdf);
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($return_pdf) {
|
2022-11-30 22:49:59 +01:00
|
|
|
return $pdf;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-11-30 22:49:59 +01:00
|
|
|
|
2020-08-04 13:00:19 +02:00
|
|
|
return $file_path;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2020-11-27 03:02:05 +01:00
|
|
|
public function convertHtmlToPdf($html)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$key = config('ninja.phantomjs_key');
|
2021-10-06 01:34:53 +02:00
|
|
|
$phantom_url = "https://phantomjscloud.com/api/browser/v2/{$key}/";
|
2022-06-21 11:57:17 +02:00
|
|
|
$pdf = CurlUtils::post($phantom_url, json_encode([
|
2021-10-06 01:34:53 +02:00
|
|
|
'content' => $html,
|
|
|
|
'renderType' => 'pdf',
|
|
|
|
'outputAsJson' => false,
|
|
|
|
'renderSettings' => [
|
|
|
|
'emulateMedia' => 'print',
|
|
|
|
'pdfOptions' => [
|
|
|
|
'preferCSSPageSize' => true,
|
|
|
|
'printBackground' => true,
|
|
|
|
],
|
|
|
|
],
|
2022-06-21 11:57:17 +02:00
|
|
|
]));
|
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);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($finfo->buffer($pdf) != 'application/pdf; charset=binary') {
|
2021-01-13 00:25:33 +01:00
|
|
|
SystemLogger::dispatch(
|
|
|
|
$pdf,
|
|
|
|
SystemLog::CATEGORY_PDF,
|
|
|
|
SystemLog::EVENT_PDF_RESPONSE,
|
|
|
|
SystemLog::TYPE_PDF_FAILURE,
|
2021-05-19 03:12:23 +02:00
|
|
|
$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');
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-01-13 00:25:33 +01:00
|
|
|
SystemLogger::dispatch(
|
2022-06-21 11:57:17 +02:00
|
|
|
'Entity PDF generated sucessfully => '.$invitation->{$entity}->number,
|
2021-01-13 00:25:33 +01:00
|
|
|
SystemLog::CATEGORY_PDF,
|
|
|
|
SystemLog::EVENT_PDF_RESPONSE,
|
|
|
|
SystemLog::TYPE_PDF_SUCCESS,
|
2021-05-19 03:12:23 +02:00
|
|
|
$invitation->contact->client,
|
|
|
|
$invitation->company,
|
2021-01-13 00:25:33 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
public function displayInvitation(string $entity, string $invitation_key)
|
|
|
|
{
|
2020-08-04 13:00:19 +02:00
|
|
|
$key = $entity.'_id';
|
|
|
|
|
2020-11-26 22:22:28 +01:00
|
|
|
$invitation_instance = 'App\Models\\'.ucfirst(Str::camel($entity)).'Invitation';
|
2022-02-26 08:48:22 +01:00
|
|
|
$invitation = $invitation_instance::where('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());
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$entity_design_id = $entity.'_design_id';
|
2021-01-30 02:23:43 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($entity == 'recurring_invoice') {
|
2021-01-30 02:23:43 +01:00
|
|
|
$entity_design_id = 'invoice_design_id';
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-01-30 02:23:43 +01:00
|
|
|
|
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));
|
|
|
|
|
2023-04-29 13:01:02 +02:00
|
|
|
$design = Design::withTrashed()->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 = [
|
2022-06-21 11:57:17 +02: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([
|
2020-11-26 22:24:25 +01:00
|
|
|
'client' => $entity_obj->client,
|
|
|
|
'entity' => $entity_obj,
|
|
|
|
'pdf_variables' => (array) $entity_obj->company->settings->pdf_variables,
|
2020-11-04 14:56:08 +01:00
|
|
|
'$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'),
|
2023-09-19 02:05:13 +02:00
|
|
|
'client' => $entity_obj->client,
|
|
|
|
'entity' => $entity_obj,
|
2020-10-27 05:26:04 +01:00
|
|
|
],
|
2021-08-09 14:31:31 +02:00
|
|
|
'process_markdown' => $entity_obj->client->company->markdown_enabled,
|
2020-10-27 05:26:04 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$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-09-06 11:38:10 +02:00
|
|
|
}
|
2023-09-10 23:22:22 +02:00
|
|
|
|
2020-08-04 13:00:19 +02:00
|
|
|
}
|