Skip to content

Commit

Permalink
Extend PDFBox 3 example
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Nov 12, 2023
1 parent 0f221a7 commit a4c878c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
4 changes: 3 additions & 1 deletion examples/pdfbox3/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/qrbill.pdf
/invoice-0.pdf
/invoice-1.pdf
/invoice-2.pdf
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
//
package net.codecrete.qrbill.examples;

import net.codecrete.qrbill.canvas.PDFCanvas;
import net.codecrete.qrbill.generator.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -22,7 +24,7 @@
*/
public class QRBillExample {

public static void main(String[] args) {
public static void main(String[] args) throws IOException, URISyntaxException {

// Setup bill
Bill bill = new Bill();
Expand Down Expand Up @@ -57,18 +59,30 @@ public static void main(String[] args) {
format.setLanguage(Language.DE);
bill.setFormat(format);

// Generate QR bill
// Generate new PDF with QR bill
byte[] svg = QRBill.generate(bill);
Path path0 = Paths.get("invoice-0.pdf");
Files.write(path0, svg);
System.out.println("QR bill saved at " + path0.toAbsolutePath());

// Save QR bill
Path path = Paths.get("qrbill.pdf");
try {
Files.write(path, svg);
} catch (IOException e) {
throw new RuntimeException(e);
// Load existing PDF
Path invoiceWithoutQRBill = Paths.get(QRBillExample.class.getResource("/invoice.pdf").toURI());

// Add QR bill to last page
try (PDFCanvas canvas = new PDFCanvas(invoiceWithoutQRBill, PDFCanvas.LAST_PAGE)) {
QRBill.draw(bill, canvas);
Path path = Paths.get("invoice-1.pdf");
canvas.saveAs(path);
System.out.println("Invoice 1 saved at " + path.toAbsolutePath());
}

System.out.println("QR bill saved at " + path.toAbsolutePath());
// Append QR bill to a new page
try (PDFCanvas canvas = new PDFCanvas(invoiceWithoutQRBill, PDFCanvas.NEW_PAGE_AT_END)) {
QRBill.draw(bill, canvas);
Path path = Paths.get("invoice-2.pdf");
canvas.saveAs(path);
System.out.println("Invoice 2 saved at " + path.toAbsolutePath());
}

System.out.println("Generated with version " + QRBill.getLibraryVersion());
}
Expand Down
Binary file not shown.

0 comments on commit a4c878c

Please sign in to comment.