From e30053424aba49d1be64f1fac4d305d8e1ba4e80 Mon Sep 17 00:00:00 2001 From: Daniel Stock Date: Tue, 28 Oct 2025 10:21:40 +0100 Subject: [PATCH] Add page numbers to PDFs, fixes #39 --- app/Http/Controllers/InvoiceController.php | 33 +++++++++++++++++----- resources/views/invoice.blade.php | 5 +++- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index d8de5c4..ad6f445 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -76,24 +76,43 @@ public function preview($id) 'isPDF' => false, 'fontPath' => '/storage/fonts', 'giroCode' => $giroCode, + 'totalPages' => 1 ]); } + + // https://www.itsolutionstuff.com/post/laravel-dompdf-table-with-page-break-exampleexample.html public function exportPdf($id) { $invoice = self::single($id); $giroCode = $this->generateGiroCode($invoice['totalAmount'], $invoice['nr'], $invoice['title']); - $pdf = Pdf::loadView('invoice', [ + $viewData = [ 'invoice' => $invoice, 'isPDF' => true, 'fontPath' => public_path('storage/fonts/'), 'giroCode' => $giroCode, - ]); - $pdf->setOption(['isRemoteEnabled' => true]); - $pdf->setOption(['fontDir' => public_path('storage/fonts/')]); - $pdf->setOption(['fontCache' => public_path('storage/fonts/')]); + 'totalPages' => 1 + ]; + + $options = [ + 'isRemoteEnabled' => true, + 'fontDir' => public_path('storage/fonts/'), + 'fontCache' => public_path('storage/fonts/') + ]; + + $pdf = Pdf::loadView('invoice', $viewData); + $pdf->setOptions($options, true); + $pdf->render(); + + // if there’s more than one page, inject page number an render again + // https://github.com/dompdf/dompdf/issues/1636 + $viewData['totalPages'] = $pdf->getCanvas()->get_page_count(); + if ($viewData['totalPages'] > 1) { + $pdf = Pdf::loadView('invoice', $viewData); + $pdf->setOptions($options, true); + } return $pdf->stream($invoice['nr'] . '.pdf'); // return $pdf->download($this->getFilename($invoice) . '.pdf'); @@ -265,14 +284,14 @@ public function exportXml($id) // string|null $chargeFreeQuantityUnitCpde __BT-X-46-0, From EXTENDED__ Unit of measure code for the quantity free of charge // float|null $packageQuantity __BT-X-47, From EXTENDED__ Number of packages // string|null $packageQuantityUnitCode __BT-X-47-0, From EXTENDED__ Unit of measure code for number of packages - + // TODO: this needs to be properly mapped. It should probably become a database item, the user can manage $units = [ 'Stück' => ZugferdUnitCodes::REC20_PIECE, 'Stunden' => ZugferdUnitCodes::REC20_HOUR, 'Tage' => ZugferdUnitCodes::REC20_WORKING_DAY, 'pauschal' => ZugferdUnitCodes::REC20_LUMP_SUM - ]; + ]; $document->setDocumentPositionQuantity( $item['quantity'], $units[$item['unit']] ?? ZugferdUnitCodes::REC20_PIECE diff --git a/resources/views/invoice.blade.php b/resources/views/invoice.blade.php index 3c2e233..855f01a 100644 --- a/resources/views/invoice.blade.php +++ b/resources/views/invoice.blade.php @@ -43,7 +43,6 @@ - + + Seite / {{ $totalPages }} + + \ No newline at end of file