Add page numbers to PDFs, fixes #39

This commit is contained in:
2025-10-28 10:21:40 +01:00
parent 51cfa0e830
commit e30053424a
2 changed files with 30 additions and 8 deletions
+26 -7
View File
@@ -76,24 +76,43 @@ public function preview($id)
'isPDF' => false, 'isPDF' => false,
'fontPath' => '/storage/fonts', 'fontPath' => '/storage/fonts',
'giroCode' => $giroCode, 'giroCode' => $giroCode,
'totalPages' => 1
]); ]);
} }
// https://www.itsolutionstuff.com/post/laravel-dompdf-table-with-page-break-exampleexample.html // https://www.itsolutionstuff.com/post/laravel-dompdf-table-with-page-break-exampleexample.html
public function exportPdf($id) public function exportPdf($id)
{ {
$invoice = self::single($id); $invoice = self::single($id);
$giroCode = $this->generateGiroCode($invoice['totalAmount'], $invoice['nr'], $invoice['title']); $giroCode = $this->generateGiroCode($invoice['totalAmount'], $invoice['nr'], $invoice['title']);
$pdf = Pdf::loadView('invoice', [ $viewData = [
'invoice' => $invoice, 'invoice' => $invoice,
'isPDF' => true, 'isPDF' => true,
'fontPath' => public_path('storage/fonts/'), 'fontPath' => public_path('storage/fonts/'),
'giroCode' => $giroCode, 'giroCode' => $giroCode,
]); 'totalPages' => 1
$pdf->setOption(['isRemoteEnabled' => true]); ];
$pdf->setOption(['fontDir' => public_path('storage/fonts/')]);
$pdf->setOption(['fontCache' => public_path('storage/fonts/')]); $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 theres 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->stream($invoice['nr'] . '.pdf');
// return $pdf->download($this->getFilename($invoice) . '.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 // 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 // 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 // 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 // TODO: this needs to be properly mapped. It should probably become a database item, the user can manage
$units = [ $units = [
'Stück' => ZugferdUnitCodes::REC20_PIECE, 'Stück' => ZugferdUnitCodes::REC20_PIECE,
'Stunden' => ZugferdUnitCodes::REC20_HOUR, 'Stunden' => ZugferdUnitCodes::REC20_HOUR,
'Tage' => ZugferdUnitCodes::REC20_WORKING_DAY, 'Tage' => ZugferdUnitCodes::REC20_WORKING_DAY,
'pauschal' => ZugferdUnitCodes::REC20_LUMP_SUM 'pauschal' => ZugferdUnitCodes::REC20_LUMP_SUM
]; ];
$document->setDocumentPositionQuantity( $document->setDocumentPositionQuantity(
$item['quantity'], $item['quantity'],
$units[$item['unit']] ?? ZugferdUnitCodes::REC20_PIECE $units[$item['unit']] ?? ZugferdUnitCodes::REC20_PIECE
+4 -1
View File
@@ -43,7 +43,6 @@
</head> </head>
<body> <body>
<!-- Fixed page elements need to bee direct descendents of body for Dompdf --> <!-- Fixed page elements need to bee direct descendents of body for Dompdf -->
<!-- see https://github.com/dompdf/dompdf/issues/1190 --> <!-- see https://github.com/dompdf/dompdf/issues/1190 -->
<img id="logo" <img id="logo"
@@ -201,6 +200,10 @@
Stadtsparkasse Augsburg, DE40 7205 0000 0251 5125 13, BIC AUGSDE77XXX, Ust-IdNr. DE309558315 Stadtsparkasse Augsburg, DE40 7205 0000 0251 5125 13, BIC AUGSDE77XXX, Ust-IdNr. DE309558315
</footer> </footer>
<pagination>
Seite <span class="page-number"></span> / {{ $totalPages }}
</pagination>
</body> </body>
</html> </html>