Implemented convertInvoiceToHtml()

This commit is contained in:
dankito 2024-11-15 01:24:38 +01:00
parent 658c02296a
commit 1a967efcbd
2 changed files with 40 additions and 1 deletions

View File

@ -52,6 +52,33 @@ class EInvoiceCreator(
exporter.export(outputFile.outputStream())
}
// extract to EInvoiceConverter?
fun convertInvoiceToHtml(invoice: Invoice, outputFile: File, language: ZUGFeRDVisualizer.Language = ZUGFeRDVisualizer.Language.DE) =
convertInvoiceToHtml(createXRechnungXml(invoice), outputFile, language)
fun convertInvoiceToHtml(invoiceXml: String, outputFile: File, language: ZUGFeRDVisualizer.Language = ZUGFeRDVisualizer.Language.DE): String {
val xmlFile = File.createTempFile("Zugferd", ".xml")
.also { it.writeText(invoiceXml) }
val visualizer = ZUGFeRDVisualizer()
val html = visualizer.visualize(xmlFile.absolutePath, language)
outputFile.writeText(html)
copyResource("xrechnung-viewer.css", outputFile, ".css")
copyResource("xrechnung-viewer.js", outputFile, ".js")
xmlFile.delete()
return html
}
private fun copyResource(resourceName: String, outputFile: File, outputFileExtension: String) {
javaClass.classLoader.getResourceAsStream(resourceName).use {
it?.copyTo(File(outputFile.parentFile, outputFile.nameWithoutExtension + outputFileExtension).outputStream())
}
}
private fun createXml(provider: IXMLProvider, invoice: Invoice): String {
val transaction = mapper.mapToTransaction(invoice)

View File

@ -1,5 +1,7 @@
package net.codinux.invoicing.creation
import assertk.assertThat
import assertk.assertions.isNotEmpty
import net.codinux.invoicing.test.DataGenerator
import net.codinux.invoicing.test.InvoiceAsserter
import org.mustangproject.ZUGFeRD.ZUGFeRDInvoiceImporter
@ -31,8 +33,8 @@ class EInvoiceCreatorTest {
@Test
fun createZugferdPdf() {
val testFile = File.createTempFile("Zugferd", ".pdf")
val invoice = createInvoice()
val testFile = File.createTempFile("Zugferd", ".pdf")
underTest.createZugferdPdf(invoice, testFile)
@ -42,6 +44,16 @@ class EInvoiceCreatorTest {
assertInvoiceXml(xml)
}
@Test
fun convertInvoiceToHtml() {
val invoice = createInvoice()
val testFile = File.createTempFile("Zugferd", ".html")
val result = underTest.convertInvoiceToHtml(invoice, testFile)
assertThat(result).isNotEmpty()
}
private fun createInvoice() = DataGenerator.createInvoice()