1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Utils/PhantomJS/Phantom.php

230 lines
7.5 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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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;
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;
use Illuminate\Support\Facades\Cache;
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
{
2022-05-25 14:00:17 +02:00
use MakesHash, PageNumbering;
/**
* 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');
$phantom_url = "https://phantomjscloud.com/api/browser/v2/{$key}/";
$pdf = CurlUtils::post($phantom_url, json_encode([
'url' => $url,
'renderType' => 'pdf',
'outputAsJson' => false,
'renderSettings' => [
'emulateMedia' => 'print',
'pdfOptions' => [
'preferCSSPageSize' => true,
'printBackground' => true,
],
],
]));
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
$numbered_pdf = $this->pageNumbering($pdf, $invitation->company);
if ($numbered_pdf) {
$pdf = $numbered_pdf;
}
2022-05-25 14:00:17 +02:00
if (! Storage::disk(config('filesystems.default'))->exists($path)) {
2021-06-13 05:57:08 +02:00
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)
{
$key = config('ninja.phantomjs_key');
$phantom_url = "https://phantomjscloud.com/api/browser/v2/{$key}/";
$pdf = CurlUtils::post($phantom_url, json_encode([
'content' => $html,
'renderType' => 'pdf',
'outputAsJson' => false,
'renderSettings' => [
'emulateMedia' => 'print',
'pdfOptions' => [
'preferCSSPageSize' => true,
'printBackground' => true,
],
],
]));
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') {
2021-01-13 00:25:33 +01:00
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');
} else {
2021-01-13 00:25:33 +01:00
SystemLogger::dispatch(
'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,
$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';
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());
$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 = [
'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'),
],
'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-08-04 13:00:19 +02:00
}