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
-
templates
Create subfolder insidetemplates
foldermkdir {templates}/directory_name # directory_name can be anything, but it'll be a necessary part of the API later
templates/directory_name/
should then be populated with your .hbs-templates. the names of these templates will also decide parts of the API paths
-
resources
should contain additional resources (ex images) which can be referred in your .hbs-templates -
data
should contain test JSON data that can be used to verify that valid PDFs get produced by templates.data
folder 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 fieldsfom
andtom
ortil
returns
{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")
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 shadowJar
See more info in CONTRIBUTING.md