Add page numbers to PDFs, fixes #39

This commit is contained in:
2025-10-28 10:21:40 +01:00
parent ab3f8409ee
commit d390eb9bd4
2 changed files with 30 additions and 8 deletions
+26 -7
View File
@@ -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 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->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