Skip to content

Commit

Permalink
Load fonts on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
sillerud committed Feb 5, 2019
1 parent aab6d33 commit c501436
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ ENV DISABLE_PDF_GET="true"
RUN mkdir out
COPY templates templates
COPY resources resources
COPY fonts fonts
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dependencies {

implementation "org.jsoup:jsoup:$jsoup_version"
implementation "com.fasterxml.jackson.core:jackson-core:$jackson_version"
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:$jackson_version"
implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
implementation "javax.xml.bind:jaxb-api:2.1"
implementation "org.glassfish.jaxb:jaxb-runtime:$jaxb_version"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions fonts/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"family": "Source Sans Pro",
"path": "SourceSansPro-Regular.ttf",
"weight": 400,
"style": "NORMAL",
"subset": false
},
{
"family": "Source Sans Pro",
"path": "SourceSansPro-Bold.ttf",
"weight": 700,
"style": "NORMAL",
"subset": false
}
]
6 changes: 6 additions & 0 deletions src/main/kotlin/no/nav/pdfgen/Bootstrap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package no.nav.pdfgen

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import com.github.jknack.handlebars.Context
import com.github.jknack.handlebars.Handlebars
import com.github.jknack.handlebars.JsonNodeValueResolver
Expand Down Expand Up @@ -53,16 +55,20 @@ val APPLICATION_PDF = ContentType.parse("application/pdf")

val collectorRegistry: CollectorRegistry = CollectorRegistry.defaultRegistry
val objectMapper: ObjectMapper = ObjectMapper()
.registerKotlinModule()
val base64encoder: Encoder = Base64.getEncoder()
val dateFormat: DateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy")
val templateRoot: Path = Paths.get("templates/")
val imagesRoot: Path = Paths.get("resources/")
val fontsRoot: Path = Paths.get("fonts/")
val images = loadImages()
val handlebars: Handlebars = Handlebars(FileTemplateLoader(templateRoot.toFile())).apply {
registerNavHelpers(this)
infiniteLoops(true)
}

val fonts: Array<FontMetadata> = objectMapper.readValue(Files.newInputStream(fontsRoot.resolve("config.json")))

val log: Logger = LoggerFactory.getLogger("pdf-gen")

fun main(args: Array<String>) {
Expand Down
22 changes: 14 additions & 8 deletions src/main/kotlin/no/nav/pdfgen/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import java.awt.geom.AffineTransform
import java.awt.image.AffineTransformOp
import java.awt.image.AffineTransformOp.TYPE_BILINEAR
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.io.OutputStream
Expand All @@ -24,14 +23,21 @@ import javax.imageio.ImageIO
class Utils

val colorProfile: ByteArray = IOUtils.toByteArray(Utils::class.java.getResourceAsStream("/sRGB2014.icc"))
val sourceSansProRegular: ByteArray = IOUtils.toByteArray(Utils::class.java.getResourceAsStream("/fonts/SourceSansPro-Regular.ttf"))
val sourceSansProBold: ByteArray = IOUtils.toByteArray(Utils::class.java.getResourceAsStream("/fonts/SourceSansPro-Bold.ttf"))

data class FontMetadata(
val family: String,
val path: String,
val weight: Int,
val style: BaseRendererBuilder.FontStyle,
val subset: Boolean
)

fun createPDFA(w3doc: Document, outputStream: OutputStream) = PdfRendererBuilder()
.useFont({ ByteArrayInputStream(sourceSansProRegular) }, "Source Sans Pro",
400, BaseRendererBuilder.FontStyle.NORMAL, false)
.useFont({ ByteArrayInputStream(sourceSansProBold) }, "Source Sans Pro", 700,
BaseRendererBuilder.FontStyle.NORMAL, false)
.apply {
for (font in fonts) {
useFont(fontsRoot.resolve(font.path).toFile(), font.family, font.weight, font.style, font.subset)
}
}
// .useFastMode() wait with fast mode until it doesn't print a bunch of errors
.useColorProfile(colorProfile)
.useSVGDrawer(BatikSVGDrawer())
Expand Down Expand Up @@ -94,4 +100,4 @@ private fun scale(image: PDImageXObject, page: PDPage): ImageSize {
}

return ImageSize(width, height)
}
}

0 comments on commit c501436

Please sign in to comment.