This example will walk through a typical QR bill generation into a PDF. It will not show all available options.

Read the Basics document first to understand the terms of a QR bill.

Generating the QR bill

This code will generate the payment part of an example invoice and store it in paymentPart.pdf.

import ch.codeblock.qrinvoice.FontFamily;
import ch.codeblock.qrinvoice.OutputFormat;
import ch.codeblock.qrinvoice.PageSize;
import ch.codeblock.qrinvoice.QrInvoicePaymentPartReceiptCreator;
import ch.codeblock.qrinvoice.model.QrInvoice;
import ch.codeblock.qrinvoice.model.builder.QrInvoiceBuilder;
import ch.codeblock.qrinvoice.model.validation.ValidationException;
import ch.codeblock.qrinvoice.model.validation.ValidationResult;
import ch.codeblock.qrinvoice.output.PaymentPartReceipt;

import java.io.FileOutputStream;
import java.util.Locale;

public class QrInvoiceBuilderTypicalExample {
    public static void main(String[] args) {
        // 1. Build the invoice object
        QrInvoice qrInvoice = buildInvoice();

        // 2. Generate the payment part as PDF
        byte[] qrBillPdf = createQrBill(qrInvoice);

        // 3. Write the PDF
        writePdf(qrBillPdf);
    }

    private static QrInvoice buildInvoice() {
        QrInvoice qrInvoice = null;

        try {
            qrInvoice = QrInvoiceBuilder
                    .create()
                    .creditorIBAN("CH44 3199 9123 0008 8901 2")
                    .paymentAmountInformation(p -> p
                            .chf(1949.75)
                    )
                    .creditor(c -> c
                            .structuredAddress()
                            .name("Robert Schneider AG")
                            .streetName("Rue du Lac")
                            .houseNumber("1268")
                            .postalCode("2501")
                            .city("Biel")
                            .country("CH")
                    )
                    .ultimateDebtor(d -> d
                            .combinedAddress() // Unstructured method to provide a postal address
                            .name("Pia-Maria Rutschmann-Schnyder")
                            .addressLine1("Grosse Marktgasse 28")
                            .addressLine2("9400 Rorschach")
                            .country("CH")
                    )
                    .paymentReference(r -> r
                            .qrReference("210000000003139471430009017")
                    )
                    .additionalInformation(a -> a
                            .unstructuredMessage("Instruction of 03.04.2019") // Max. 140 characters, no linebreaks
                    )
                    .build();
        } catch (ValidationException validationException) {
            final ValidationResult validationResult = validationException.getValidationResult();

            System.out.println(validationResult.getValidationErrorSummary());
            System.exit(1);
        }

        return qrInvoice;
    }

    private static byte[] createQrBill(QrInvoice qrInvoice) {
        final PaymentPartReceipt paymentPartReceipt = QrInvoicePaymentPartReceiptCreator
                .create()
                .qrInvoice(qrInvoice)
                .outputFormat(OutputFormat.PDF)
                .pageSize(PageSize.A4) // Use DIN_LANG to generate a A6 page with the payment part only
                .fontFamily(FontFamily.LIBERATION_SANS) // or HELVETICA, ARIAL
                .locale(Locale.GERMAN) // Supported: GERMAN, FRENCH, ENGLISH, ITALIAN
                .createPaymentPartReceipt();

        // the resulting byte array contains the payment part & receipt as PDF
        return paymentPartReceipt.getData();
    }

    private static void writePdf(byte[] qrBillPdf) {
        try (FileOutputStream stream = new FileOutputStream("paymentPart.pdf")) {
            stream.write(qrBillPdf);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Combining the Payment Part with your invoice

The QrPdfMerger library provides some utility methods to handle PDF documents.

Assuming the top part of your QR bill is stored in invoice.pdf and the payment part in paymentPart.pdf, the following code will combine those PDFs together into qrBill.pdf:

import ch.codeblock.qrinvoice.pdf.QrPdfMerger;

import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

public class CombinePdfs {
    public static void main(String[] args) {
        try {
            // 1. Read in invoice part
            byte[] invoice = Files.readAllBytes(Paths.get("invoice.pdf"));

            // 2. Read in payment part (Generated by QrInvoiceBuilderTypicalExample.java)
            byte[] paymentPart = Files.readAllBytes(Paths.get("paymentPart.pdf"));

            // 3. Merge files
            byte[] qrBill = QrPdfMerger.create().mergePdfs(paymentPart, invoice);

            // 4. Write merged QR bill
            FileOutputStream qrBillFile = new FileOutputStream("qrBill.pdf");
            qrBillFile.write(qrBill);
            qrBillFile.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}