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
.structuredAddress()
.name("Pia-Maria Rutschmann-Schnyder")
.streetName("Grosse Marktgasse")
.houseNumber("28")
.postalCode("9400")
.city("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();
}
}
}