|
6 | 6 | // |
7 | 7 | package net.codecrete.qrbill.examples; |
8 | 8 |
|
| 9 | +import net.codecrete.qrbill.canvas.PDFCanvas; |
9 | 10 | import net.codecrete.qrbill.generator.*; |
10 | 11 |
|
11 | 12 | import java.io.IOException; |
| 13 | +import java.net.URISyntaxException; |
12 | 14 | import java.nio.file.Files; |
13 | 15 | import java.nio.file.Path; |
14 | 16 | import java.nio.file.Paths; |
|
22 | 24 | */ |
23 | 25 | public class QRBillExample { |
24 | 26 |
|
25 | | - public static void main(String[] args) { |
| 27 | + public static void main(String[] args) throws IOException, URISyntaxException { |
26 | 28 |
|
27 | 29 | // Setup bill |
28 | 30 | Bill bill = new Bill(); |
@@ -57,18 +59,30 @@ public static void main(String[] args) { |
57 | 59 | format.setLanguage(Language.DE); |
58 | 60 | bill.setFormat(format); |
59 | 61 |
|
60 | | - // Generate QR bill |
| 62 | + // Generate new PDF with QR bill |
61 | 63 | byte[] svg = QRBill.generate(bill); |
| 64 | + Path path0 = Paths.get("invoice-0.pdf"); |
| 65 | + Files.write(path0, svg); |
| 66 | + System.out.println("QR bill saved at " + path0.toAbsolutePath()); |
62 | 67 |
|
63 | | - // Save QR bill |
64 | | - Path path = Paths.get("qrbill.pdf"); |
65 | | - try { |
66 | | - Files.write(path, svg); |
67 | | - } catch (IOException e) { |
68 | | - throw new RuntimeException(e); |
| 68 | + // Load existing PDF |
| 69 | + Path invoiceWithoutQRBill = Paths.get(QRBillExample.class.getResource("/invoice.pdf").toURI()); |
| 70 | + |
| 71 | + // Add QR bill to last page |
| 72 | + try (PDFCanvas canvas = new PDFCanvas(invoiceWithoutQRBill, PDFCanvas.LAST_PAGE)) { |
| 73 | + QRBill.draw(bill, canvas); |
| 74 | + Path path = Paths.get("invoice-1.pdf"); |
| 75 | + canvas.saveAs(path); |
| 76 | + System.out.println("Invoice 1 saved at " + path.toAbsolutePath()); |
69 | 77 | } |
70 | 78 |
|
71 | | - System.out.println("QR bill saved at " + path.toAbsolutePath()); |
| 79 | + // Append QR bill to a new page |
| 80 | + try (PDFCanvas canvas = new PDFCanvas(invoiceWithoutQRBill, PDFCanvas.NEW_PAGE_AT_END)) { |
| 81 | + QRBill.draw(bill, canvas); |
| 82 | + Path path = Paths.get("invoice-2.pdf"); |
| 83 | + canvas.saveAs(path); |
| 84 | + System.out.println("Invoice 2 saved at " + path.toAbsolutePath()); |
| 85 | + } |
72 | 86 |
|
73 | 87 | System.out.println("Generated with version " + QRBill.getLibraryVersion()); |
74 | 88 | } |
|
0 commit comments