Skip to content

Commit 03ecc00

Browse files
committed
Go through this api to generate PDF
1 parent d3be22d commit 03ecc00

File tree

6 files changed

+107
-1
lines changed

6 files changed

+107
-1
lines changed

deploy/nais.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ spec:
6666
inbound:
6767
rules:
6868
- application: kabal-frontend
69+
outbound:
70+
rules:
71+
- application: kabal-json-to-pdf
6972
azure:
7073
application:
7174
enabled: true

src/main/kotlin/no/nav/klage/document/api/DocumentController.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package no.nav.klage.document.api
22

33
import io.swagger.annotations.Api
44
import io.swagger.annotations.ApiOperation
5+
import io.swagger.annotations.ApiParam
56
import no.nav.klage.document.api.views.CommentInput
67
import no.nav.klage.document.api.views.CommentView
78
import no.nav.klage.document.api.views.DocumentView
@@ -10,6 +11,10 @@ import no.nav.klage.document.domain.Document
1011
import no.nav.klage.document.service.CommentService
1112
import no.nav.klage.document.service.DocumentService
1213
import no.nav.klage.document.util.getLogger
14+
import org.springframework.http.HttpHeaders
15+
import org.springframework.http.HttpStatus
16+
import org.springframework.http.MediaType
17+
import org.springframework.http.ResponseEntity
1318
import org.springframework.web.bind.annotation.*
1419
import java.util.*
1520

@@ -128,6 +133,29 @@ class DocumentController(
128133
return mapCommentToView(commentService.getComment(commentId = commentId))
129134
}
130135

136+
@ApiOperation(
137+
value = "Generer PDF",
138+
notes = "Generer PDF"
139+
)
140+
@ResponseBody
141+
@GetMapping("/{documentId}/pdf")
142+
fun getDocumentAsPDF(
143+
@PathVariable("documentId") documentId: UUID
144+
): ResponseEntity<ByteArray> {
145+
logger.debug("getDocumentAsPDF with id {}", documentId)
146+
147+
val pdfDocument = documentService.getDocumentAsPDF(documentId)
148+
149+
val responseHeaders = HttpHeaders()
150+
responseHeaders.contentType = MediaType.APPLICATION_PDF
151+
responseHeaders.add("Content-Disposition", "inline; filename=${pdfDocument.filename}.pdf")
152+
return ResponseEntity(
153+
pdfDocument.bytes,
154+
responseHeaders,
155+
HttpStatus.OK
156+
)
157+
}
158+
131159
private fun mapToDocumentView(document: Document): DocumentView =
132160
DocumentView(
133161
id = document.id,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package no.nav.klage.document.clients
2+
3+
import no.nav.klage.document.domain.PDFDocument
4+
import no.nav.klage.document.util.getLogger
5+
import org.springframework.http.MediaType
6+
import org.springframework.stereotype.Component
7+
import org.springframework.web.reactive.function.client.WebClient
8+
9+
@Component
10+
class KabalJsonToPdfClient(
11+
private val kabalJsonToPdfWebClient: WebClient
12+
) {
13+
companion object {
14+
@Suppress("JAVA_CLASS_ON_COMPANION")
15+
private val logger = getLogger(javaClass.enclosingClass)
16+
}
17+
18+
fun getPDFDocument(json: String): PDFDocument {
19+
return kabalJsonToPdfWebClient.post()
20+
.uri { it.path("/topdf").build() }
21+
.contentType(MediaType.APPLICATION_JSON)
22+
.bodyValue(json)
23+
.retrieve()
24+
.toEntity(ByteArray::class.java)
25+
.map {
26+
val filename = it.headers["filename"]?.first()
27+
PDFDocument(
28+
filename = filename ?: throw RuntimeException("Could not get filename from headers"),
29+
bytes = it.body ?: throw RuntimeException("Could not get PDF data")
30+
)
31+
}
32+
.block() ?: throw RuntimeException("PDF could not be created")
33+
}
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package no.nav.klage.document.config
2+
3+
import no.nav.klage.document.util.getLogger
4+
import org.springframework.context.annotation.Bean
5+
import org.springframework.context.annotation.Configuration
6+
import org.springframework.web.reactive.function.client.WebClient
7+
8+
@Configuration
9+
class KabalJsonToPdfClientConfiguration(
10+
private val webClientBuilder: WebClient.Builder
11+
) {
12+
13+
companion object {
14+
@Suppress("JAVA_CLASS_ON_COMPANION")
15+
private val logger = getLogger(javaClass.enclosingClass)
16+
}
17+
18+
private var appURL = "http://kabal-json-to-pdf"
19+
20+
@Bean
21+
fun kabalJsonToPdfWebClient(): WebClient {
22+
return webClientBuilder
23+
.baseUrl(appURL)
24+
.build()
25+
}
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package no.nav.klage.document.domain
2+
3+
data class PDFDocument(
4+
val filename: String,
5+
val bytes: ByteArray
6+
)

src/main/kotlin/no/nav/klage/document/service/DocumentService.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package no.nav.klage.document.service
22

3+
import no.nav.klage.document.clients.KabalJsonToPdfClient
34
import no.nav.klage.document.domain.Document
5+
import no.nav.klage.document.domain.PDFDocument
46
import no.nav.klage.document.repositories.DocumentRepository
57
import org.springframework.stereotype.Service
68
import org.springframework.transaction.annotation.Transactional
@@ -9,7 +11,10 @@ import java.util.*
911

1012
@Service
1113
@Transactional
12-
class DocumentService(private val documentRepository: DocumentRepository) {
14+
class DocumentService(
15+
private val documentRepository: DocumentRepository,
16+
private val kabalJsonToPdfClient: KabalJsonToPdfClient
17+
) {
1318

1419
fun createDocument(json: String): Document {
1520
val now = LocalDateTime.now()
@@ -33,4 +38,8 @@ class DocumentService(private val documentRepository: DocumentRepository) {
3338
return documentRepository.getById(documentId)
3439
}
3540

41+
fun getDocumentAsPDF(documentId: UUID): PDFDocument {
42+
return kabalJsonToPdfClient.getPDFDocument(documentRepository.getById(documentId).json)
43+
}
44+
3645
}

0 commit comments

Comments
 (0)