noms-digital-studio/pdf-generator

Name: pdf-generator

Owner: NOMS Digital Studio

Description: PDF Generator Microservice

Created: 2017-06-09 14:44:28.0

Updated: 2018-05-11 12:31:34.0

Pushed: 2018-05-22 11:21:09.0

Homepage: null

Size: 360

Language: HTML

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Pdf Generator Microservice

CircleCI

A REST-accessible self-contained fat-jar micro-service that creates PDFs from XHTML templates, substituting placeholder text in the template with caller-supplied values.

Building and running

Build command (includes running unit and integration tests):

Run locally:

Running deployable fat jar (after building):

Configuration parameters can be supplied via environment variables, e.g.:

The service endpoint defaults to local port 8080.

Development notes

Developed in Java 8 with additional Lombok support. Unit and Integration tests are written in Groovy and Spock. The build system used is Gradle.

Google Guice provides a Dependency Injection framework, the HTTP REST service is provided by the Java Spark Framework, and the PDF Renderer uses the Flying Saucer library.

Templates are currently stored as *.html resources within the service, but this will be updated in the near future to obtain templates from a MongoDB database instead.

Deployment notes

The service currently runs stand-alone (it will require access to a MongoDB database instance in the future), and is configurable via environment parameters:

Usage notes

To generate a PDF, issue a POST request to /generate that include the template name and substitution values, e.g.:

 /generate HTTP/1.1
: localhost:8080


"templateName": "helloWorld",
"values": {
    "SALUTATION": "Mr",
    "FORENAME:" "John",
    "SURNAME": "Smith"
}

The service will return the PDF binary object as a JSON array of Byte values, e.g. [37, 80, 68, 70, 45, 49 ..

Debug mode

When the service is instantiated in Debug mode e.g. DEBUG_LOG=true java -jar pdfGenerator.jar, a browser-accessible GET endpoint for PDF generation is made available at /debug which can be used to test a template and value substitution directly.

E.g. browse to http://localhost:8080/debug/helloWorld?SALUTATION=Mr&FORENAME=John&SURNAME=Smith&CASE_NUMBER=ABC12345&ADDRESS_LINE_1=10%20High%20Street&ADDRESS_LINE_2=Sometown&ADDRESS_LINE_3=Shiresville&DD_MMM_YYYY=10th%20June%202017 for a demonstration if the service is running locally on your machine with default settings.

Template creation

To create or modify new templates, load or create e.g. a Word Document in LibreOffice, and alter as necessary to include PLACE_HOLDER text which will be substituted in generated PDFs.

Then choose File | Export.. from the menu, and export in XHTML .html format. This new template should then be made available to the PDF Generator service by storaing in the src/main/resources/templates directory.

Building and running with Docker

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.