diff --git a/gradle.properties b/gradle.properties index a8fbcdc..df152ca 100644 --- a/gradle.properties +++ b/gradle.properties @@ -23,6 +23,9 @@ pdfboxTextExtractor=0.6.1 angusMailVersion=2.0.3 +openHtmlToPdfVersion=1.1.22 +jsoupVersion=1.18.1 + klfVersion=1.6.2 lokiLogAppenderVersion=0.5.5 # only used for tests diff --git a/invoice-creator/build.gradle.kts b/invoice-creator/build.gradle.kts new file mode 100644 index 0000000..6c55f73 --- /dev/null +++ b/invoice-creator/build.gradle.kts @@ -0,0 +1,49 @@ +plugins { + kotlin("jvm") +} + + +kotlin { + jvmToolchain(11) +} + +java { + withSourcesJar() +} + + +val openHtmlToPdfVersion: String by project + +val jsoupVersion: String by project + +val klfVersion: String by project + +val assertKVersion: String by project +val logbackVersion: String by project + +dependencies { + implementation("io.github.openhtmltopdf:openhtmltopdf-pdfbox:$openHtmlToPdfVersion") + implementation("io.github.openhtmltopdf:openhtmltopdf-slf4j:$openHtmlToPdfVersion") + + implementation("org.jsoup:jsoup:$jsoupVersion") + + implementation("net.codinux.log:klf:$klfVersion") + + + testImplementation(kotlin("test")) + + testImplementation("com.willowtreeapps.assertk:assertk:$assertKVersion") + + testImplementation("ch.qos.logback:logback-classic:$logbackVersion") +} + + +tasks.test { + useJUnitPlatform() +} + + + +ext["customArtifactId"] = "invoice-creator" + +apply(from = "../gradle/scripts/publish-codinux-repo.gradle.kts") \ No newline at end of file diff --git a/invoice-creator/src/main/kotlin/net/codinux/invoicing/pdf/OpenHtmlToPdfHtmlToPdfConverter.kt b/invoice-creator/src/main/kotlin/net/codinux/invoicing/pdf/OpenHtmlToPdfHtmlToPdfConverter.kt new file mode 100644 index 0000000..8176450 --- /dev/null +++ b/invoice-creator/src/main/kotlin/net/codinux/invoicing/pdf/OpenHtmlToPdfHtmlToPdfConverter.kt @@ -0,0 +1,40 @@ +package net.codinux.invoicing.pdf + +import com.openhtmltopdf.pdfboxout.PdfRendererBuilder +import org.jsoup.Jsoup +import org.jsoup.helper.W3CDom +import org.jsoup.nodes.Document +import java.io.File +import java.nio.file.FileSystems + + +open class OpenHtmlToPdfHtmlToPdfConverter { + + open fun renderHtml(htmlFile: File, outputFile: File) = + renderHtml(htmlFile.readText(), outputFile) + + open fun renderHtml(html: String, outputFile: File) { + val doc = createWellFormedHtml(html) + xhtmlToPdf(doc, outputFile) + } + + protected open fun xhtmlToPdf(doc: Document, outputPdf: File) { + outputPdf.outputStream().use { output -> + val baseUri = FileSystems.getDefault().getPath("/src/main/resources") + .toUri().toString() + + val builder = PdfRendererBuilder() + builder.useFastMode() + + builder.toStream(output) + builder.withW3cDocument(W3CDom().fromJsoup(doc), baseUri) + builder.run() + } + } + + protected open fun createWellFormedHtml(html: String): Document = + Jsoup.parse(html, Charsets.UTF_8.name()).apply { + this.outputSettings().syntax(Document.OutputSettings.Syntax.xml) + } + +} \ No newline at end of file diff --git a/invoice-creator/src/main/kotlin/net/codinux/invoicing/pdf/ResourceUtil.kt b/invoice-creator/src/main/kotlin/net/codinux/invoicing/pdf/ResourceUtil.kt new file mode 100644 index 0000000..a07a30a --- /dev/null +++ b/invoice-creator/src/main/kotlin/net/codinux/invoicing/pdf/ResourceUtil.kt @@ -0,0 +1,24 @@ +package net.codinux.invoicing.pdf + +import java.io.InputStream +import java.net.URL + +object ResourceUtil { + + fun getResourceUrl(resourcePath: String): URL? = + javaClass.classLoader.getResource(resourcePath) + + fun getResourceAsStream(resourcePath: String): InputStream = + javaClass.classLoader.getResourceAsStream(resourcePath)!! + + fun getResourceBytes(resourcePath: String): ByteArray = + getResourceAsStream(resourcePath).use { + it.readBytes() + } + + fun getResourceAsText(resourcePath: String): String = + getResourceAsStream(resourcePath).use { inputStream -> + inputStream.reader().use { it.readText() } + } + +} \ No newline at end of file diff --git a/invoice-creator/src/test/resources/logback-test.xml b/invoice-creator/src/test/resources/logback-test.xml new file mode 100644 index 0000000..3a5e539 --- /dev/null +++ b/invoice-creator/src/test/resources/logback-test.xml @@ -0,0 +1,32 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + DEBUG + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 71deb8f..63e3326 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -28,4 +28,6 @@ rootProject.name = "eInvoicing" include("e-invoice-domain") +include("invoice-creator") + include("e-invoice-api")