Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pdfgen-core #147

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ val junitJupiterVersion = "5.10.1"
val verapdfVersion = "1.24.1"
val ktfmtVersion = "0.44"
val testcontainersVersion= "1.19.2"
val pdfgencoreVersion = "1.0.3"


plugins {
Expand Down Expand Up @@ -67,23 +68,22 @@ tasks {

repositories {
mavenCentral()
maven {
url = uri("https://github-package-registry-mirror.gc.nav.no/cached/maven-release")
}
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("no.nav.pdfgen:pdfgen-core:$pdfgencoreVersion")

implementation("com.github.jknack:handlebars:$handlebarsVersion")
implementation("com.github.jknack:handlebars-jackson2:$handlebarsVersion")
implementation("com.openhtmltopdf:openhtmltopdf-pdfbox:$openHtmlToPdfVersion")
implementation("com.openhtmltopdf:openhtmltopdf-slf4j:$openHtmlToPdfVersion")
implementation("com.openhtmltopdf:openhtmltopdf-svg-support:$openHtmlToPdfVersion")

implementation("org.jsoup:jsoup:$jsoupVersion")
implementation("com.fasterxml.jackson.core:jackson-core:$jacksonVersion")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion")
implementation("javax.xml.bind:jaxb-api:$jaxbApiVersion")
implementation("org.glassfish.jaxb:jaxb-runtime:$jaxbVersion")

implementation("io.ktor:ktor-server-netty:$ktorVersion")
implementation("io.ktor:ktor-server-core:$ktorVersion")
Expand Down
15 changes: 7 additions & 8 deletions src/main/kotlin/no/nav/pdfgen/Bootstrap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package no.nav.pdfgen
// Uncommemt to enable debug to file
// import java.io.File

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import com.github.jknack.handlebars.Template
import com.openhtmltopdf.slf4j.Slf4jLogger
import com.openhtmltopdf.util.XRLog
Expand Down Expand Up @@ -32,13 +30,12 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import no.nav.pdfgen.api.setupGeneratePdfApi
import no.nav.pdfgen.template.loadTemplates
import no.nav.pdfgen.core.PDFgen
import no.nav.pdfgen.core.template.loadTemplates
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.verapdf.gf.foundry.VeraGreenfieldFoundryProvider

val objectMapper: ObjectMapper = ObjectMapper().registerKotlinModule()

val log: Logger = LoggerFactory.getLogger("pdfgen")

fun main() {
Expand All @@ -49,8 +46,10 @@ fun initializeApplication(port: Int): ApplicationEngine {
System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider")
VeraGreenfieldFoundryProvider.initialise()

val env = Environment()
val templates = loadTemplates(env)
PDFgen.init(no.nav.pdfgen.core.Environment())

val env = no.nav.pdfgen.core.Environment()
val templates = loadTemplates()
val collectorRegistry: CollectorRegistry = CollectorRegistry.defaultRegistry

XRLog.setLoggerImpl(Slf4jLogger())
Expand Down Expand Up @@ -92,7 +91,7 @@ fun initializeApplication(port: Int): ApplicationEngine {
}
}
}
setupGeneratePdfApi(env, templates)
setupGeneratePdfApi(env)
}
}
}
Expand Down
73 changes: 0 additions & 73 deletions src/main/kotlin/no/nav/pdfgen/Environment.kt

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/kotlin/no/nav/pdfgen/MetricRegistry.kt

This file was deleted.

95 changes: 32 additions & 63 deletions src/main/kotlin/no/nav/pdfgen/api/GeneratePdfApi.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package no.nav.pdfgen.api

import com.fasterxml.jackson.databind.JsonNode
import com.github.jknack.handlebars.Context
import com.github.jknack.handlebars.JsonNodeValueResolver
import com.github.jknack.handlebars.Template
import com.github.jknack.handlebars.context.MapValueResolver
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.contentType
Expand All @@ -23,20 +19,24 @@ import java.nio.file.Files
import java.nio.file.Paths
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import net.logstash.logback.argument.StructuredArguments
import no.nav.pdfgen.*
import no.nav.pdfgen.pdf.PdfContent
import no.nav.pdfgen.pdf.createPDFA
import no.nav.pdfgen.template.TemplateMap
import no.nav.pdfgen.template.loadTemplates
import no.nav.pdfgen.core.Environment
import no.nav.pdfgen.core.OPENHTMLTOPDF_RENDERING_SUMMARY
import no.nav.pdfgen.core.objectMapper
import no.nav.pdfgen.core.pdf.createHtml
import no.nav.pdfgen.core.pdf.createHtmlFromTemplateData
import no.nav.pdfgen.core.pdf.createPDFA
import no.nav.pdfgen.log

