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
+24 -5
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');
+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>