mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-19 11:14:06 +00:00
Consolidate PDF classes under app/Services/Pdf with consistent naming
Split PDFService.php (3 classes + 2 interfaces in one file) into separate files. Move GotenbergPDFDriver from app/Services/PDFDrivers/ into app/Services/Pdf/. Normalize casing from ALL-CAPS PDF to Pdf throughout: facade, provider, service, driver factory, and Gotenberg driver. Fix PaymentService using Barryvdh DomPDF facade directly instead of the app's PDF facade (bypassed the driver factory). Report controllers also updated to use the app facade.
This commit is contained in:
@@ -4,7 +4,7 @@ namespace App\Services;
|
||||
|
||||
use App;
|
||||
use App\Facades\Hashids;
|
||||
use App\Facades\PDF;
|
||||
use App\Facades\Pdf;
|
||||
use App\Mail\SendEstimateMail;
|
||||
use App\Models\Company;
|
||||
use App\Models\CompanySetting;
|
||||
@@ -197,6 +197,6 @@ class EstimateService
|
||||
return view($templatePath);
|
||||
}
|
||||
|
||||
return PDF::loadView($templatePath);
|
||||
return Pdf::loadView($templatePath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace App\Services;
|
||||
|
||||
use App;
|
||||
use App\Facades\Hashids;
|
||||
use App\Facades\PDF;
|
||||
use App\Facades\Pdf;
|
||||
use App\Mail\SendInvoiceMail;
|
||||
use App\Models\Company;
|
||||
use App\Models\CompanySetting;
|
||||
@@ -262,6 +262,6 @@ class InvoiceService
|
||||
return view($templatePath);
|
||||
}
|
||||
|
||||
return PDF::loadView($templatePath);
|
||||
return Pdf::loadView($templatePath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\PDFDrivers;
|
||||
|
||||
use Gotenberg\Gotenberg;
|
||||
use Gotenberg\Stream;
|
||||
use Illuminate\Http\Response;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class GotenbergPDFResponse
|
||||
{
|
||||
/** @var ResponseInterface */
|
||||
protected $response;
|
||||
|
||||
public function __construct($stream)
|
||||
{
|
||||
$this->response = $stream;
|
||||
}
|
||||
|
||||
public function stream(string $filename = 'document.pdf'): Response
|
||||
{
|
||||
$output = $this->response->getBody();
|
||||
|
||||
return new Response($output, 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="'.$filename.'"',
|
||||
]);
|
||||
}
|
||||
|
||||
public function output(): string
|
||||
{
|
||||
return $this->response->getBody()->getContents();
|
||||
}
|
||||
}
|
||||
|
||||
class GotenbergPDFDriver
|
||||
{
|
||||
public function loadView(string $viewname): GotenbergPDFResponse
|
||||
{
|
||||
$papersize = explode(' ', config('pdf.connections.gotenberg.papersize'));
|
||||
if (count($papersize) != 2) {
|
||||
throw new \InvalidArgumentException('Invalid Gotenberg Papersize specified');
|
||||
}
|
||||
|
||||
$host = config('pdf.connections.gotenberg.host');
|
||||
$request = Gotenberg::chromium($host)
|
||||
->pdf()
|
||||
->margins(0, 0, 0, 0) // Margins can be set using CSS
|
||||
->paperSize($papersize[0], $papersize[1])
|
||||
->html(
|
||||
Stream::string(
|
||||
'document.html',
|
||||
view($viewname)->render(),
|
||||
)
|
||||
);
|
||||
$result = Gotenberg::send($request);
|
||||
|
||||
return new GotenbergPDFResponse($result);
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
/*
|
||||
* Two options:
|
||||
* - Barryvdh\DomPDF\Facade\Pdf
|
||||
* - Gotenberg
|
||||
*/
|
||||
|
||||
use App;
|
||||
use App\Services\PDFDrivers\GotenbergPDFDriver;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
interface ResponseStream
|
||||
{
|
||||
public function stream(string $filename): Response;
|
||||
|
||||
public function output(): string;
|
||||
}
|
||||
|
||||
interface PDFDriver
|
||||
{
|
||||
public function loadView(string $template): ResponseStream;
|
||||
}
|
||||
|
||||
class PDFDriverFactory
|
||||
{
|
||||
public static function create(string $driver)
|
||||
{
|
||||
return match ($driver) {
|
||||
'dompdf' => App::make('dompdf.wrapper'),
|
||||
'gotenberg' => new GotenbergPDFDriver,
|
||||
default => throw new \InvalidArgumentException('Invalid PDFDriver requested')
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class PDFService
|
||||
{
|
||||
public static function loadView(string $template)
|
||||
{
|
||||
$driver = config('pdf.driver');
|
||||
|
||||
return PDFDriverFactory::create($driver)->loadView($template);
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,13 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Facades\Hashids;
|
||||
use App\Facades\Pdf;
|
||||
use App\Mail\SendPaymentMail;
|
||||
use App\Models\Company;
|
||||
use App\Models\CompanySetting;
|
||||
use App\Models\ExchangeRateLog;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Payment;
|
||||
use Barryvdh\DomPDF\Facade\Pdf as PDF;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -184,7 +184,7 @@ class PaymentService
|
||||
return view('app.pdf.payment.payment');
|
||||
}
|
||||
|
||||
return PDF::loadView('app.pdf.payment.payment');
|
||||
return Pdf::loadView('app.pdf.payment.payment');
|
||||
}
|
||||
|
||||
public function generateFromTransaction($transaction): Payment
|
||||
|
||||
32
app/Services/Pdf/GotenbergPdfDriver.php
Normal file
32
app/Services/Pdf/GotenbergPdfDriver.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Pdf;
|
||||
|
||||
use Gotenberg\Gotenberg;
|
||||
use Gotenberg\Stream;
|
||||
|
||||
class GotenbergPdfDriver
|
||||
{
|
||||
public function loadView(string $viewname): GotenbergPdfResponse
|
||||
{
|
||||
$papersize = explode(' ', config('pdf.connections.gotenberg.papersize'));
|
||||
if (count($papersize) != 2) {
|
||||
throw new \InvalidArgumentException('Invalid Gotenberg Papersize specified');
|
||||
}
|
||||
|
||||
$host = config('pdf.connections.gotenberg.host');
|
||||
$request = Gotenberg::chromium($host)
|
||||
->pdf()
|
||||
->margins(0, 0, 0, 0)
|
||||
->paperSize($papersize[0], $papersize[1])
|
||||
->html(
|
||||
Stream::string(
|
||||
'document.html',
|
||||
view($viewname)->render(),
|
||||
)
|
||||
);
|
||||
$result = Gotenberg::send($request);
|
||||
|
||||
return new GotenbergPdfResponse($result);
|
||||
}
|
||||
}
|
||||
31
app/Services/Pdf/GotenbergPdfResponse.php
Normal file
31
app/Services/Pdf/GotenbergPdfResponse.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Pdf;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class GotenbergPdfResponse
|
||||
{
|
||||
protected ResponseInterface $response;
|
||||
|
||||
public function __construct($stream)
|
||||
{
|
||||
$this->response = $stream;
|
||||
}
|
||||
|
||||
public function stream(string $filename = 'document.pdf'): Response
|
||||
{
|
||||
$output = $this->response->getBody();
|
||||
|
||||
return new Response($output, 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="'.$filename.'"',
|
||||
]);
|
||||
}
|
||||
|
||||
public function output(): string
|
||||
{
|
||||
return $this->response->getBody()->getContents();
|
||||
}
|
||||
}
|
||||
8
app/Services/Pdf/PdfDriver.php
Normal file
8
app/Services/Pdf/PdfDriver.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Pdf;
|
||||
|
||||
interface PdfDriver
|
||||
{
|
||||
public function loadView(string $template): ResponseStream;
|
||||
}
|
||||
17
app/Services/Pdf/PdfDriverFactory.php
Normal file
17
app/Services/Pdf/PdfDriverFactory.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Pdf;
|
||||
|
||||
use App;
|
||||
|
||||
class PdfDriverFactory
|
||||
{
|
||||
public static function create(string $driver)
|
||||
{
|
||||
return match ($driver) {
|
||||
'dompdf' => App::make('dompdf.wrapper'),
|
||||
'gotenberg' => new GotenbergPdfDriver,
|
||||
default => throw new \InvalidArgumentException('Invalid PdfDriver requested')
|
||||
};
|
||||
}
|
||||
}
|
||||
13
app/Services/Pdf/PdfService.php
Normal file
13
app/Services/Pdf/PdfService.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Pdf;
|
||||
|
||||
class PdfService
|
||||
{
|
||||
public static function loadView(string $template)
|
||||
{
|
||||
$driver = config('pdf.driver');
|
||||
|
||||
return PdfDriverFactory::create($driver)->loadView($template);
|
||||
}
|
||||
}
|
||||
12
app/Services/Pdf/ResponseStream.php
Normal file
12
app/Services/Pdf/ResponseStream.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Pdf;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
interface ResponseStream
|
||||
{
|
||||
public function stream(string $filename): Response;
|
||||
|
||||
public function output(): string;
|
||||
}
|
||||
Reference in New Issue
Block a user