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
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
2020-10-27 05:26:56 +01:00
|
|
|
use Illuminate\Support\Str;
|
2020-11-27 03:30:37 +01:00
|
|
|
use Illuminate\Support\Facades\Response;
|
2020-08-04 13:00:19 +02:00
|
|
|
|
|
|
|
class Phantom
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
use MakesHash;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2020-11-25 11:30:00 +01:00
|
|
|
public function generate($invitation)
|
2020-09-06 11:38:10 +02:00
|
|
|
{
|
|
|
|
$entity = false;
|
|
|
|
|
|
|
|
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';
|
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') {
|
|
|
|
$path = $entity_obj->client->invoice_filepath();
|
|
|
|
}
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($entity == 'quote') {
|
|
|
|
$path = $entity_obj->client->quote_filepath();
|
|
|
|
}
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($entity == 'credit') {
|
|
|
|
$path = $entity_obj->client->credit_filepath();
|
|
|
|
}
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$file_path = $path.$entity_obj->number.'.pdf';
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2020-09-06 11:38:10 +02: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
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$key = config('ninja.phantomjs_key');
|
|
|
|
$secret = config('ninja.phantomjs_key');
|
2020-08-04 13:29:22 +02:00
|
|
|
|
2020-09-06 11:38:10 +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-11-26 10:39:35 +01:00
|
|
|
//info($pdf);
|
|
|
|
|
2020-08-12 05:13:39 +02: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-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)
|
|
|
|
{
|
2020-11-27 10:14:01 +01:00
|
|
|
$hash = Str::random(32);
|
|
|
|
Cache::put($hash, $html, 300);
|
|
|
|
|
|
|
|
$url = route('tmp_pdf', ['hash' => $hash]);
|
|
|
|
|
2020-11-27 03:02:05 +01:00
|
|
|
$key = config('ninja.phantomjs_key');
|
2020-11-27 10:14:01 +01:00
|
|
|
$phantom_url = "https://phantomjscloud.com/api/browser/v2/{$key}/?request=%url:%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
|
|
|
}
|
|
|
|
|
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';
|
2020-09-06 11:38:10 +02:00
|
|
|
$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([
|
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'),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$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-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-04 13:00:19 +02:00
|
|
|
}
|