From c64b6e7c77b8ac951ec998c9341f4baea83c9722 Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 19 Nov 2024 23:49:39 +0100 Subject: [PATCH] Added example how to combine existing PDF and invoice XML --- README.md | 12 ++++++++++++ .../codinux/invoicing/creation/EInvoiceCreator.kt | 9 ++++++--- .../kotlin/net/codinux/invoicing/Demonstration.kt | 11 +++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0e81229..ed39139 100644 --- a/README.md +++ b/README.md @@ -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) ``` \ No newline at end of file diff --git a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt index 4d9e117..0cafae8 100644 --- a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt +++ b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/creation/EInvoiceCreator.kt @@ -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()) } diff --git a/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/Demonstration.kt b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/Demonstration.kt index 8c276fa..69a3424 100644 --- a/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/Demonstration.kt +++ b/e-invoicing-domain/src/test/kotlin/net/codinux/invoicing/Demonstration.kt @@ -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(),