fun Routing.setupGeneratePdfApi(env: Environment, templates: TemplateMap) {
fun Routing.setupGeneratePdfApi(env: Environment) {
route("/api/v1/genpdf") {
if (!env.disablePdfGet) {
get("/{applicationName}/{template}") {
val hotTemplates = loadTemplates(env)
createHtml(call, hotTemplates, true)?.let { document ->
call.respond(PdfContent(document, env))
val template = call.parameters["template"]!!
val applicationName = call.parameters["applicationName"]!!
val jsonNode = hotTemplateData(applicationName, template)

createHtml(template, applicationName, jsonNode)?.let { document ->
call.respond(createPDFA(document))
}
?: call.respondText(
"Template or application not found",
Expand All @@ -46,23 +46,27 @@ fun Routing.setupGeneratePdfApi(env: Environment, templates: TemplateMap) {
}
post("/{applicationName}/{template}") {
val startTime = System.currentTimeMillis()
createHtml(call, templates)?.let { document ->
call.respond(PdfContent(document, env))
val template = call.parameters["template"]!!
val applicationName = call.parameters["applicationName"]!!

createHtmlFromTemplateData(template, applicationName)?.let { document ->
call.respond(createPDFA(document))
log.info("Done generating PDF in ${System.currentTimeMillis() - startTime}ms")
}
?: call.respondText(
"Template or application not found",
status = HttpStatusCode.NotFound
)
}

post("/html/{applicationName}") {
val applicationName = call.parameters["applicationName"]!!
val timer =
OPENHTMLTOPDF_RENDERING_SUMMARY.labels(applicationName, "converthtml").startTimer()

val html = call.receiveText()

call.respond(PdfContent(html, env))
call.respond(createPDFA(html))
log.info(
"Generated PDF using HTML template for $applicationName om ${timer.observeDuration()}ms"
)
Expand All @@ -78,7 +82,7 @@ fun Routing.setupGeneratePdfApi(env: Environment, templates: TemplateMap) {
withContext(Dispatchers.IO) {
call.receive<InputStream>().use { inputStream ->
ByteArrayOutputStream().use { outputStream ->
createPDFA(inputStream, outputStream, env)
createPDFA(inputStream, outputStream)
call.respondBytes(
outputStream.toByteArray(),
contentType = ContentType.Application.Pdf
Expand All @@ -98,8 +102,11 @@ fun Routing.setupGeneratePdfApi(env: Environment, templates: TemplateMap) {
route("/api/v1/genhtml") {
if (!env.disablePdfGet) {
get("/{applicationName}/{template}") {
val hotTemplates = loadTemplates(env)
createHtml(call, hotTemplates, true)?.let { call.respond(it) }
val template = call.parameters["template"]!!
val applicationName = call.parameters["applicationName"]!!
val jsonNode = hotTemplateData(applicationName, template)

createHtml(template, applicationName, jsonNode)?.let { call.respond(it) }
?: call.respondText(
"Template or application not found",
status = HttpStatusCode.NotFound
Expand All @@ -109,7 +116,11 @@ fun Routing.setupGeneratePdfApi(env: Environment, templates: TemplateMap) {

post("/{applicationName}/{template}") {
val startTime = System.currentTimeMillis()
createHtml(call, templates)?.let {
val template = call.parameters["template"]!!
val applicationName = call.parameters["applicationName"]!!
val jsonNode: JsonNode = call.receive()

createHtml(template, applicationName, jsonNode)?.let {
call.respond(it)
log.info("Done generating HTML in ${System.currentTimeMillis() - startTime}ms")
}
Expand All @@ -135,45 +146,3 @@ private fun hotTemplateData(applicationName: String, template: String): JsonNode
)
return data
}

private suspend fun createHtml(
call: ApplicationCall,
templates: TemplateMap,
useHottemplate: Boolean = false,
): String? {
val template = call.parameters["template"]!!
val applicationName = call.parameters["applicationName"]!!
val jsonNode =
if (useHottemplate) hotTemplateData(applicationName, template) else call.receive()
log.debug("JSON: {}", objectMapper.writeValueAsString(jsonNode))
return render(applicationName, template, templates, jsonNode)
}

fun render(
applicationName: String,
template: String,
templates: Map<Pair<String, String>, Template>,
jsonNode: JsonNode
): String? {
return HANDLEBARS_RENDERING_SUMMARY.startTimer()
.use {
templates[applicationName to template]?.apply(
Context.newBuilder(jsonNode)
.resolver(
JsonNodeValueResolver.INSTANCE,
MapValueResolver.INSTANCE,
)
.build(),
)
}
?.let { html ->
log.debug("Generated HTML {}", StructuredArguments.keyValue("html", html))

/* Uncomment to output html to file for easier debug
* File("pdf.html").bufferedWriter().use { out ->
* out.write(html)
* }
*/
html
}
}
8 changes: 0 additions & 8 deletions src/main/kotlin/no/nav/pdfgen/domain/syfosoknader/Periode.kt

This file was deleted.

27 changes: 0 additions & 27 deletions src/main/kotlin/no/nav/pdfgen/domain/syfosoknader/PeriodeMapper.kt

This file was deleted.

Loading
Loading