Added example how to combine existing PDF and invoice XML

This commit is contained in:
dankito 2024-11-19 23:49:39 +01:00
parent ddd6e01f1f
commit c64b6e7c77
3 changed files with 29 additions and 3 deletions

View File

@ -67,4 +67,16 @@ private fun createInvoice() = Invoice(
recipient = Party("Abzock GmbH", "Ausbeutstr.", "12345", "Abzockhausen"),
items = listOf(LineItem("Erbrachte Dienstleistungen", "HUR", BigDecimal(170), BigDecimal(1_000_000), BigDecimal(19))) // HUR = EN code for hour
)
```
### Attach invoice XML to existing PDF
```kotlin
val invoice: Invoice = createInvoice()
val existingPdf = File("Invoice.pdf")
val output = File("Zugferd.pdf")
val creator = EInvoiceCreator()
creator.combinePdfAndInvoiceXml(invoice, existingPdf, output)
```

View File

@ -32,13 +32,16 @@ class EInvoiceCreator(
val visualizer = ZUGFeRDVisualizer()
visualizer.toPDF(xmlFile.absolutePath, pdfFile.absolutePath)
combinePdfAndXml(pdfFile, xml, outputFile)
combinePdfAndInvoiceXml(xml, pdfFile, outputFile)
xmlFile.delete()
pdfFile.delete()
}
fun combinePdfAndXml(pdfFile: File, xml: String, outputFile: File) {
fun combinePdfAndInvoiceXml(invoice: Invoice, pdfFile: File, outputFile: File) =
combinePdfAndInvoiceXml(createZugferdXml(invoice), pdfFile, outputFile)
fun combinePdfAndInvoiceXml(invoiceXml: String, pdfFile: File, outputFile: File) {
val exporter = ZUGFeRDExporterFromA3()
.setZUGFeRDVersion(2)
.setProfile("EN16931") // available values: MINIMUM, BASICWL, BASIC, CIUS, EN16931, EXTENDED, XRECHNUNG
@ -47,7 +50,7 @@ class EInvoiceCreator(
.setCreator(System.getProperty("user.name"))
exporter.load(pdfFile.inputStream())
exporter.setXML(xml.toByteArray())
exporter.setXML(invoiceXml.toByteArray())
exporter.export(outputFile.outputStream())
}

View File

@ -62,6 +62,17 @@ class Demonstration {
val xRechnung = creator.createXRechnungXml(invoice)
}
fun combinePdfAndInvoiceXml() {
val invoice: Invoice = createInvoice()
val existingPdf = File("Invoice.pdf")
val output = File("Zugferd.pdf")
val creator = EInvoiceCreator()
creator.combinePdfAndInvoiceXml(invoice, existingPdf, output)
}
private fun createInvoice() = Invoice(
invoiceNumber = "RE-00001",
invoicingDate = LocalDate.now(),