-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZugferdFlow.java
More file actions
96 lines (83 loc) · 3.24 KB
/
Copy pathZugferdFlow.java
File metadata and controls
96 lines (83 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// End-to-end ZUGFeRD flows for a Spring Boot backend.
//
// Two ways to produce a ZUGFeRD PDF/A-3:
//
// 1. One call. The API renders the PDF and embeds the XML.
// createZugferd(json)
//
// 2. Two calls, when your system already renders the invoice PDF and that PDF is not
// reachable from the internet. Your PDF is uploaded directly, no public URL needed.
// createCiiXml(json) -> embedZugferd(pdf, xml)
//
// Requires Java 15+ for the text block. Change the package to your own.
package com.example.invoicexml;
import java.nio.file.Files;
import java.nio.file.Path;
public class ZugferdFlow {
private final InvoiceXmlClient api;
public ZugferdFlow(InvoiceXmlClient api) {
this.api = api;
}
/** Two-step flow: your own PDF keeps its exact layout, fonts and branding. */
public byte[] fromOwnPdf(String invoiceJson, byte[] ownPdf) {
byte[] cii = api.createCiiXml(invoiceJson);
return api.embedZugferd(ownPdf, cii);
}
/** One-step flow: the API renders the visual layer for you. */
public byte[] fromModelOnly(String invoiceJson) {
return api.createZugferd(invoiceJson);
}
// Map your own invoice model onto this shape. The full field reference is at
// https://www.invoicexml.com/docs/api/create/zugferd
public static final String EXAMPLE_JSON = """
{
"invoice": {
"invoiceNumber": "RE-2026-000123",
"issueDate": "2026-05-18",
"currency": "EUR",
"seller": {
"name": "Acme GmbH",
"vatIdentifier": "DE123456789",
"legalRegistration": { "identifier": "HRB 12345" },
"postalAddress": {
"line1": "Hauptstraße 12",
"city": "Berlin",
"postCode": "10115",
"country": "DE"
}
},
"buyer": {
"name": "Globex SAS",
"postalAddress": {
"line1": "15 rue de Rivoli",
"city": "Paris",
"postCode": "75001",
"country": "FR"
}
},
"paymentDetails": { "paymentAccountIdentifier": "DE89370400440532013000" },
"lines": [
{
"quantity": 10,
"priceDetails": { "netPrice": 150.00 },
"vatInformation": { "rate": 19.00 },
"item": { "name": "Senior consulting" }
}
]
}
}
""";
// Standalone runner. In a Spring Boot application you would inject InvoiceXmlClient
// from InvoiceXmlConfig instead of building one here.
public static void main(String[] args) throws Exception {
var api = new InvoiceXmlClient(
org.springframework.web.client.RestClient.builder(),
System.getenv("INVOICEXML_API_KEY"));
var flow = new ZugferdFlow(api);
byte[] ownPdf = Files.readAllBytes(Path.of("invoice.pdf"));
byte[] hybrid = flow.fromOwnPdf(EXAMPLE_JSON, ownPdf);
Files.write(Path.of("invoice-zugferd.pdf"), hybrid);
System.out.println("Saved invoice-zugferd.pdf (" + hybrid.length + " bytes)");
System.out.println(api.validateZugferd(hybrid));
}
}