Implemented createZugferdPdf() using the standard template of Mustang for PDF file

This commit is contained in:
dankito 2024-11-15 00:34:00 +01:00
parent 148fc91b58
commit c94b9f8ece
2 changed files with 48 additions and 8 deletions

View File

@ -1,10 +1,9 @@
package net.codinux.invoicing.creation package net.codinux.invoicing.creation
import net.codinux.invoicing.mapper.MustangMapper
import net.codinux.invoicing.model.Invoice import net.codinux.invoicing.model.Invoice
import org.mustangproject.ZUGFeRD.IXMLProvider import org.mustangproject.ZUGFeRD.*
import org.mustangproject.ZUGFeRD.Profiles import java.io.File
import org.mustangproject.ZUGFeRD.ZUGFeRD2PullProvider
import org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromA3
class EInvoiceCreator( class EInvoiceCreator(
private val mapper: MustangMapper = MustangMapper() private val mapper: MustangMapper = MustangMapper()
@ -17,14 +16,40 @@ class EInvoiceCreator(
return createXml(provider, invoice) return createXml(provider, invoice)
} }
fun createZugferdXml(invoice: Invoice, zugferdVersion: Int = 2): String { fun createZugferdXml(invoice: Invoice): String {
val exporter = ZUGFeRDExporterFromA3() val exporter = ZUGFeRDExporterFromA3()
.setZUGFeRDVersion(zugferdVersion) .setProfile("EN16931") // required for XML?
.setProfile("EN16931")
return createXml(exporter.provider, invoice)
}
fun createZugferdPdf(invoice: Invoice, outputFile: File) {
val xml = createZugferdXml(invoice)
val xmlFile = File.createTempFile(outputFile.nameWithoutExtension, ".xml")
.also { it.writeText(xml) }
val pdfFile = File(xmlFile.parentFile, xmlFile.nameWithoutExtension + ".pdf")
val visualizer = ZUGFeRDVisualizer()
visualizer.toPDF(xmlFile.absolutePath, pdfFile.absolutePath)
combinePdfAndXml(pdfFile, xml, outputFile)
xmlFile.delete()
pdfFile.delete()
}
fun combinePdfAndXml(pdfFile: File, xml: String, outputFile: File) {
val exporter = ZUGFeRDExporterFromA3()
.setZUGFeRDVersion(2)
.setProfile("EN16931") // available values: MINIMUM, BASICWL, BASIC, CIUS, EN16931, EXTENDED, XRECHNUNG
// .disableFacturX()
.setProducer("danki die geile Sau") .setProducer("danki die geile Sau")
.setCreator(System.getProperty("user.name")) .setCreator(System.getProperty("user.name"))
return createXml(exporter.provider, invoice) exporter.load(pdfFile.inputStream())
exporter.setXML(xml.toByteArray())
exporter.export(outputFile.outputStream())
} }

View File

@ -4,6 +4,8 @@ import assertk.assertThat
import assertk.assertions.isNotEmpty import assertk.assertions.isNotEmpty
import net.codinux.invoicing.test.DataGenerator import net.codinux.invoicing.test.DataGenerator
import net.codinux.invoicing.test.XPathAsserter import net.codinux.invoicing.test.XPathAsserter
import org.mustangproject.ZUGFeRD.ZUGFeRDInvoiceImporter
import java.io.File
import java.math.BigDecimal import java.math.BigDecimal
import kotlin.test.Test import kotlin.test.Test
@ -30,6 +32,19 @@ class EInvoiceCreatorTest {
assertInvoiceXml(result) assertInvoiceXml(result)
} }
@Test
fun createZugferdPdf() {
val testFile = File.createTempFile("Zugferd", ".pdf")
val invoice = createInvoice()
underTest.createZugferdPdf(invoice, testFile)
val importer = ZUGFeRDInvoiceImporter(testFile.inputStream())
val xml = String(importer.rawXML, Charsets.UTF_8)
assertInvoiceXml(xml)
}
private fun createInvoice() = DataGenerator.createInvoice() private fun createInvoice() = DataGenerator.createInvoice()