-
composer.json
Open in GitHub{ "name": "akaunting/akaunting", // ... "require": { "php": "^7.3.0", "ext-bcmath": "*", "akaunting/laravel-firewall": "^1.2", // ... "barryvdh/laravel-dompdf": "0.*", // ... }, }
-
app/Http/Controllers/Portal/Invoices.php
Open in GitHub// Controller method to download the PDF invoice // It builds the PDF on the fly from the Blade View // Trait "Documents" is important, see its content below use App\Traits\Documents; class Invoices extends Controller { use DateTime, Currencies, Documents, Uploads; public function pdfInvoice(Document $invoice, Request $request) { $view = view($invoice->template_path, compact('invoice', 'currency_style'))->render(); $html = mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8'); $pdf = app('dompdf.wrapper'); $pdf->loadHTML($html); $file_name = $this->getDocumentFileName($invoice); return $pdf->download($file_name); } }
-
app/Traits/Documents.php
Open in GitHubnamespace App\Traits; trait Documents { // ... other methods public function getDocumentFileName(Document $document, string $separator = '-', string $extension = 'pdf'): string { return $this->getSafeDocumentNumber($document, $separator) . $separator . time() . '.' . $extension; } public function getSafeDocumentNumber(Document $document, string $separator = '-'): string { return Str::slug($document->document_number, $separator, language()->getShortCode()); } }
-
routes/portal.php
Open in GitHubRoute::group(['as' => 'portal.'], function () { // ... other routes Route::get('invoices/{invoice}/pdf', 'Portal\Invoices@pdfInvoice')->name('invoices.pdf'); });