Here’s my current workflow:
- Load POJOs from the database.
- Pass them into Velocity, rendering a HTML template in-memory.
- Then render the outputted HTML to a PDF using Flying Saucer and iText.
The problem occurs when I try to resolve relative URLs like images and the like. Since my document lives in memory, it doesn’t know how to resolve images or stylesheets at all. How can I set the document for it to an in-memory String object while still passing it a directory to load resources from?
String velocityOutput = VelocityEngineUtils.mergeTemplateIntoString(...);
ByteArrayOutputStream output = new ByteArrayOutputStream(...);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(velocityOutput);
renderer.layout();
renderer.createPDF(output);
I need to do everything in-memory as I’m simply generating the report to be emailed to clients. How can I make an <img src="..."/> resolve a relative URL in my HTML template when generated to a PDF?
I ended up simply loading my document into a Java
Documentinstance and then sending it to Flying Saucer with the resource directory in one call:One thing to be especially careful of is the fact that Flying Saucer needs file-based URIs to be prefixed with
file://, and that it requires a slash at the end of the file path in order to work properly: