Convert Freemarker to PDF

I design reports using freemarker, I have a problem when I need processed PDF output.

What I want to do is to pass the HTML + CSS fremarker template to the freemarker mechanism and output the processed HTML as a PDF. The current issue is how to convert processed freemarker to pdf

try { Configuration cfg = new Configuration(); Template tpl = cfg.getTemplate("example.ftl"); OutputStreamWriter output = new OutputStreamWriter(System.out); Map testHashMap = new HashMap(); testHashMap.put("test", "testValue"); tpl.process(testHashMap, output); } catch (Exception e) { e.printStackTrace(); } 

While searching the Internet, I could not find any information on this topic, but I found out about the iText infrastructure

 try { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(new StringBufferInputStream(buf.toString())); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(doc, null); renderer.layout(); OutputStream os = response.getOutputStream(); renderer.createPDF(os); os.close(); } catch (Exception ex) { ex.printStackTrace(); } 

Now the problem is how to combine these two code snippets to create pdf?

All help is really appreciated

Regards, MilindaD

+4
source share
1 answer

I think it would be better to use two different pipelines and consider them as two different types of the same model.

Data → Frefarker transfomer → HTML

Data → iText Transformer → pdf

or you can use XSLT in html and use XSL-FO like Apache FOP, but it seems to me too complicated.

+3
source

All Articles