Page numbering for PDFs

This commit is contained in:
David Bomba 2022-05-25 22:00:17 +10:00
parent a2977ef1ad
commit b6f48c0d8c
9 changed files with 69 additions and 16 deletions

View File

@ -17,6 +17,7 @@ use App\Transformers\ActivityTransformer;
use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\Ninja;
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\Pdf\PageNumbering;
use App\Utils\Traits\Pdf\PdfMaker;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@ -27,7 +28,7 @@ use stdClass;
class ActivityController extends BaseController
{
use PdfMaker;
use PdfMaker, PageNumbering;
protected $entity_type = Activity::class;
@ -164,12 +165,35 @@ class ActivityController extends BaseController
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
$pdf = (new Phantom)->convertHtmlToPdf($html_backup);
$numbered_pdf = $this->pageNumbering($pdf, $activity->company);
if($numbered_pdf)
$pdf = $numbered_pdf;
}
elseif(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja'){
$pdf = (new NinjaPdf())->build($html_backup);
$numbered_pdf = $this->pageNumbering($pdf, $activity->company);
if($numbered_pdf)
$pdf = $numbered_pdf;
}
else {
$pdf = $this->makePdf(null, null, $html_backup);
$numbered_pdf = $this->pageNumbering($pdf, $activity->company);
if($numbered_pdf)
$pdf = $numbered_pdf;
}
if (isset($activity->invoice_id)) {

View File

@ -191,7 +191,6 @@ class InvitationController extends Controller
return response()->json(["message" => "no record found"], 400);
$file_name = $invitation->{$entity}->numberFormatter().'.pdf';
nlog($file_name);
$file = CreateRawPdf::dispatchNow($invitation, $invitation->company->db);

View File

@ -190,15 +190,22 @@ class CreateEntityPdf implements ShouldQueue
if(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja'){
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
$pdf = $this->pageNumbering($pdf, $this->company);
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
if($numbered_pdf)
$pdf = $numbered_pdf;
}
else {
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
$pdf = $this->pageNumbering($pdf, $this->company);
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
if($numbered_pdf)
$pdf = $numbered_pdf;
}

View File

@ -1,5 +1,4 @@
<?php
/**
* Entity Ninja (https://entityninja.com).
*
@ -34,6 +33,7 @@ use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\NumberFormatter;
use App\Utils\Traits\Pdf\PageNumbering;
use App\Utils\Traits\Pdf\PdfMaker;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -46,7 +46,7 @@ use Illuminate\Support\Facades\Storage;
class CreateRawPdf implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash, PageNumbering;
public $entity;
@ -177,11 +177,22 @@ class CreateRawPdf implements ShouldQueue
if($finfo->buffer($pdf) != 'application/pdf; charset=binary')
{
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
if($numbered_pdf)
$pdf = $numbered_pdf;
}
}
else {
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
if($numbered_pdf)
$pdf = $numbered_pdf;
}
} catch (\Exception $e) {

View File

@ -49,8 +49,11 @@ class PreviewPdf implements ShouldQueue
{
$pdf = $this->makePdf(null, null, $this->design_string);
$pdf = $this->pageNumbering($pdf, $this->company);
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
if($numbered_pdf)
$pdf = $numbered_pdf;
return $pdf;
}
}

View File

@ -92,6 +92,9 @@ class PaymentNotification implements ShouldQueue
$analytics_id = $company->google_analytics_key;
if(!strlen($analytics_id) > 2)
return;
$client = $payment->client;
$amount = $payment->amount;

View File

@ -25,6 +25,7 @@ use App\Services\PdfMaker\PdfMaker as PdfMakerService;
use App\Utils\CurlUtils;
use App\Utils\HtmlEngine;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\Pdf\PageNumbering;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Storage;
@ -33,7 +34,7 @@ use Illuminate\Support\Str;
class Phantom
{
use MakesHash;
use MakesHash, PageNumbering;
/**
* Generate a PDF from the
@ -99,6 +100,12 @@ class Phantom
$this->checkMime($pdf, $invitation, $entity);
$numbered_pdf = $this->pageNumbering($pdf, $invitation->company);
if($numbered_pdf)
$pdf = $numbered_pdf;
if(!Storage::disk(config('filesystems.default'))->exists($path))
Storage::disk(config('filesystems.default'))->makeDirectory($path, 0775);

View File

@ -20,14 +20,11 @@ class PDF extends FPDI
function Footer()
{
$this->SetXY(0, -5);
$this->SetFont('Arial','I', 9);
$this->SetTextColor(135,135,135);
$trans = ctrans('texts.pdf_page_info', ['current' => $this->PageNo(), 'total' => '{nb}']);
$this->Cell(0,5, $trans ,0,0, $this->text_alignment);
}

View File

@ -17,7 +17,10 @@ trait PageNumbering
{
private function pageNumbering($pdf_data_object, $company)
{
// if(!$company->settings->page_numbering)
// return $pdf_data_object;
try
{
$pdf = new PDF();
@ -39,9 +42,8 @@ trait PageNumbering
$pdf->useTemplate($tplId);
}
ob_end_flush();
return $pdf->Output();
return $pdf->Output('S');
}
catch(\Exception $e) {