Repository for pdfgen-core, an application written in Kotlin used to create PDFs and HTMLs
- Kotlin
- Gradle
- Ktor
- Junit
- Handlebars
- VeraPDF-validation
- JDK 21
pdfgen-core is used with templates, fonts, additional resources, and potential test data to verify that valid PDFs get produced by the templates.
Check GitHub releases to find the latest release version
Check Github releases to find the latest release version
In your own repository create folders templates, resources and data on root of you repository
-
templatesCreate subfolder insidetemplatesfoldermkdir {templates}/directory_name # directory_name can be anything, but it'll be a necessary part of the API latertemplates/directory_name/should then be populated with your .hbs-templates. the names of these templates will also decide parts of the API paths
-
resourcesshould contain additional resources (ex images) which can be referred in your .hbs-templates -
datashould contain test JSON data that can be used to verify that valid PDFs get produced by templates.datafolder should have same subdirectory structure astemplates. Example how you can produce HTML and PDF with test data
You can initialize pdfgen-core with additional handlebar helpers:
PDFGenCore.init(Environment(additionalHandlebarHelpers = mapOf(
"enum_to_readable" to Helper<String> { context, _ ->
when(context){
"SOME_ENUM_VALUE" -> "Human readable text"
else -> ""
}
},
)))PDFGenCore.init(
Environment(
additionalHandlebarHelpers = mapOf(
"enum_to_readable" to Helper<String> { context, _ ->
when (context) {
"BIDRAGSMOTTAKER" -> "Bidragsmottaker"
else -> ""
}
},
),
templateRoot = PDFGenResource("/path/to/templates"),
resourcesRoot = PDFGenResource("/path/to/resources"),
),
)You can reload the resources and templates from disk using the following method. This will be especially useful for when developing templates and should be executed before generating HTML or PDF such that the updated templates and data are loaded from disk
PDFGenCore.reloadEnvironment()val html: String = createHtmlFromTemplateData(template, directoryName)val html: String = createHtmlFromTemplateData(template, directoryName)
val pdfBytes: ByteArray = createPDFA(html)val html: String = createHtmlFromTemplateData(template, directoryName, jsonNode)
val pdfBytes: ByteArray = createPDFA(html)
// Or directly
val pdfBytes: ByteArray = createPDFA(template, directoryName, jsonNode)Example of usage of handlebar helpers defined in this library
Filter array of objects by fieldvalue
Example data
{
"roller": [
{
"type": "BARN",
"navn": "Barn1 Etternavn"
},
{
"type": "BARN",
"navn": "Barn2 Etternavn"
},
{
"type": "FORELDRE",
"navn": "Mor Etternavn"
}
]
}Params
json{List} List of JSON valuesfieldname{String} Fieldname used for filteringvalue{String} Value to filter byreturns{List} Filtered list
Example
Format json with parameters fom, tom/til as period string
Example data
{
"periode": {
"fom": "2020-03-20",
"tom": "2021-09-23"
}
}Params
json{Periode} Object with fieldsfomandtomortilreturns{List} Formatted date (ex20.03.2020 - 23.09.2021)
Example
./gradlew build
Run the following command
./gradlew publishToMavenLocal (or ./gradlew -t publishToMavenLocal if you want to enable autobuild on changes)
This will publish pdfgen-core to local maven repository with version local-build
You can then import pdfgen-core to your gradle project with
implementation("no.nav.pdfgen:pdfgen-core:local-build")
We use default github release,
This project uses semantic versioning and does NOT prefix tags or release titles with v i.e. use 1.2.3 instead of v1.2.3
see guide about how to relese:creating release github
Find the newest version of gradle here: https://gradle.org/releases/ Then run this command:
./gradlew wrapper --gradle-version $gradleVersjon
This project is currently maintained by the organisation @navikt.
If you need to raise an issue or question about this library, please create an issue here and tag it with the appropriate label.
For contact requests within the @navikt org, you can use the slack channel #pdfgen
If you need to contact anyone directly, please see contributors.
To get started, please fork the repo and checkout a new branch. You can then build the library with the Gradle wrapper
./gradlew shadowJarSee more info in CONTRIBUTING.md