Implemented OpenHtmlToPdfHtmlToPdfConverter to convert HTML to PDF
This commit is contained in:
parent
8c4614fe9c
commit
6a9ed9078e
|
@ -23,6 +23,9 @@ pdfboxTextExtractor=0.6.1
|
||||||
|
|
||||||
angusMailVersion=2.0.3
|
angusMailVersion=2.0.3
|
||||||
|
|
||||||
|
openHtmlToPdfVersion=1.1.22
|
||||||
|
jsoupVersion=1.18.1
|
||||||
|
|
||||||
klfVersion=1.6.2
|
klfVersion=1.6.2
|
||||||
lokiLogAppenderVersion=0.5.5
|
lokiLogAppenderVersion=0.5.5
|
||||||
# only used for tests
|
# only used for tests
|
||||||
|
|
|
@ -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")
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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() }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
<configuration>
|
||||||
|
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<!-- encoders are assigned the type
|
||||||
|
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
|
||||||
|
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||||
|
<level>DEBUG</level>
|
||||||
|
</filter>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<!-- Insert the current time formatted as "yyyyMMdd'T'HHmmss" under
|
||||||
|
the key "bySecond" into the logger context. This value will be
|
||||||
|
available to all subsequent configuration elements. -->
|
||||||
|
<timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/>
|
||||||
|
|
||||||
|
<root level="ALL">
|
||||||
|
<appender-ref ref="STDOUT"/>
|
||||||
|
</root>
|
||||||
|
|
||||||
|
<!-- Apache FOP will flood otherwise the log so that test run crashes -->
|
||||||
|
<logger name="org.apache.fop" level="INFO">
|
||||||
|
<appender-ref ref="STDOUT"/>
|
||||||
|
</logger>
|
||||||
|
<logger name="org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry" level="INFO">
|
||||||
|
<appender-ref ref="STDOUT"/>
|
||||||
|
</logger>
|
||||||
|
|
||||||
|
</configuration>
|
|
@ -28,4 +28,6 @@ rootProject.name = "eInvoicing"
|
||||||
|
|
||||||
include("e-invoice-domain")
|
include("e-invoice-domain")
|
||||||
|
|
||||||
|
include("invoice-creator")
|
||||||
|
|
||||||
include("e-invoice-api")
|
include("e-invoice-api")
|
||||||
|
|
Loading…
Reference in New Issue