1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Implement PhantomJS

This commit is contained in:
David Bomba 2020-08-04 21:00:19 +10:00
parent 374fd90df1
commit d7dbde3789
11 changed files with 171 additions and 3 deletions

View File

@ -51,6 +51,9 @@ ERROR_EMAIL=
NINJA_ENVIRONMENT=selfhost
PHANTOMJS_KEY=
PHANTOMJS_SECRET=
SELF_UPDATER_REPO_VENDOR = invoiceninja
SELF_UPDATER_REPO_NAME = invoiceninja
SELF_UPDATER_USE_BRANCH = v2

View File

@ -126,14 +126,13 @@ class PreviewController extends BaseController
'company_id' => auth()->user()->company()->id,
'client_id' => $client->id,
]);
info('blank');
$invitation = factory(\App\Models\InvoiceInvitation::class)->create([
'user_id' => auth()->user()->id,
'company_id' => auth()->user()->company()->id,
'invoice_id' => $invoice->id,
'client_contact_id' => $contact->id,
]);
info('post invite');
$invoice->setRelation('invitations', $invitation);
$invoice->setRelation('client', $client);

View File

@ -115,6 +115,6 @@ class Kernel extends HttpKernel
'locale' => \App\Http\Middleware\Locale::class,
'contact.register' => \App\Http\Middleware\ContactRegister::class,
'shop_token_auth' => \App\Http\Middleware\Shop\ShopTokenAuth::class,
'phantom_secret' => \App\Http\Middleware\PhantomSecret::class,
];
}

View File

@ -0,0 +1,37 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @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\Http\Middleware;
use Closure;
class PhantomSecret
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if( config('ninja.phantomjs_secret') && $request->has('phantomjs_secret') && (config('ninja.phantomjs_secret') == $request->input('phantomjs_secret')) )
{
return $next($request);
}
return redirect('/');
}
}

View File

@ -20,6 +20,7 @@ use App\Models\Company;
use App\Models\Design;
use App\Models\Invoice;
use App\Utils\HtmlEngine;
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\NumberFormatter;
@ -67,6 +68,9 @@ class CreateCreditPdf implements ShouldQueue
public function handle()
{
if(config('ninja.phantomjs_key'))
return (new Phantom)->generate($this->invitation);
$this->credit->load('client');
App::setLocale($this->contact->preferredLocale());

View File

@ -20,6 +20,7 @@ use App\Models\Company;
use App\Models\Design;
use App\Models\Invoice;
use App\Utils\HtmlEngine;
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\NumberFormatter;
@ -67,6 +68,9 @@ class CreateInvoicePdf implements ShouldQueue
public function handle()
{
if(config('ninja.phantomjs_key'))
return (new Phantom)->generate($this->invitation);
App::setLocale($this->contact->preferredLocale());
$path = $this->invoice->client->invoice_filepath();

View File

@ -20,6 +20,7 @@ use App\Models\Company;
use App\Models\Design;
use App\Models\Invoice;
use App\Utils\HtmlEngine;
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\NumberFormatter;
@ -67,6 +68,9 @@ class CreateQuotePdf implements ShouldQueue
public function handle()
{
if(config('ninja.phantomjs_key'))
return (new Phantom)->generate($this->invitation);
$this->quote->load('client');
App::setLocale($this->contact->preferredLocale());

View File

@ -0,0 +1,111 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @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\Designs\Designer;
use App\Models\CreditInvitation;
use App\Models\Design;
use App\Models\InvoiceInvitation;
use App\Models\QuoteInvitation;
use App\Utils\HtmlEngine;
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Storage;
class Phantom
{
use MakesHash;
/**
* Generate a PDF from the
* Phantom JS API
*
* @return pdf HTML to PDF conversion
*/
public function generate($invitation)
{
$entity = false;
if($invitation instanceof InvoiceInvitation)
$entity = 'invoice';
elseif($invitation instanceof CreditInvitation)
$entity = 'credit';
elseif($invitation instanceof QuoteInvitation)
$entity = 'quote';
$entity_obj = $invitation->{$entity};
if($entity == 'invoice')
$path = $entity_obj->client->invoice_filepath();
if($entity == 'quote')
$path = $entity_obj->client->quote_filepath();
if($entity == 'credit')
$path = $entity_obj->client->credit_filepath();
$file_path = $path . $entity_obj->number . '.pdf';
$url = rtrim(config('ninja.app_url'), "/") . 'phantom/' . $entity . '/' . $invitation . '?phantomjs_secret='. config('ninja.phantomjs_secret');
$json_payload = new \stdClass;
$json_payload->url = $url;
$json_payload->renderType = "pdf";
$url = "http://PhantomJScloud.com/api/browser/v2/" . config('ninja.phantomjs_key');
$payload = json_encode($json_payload);
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => $payload
)
);
$context = stream_context_create($options);
$pdf = file_get_contents($url, false, $context);
if ($pdf === FALSE) { /* Handle error */ info("i did not make the PDF from phantom"); }
Storage::makeDirectory($path, 0755);
$instance = Storage::disk(config('filesystems.default'))->put($file_path, $pdf);
return $file_path;
}
public function displayInvitation(string $entity, string $invitation_key)
{
$key = $entity.'_id';
$invitation_instance = 'App\Models\\'.ucfirst($entity).'Invitation';
$invitation = $invitation_instance::whereRaw("BINARY `key`= ?", [$invitation_key])->first();
$entity_obj = $invitation->{$entity};
$entity_obj->load('client');
App::setLocale($invitation->contact->preferredLocale());
$design_id = $entity_obj->design_id ? $entity_obj->design_id : $this->decodePrimaryKey($entity_obj->client->getSetting($entity . '_design_id'));
$design = Design::find($design_id);
$designer = new Designer($entity_obj, $design, $entity_obj->client->getSetting('pdf_variables'), $entity);
$data['html'] = (new HtmlEngine($designer, $invitation, $entity))->build();
return view('pdf.html', $data);
}
}

View File

@ -27,6 +27,8 @@ return [
'hash_salt' => env('HASH_SALT', ''),
'currency_converter_api_key' => env('OPENEXCHANGE_APP_ID',''),
'enabled_modules' => 32767,
'phantomjs_key' => env('PHANTOMJS_KEY', false),
'phantomjs_secret' => env('PHANTOMJS_SECRET', false),
'environment' => env('NINJA_ENVIRONMENT', 'selfhost'), // 'hosted', 'development', 'selfhost', 'reseller'

View File

@ -0,0 +1 @@
{!! $html !!}

View File

@ -73,6 +73,9 @@ Route::group(['middleware' => ['invite_db'], 'prefix' => 'client', 'as' => 'clie
Route::get('{entity}/{client_hash}/{invitation_key}', 'ClientPortal\InvitationController@routerForIframe')->name('invoice.client_hash_and_invitation_key'); //should never need this
Route::get('payment_hook/{company_gateway_id}/{gateway_type_id}', 'ClientPortal\PaymentHookController@process');
});
Route::get('phantom/{entity}/{invitation_key}', '\App\Utils\PhantomJS\Phantom@displayInvitation')->middleware(['invite_db','phantom_secret'])->name('phantom_view');
Route::fallback('BaseController@notFoundClient');