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

bugfix: Create html from inputdata and not template data #151

Merged
merged 1 commit into from
Nov 20, 2023
Merged
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
37 changes: 9 additions & 28 deletions src/main/kotlin/no/nav/pdfgen/api/GeneratePdfApi.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package no.nav.pdfgen.api

import com.fasterxml.jackson.databind.JsonNode
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.call
import io.ktor.server.request.contentType
import io.ktor.server.request.receive
import io.ktor.server.request.receiveText
Expand All @@ -13,29 +14,24 @@ import io.ktor.server.routing.Routing
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.route
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.Paths
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
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
import java.io.ByteArrayOutputStream
import java.io.InputStream

fun Routing.setupGeneratePdfApi(env: Environment) {
route("/api/v1/genpdf") {
if (!env.disablePdfGet) {
get("/{applicationName}/{template}") {
val template = call.parameters["template"]!!
val applicationName = call.parameters["applicationName"]!!
val jsonNode = hotTemplateData(applicationName, template)

createHtml(template, applicationName, jsonNode)?.let { document ->
createHtmlFromTemplateData(template, applicationName)?.let { document ->
call.respond(createPDFA(document))
}
?: call.respondText(
Expand All @@ -48,8 +44,9 @@ fun Routing.setupGeneratePdfApi(env: Environment) {
val startTime = System.currentTimeMillis()
val template = call.parameters["template"]!!
val applicationName = call.parameters["applicationName"]!!
val jsonNode: JsonNode = call.receive()

createHtmlFromTemplateData(template, applicationName)?.let { document ->
createHtml(template, applicationName, jsonNode)?.let { document ->
call.respond(createPDFA(document))
log.info("Done generating PDF in ${System.currentTimeMillis() - startTime}ms")
}
Expand Down Expand Up @@ -104,9 +101,7 @@ fun Routing.setupGeneratePdfApi(env: Environment) {
get("/{applicationName}/{template}") {
val template = call.parameters["template"]!!
val applicationName = call.parameters["applicationName"]!!
val jsonNode = hotTemplateData(applicationName, template)

createHtml(template, applicationName, jsonNode)?.let { call.respond(it) }
createHtmlFromTemplateData(template, applicationName)?.let { call.respond(it) }
?: call.respondText(
"Template or application not found",
status = HttpStatusCode.NotFound
Expand All @@ -132,17 +127,3 @@ fun Routing.setupGeneratePdfApi(env: Environment) {
}
}
}

private fun hotTemplateData(applicationName: String, template: String): JsonNode {
val dataFile = Paths.get("data", applicationName, "$template.json")
val data =
objectMapper.readValue(
if (Files.exists(dataFile)) {
Files.readAllBytes(dataFile)
} else {
"{}".toByteArray(Charsets.UTF_8)
},
JsonNode::class.java,
)
return data
}
Loading