I have a Hashmap as a source of data and I'm generating a separate pdf for each key. But when the hashmap length is greater than 2 I'm getting only the 2nd pdf. 2nd one overrides the 1st pdf.(I'm storing HTML in the body key as you can see in the code). I don't want to create separate HTML files and store them in sever.Is there any way where I can store the HTML and generate separate pdf for all keys in hashmap
public byte[] BulkPdf(Map jsonObject) throws ApplicationException, IOException {
LinkedHashMap<String, List<Map<String, Object>>> hashMapDept = new LinkedHashMap<>();
byte[] reportByte = null;
if (!hashMapDept.isEmpty()) {
Integer count = 0;
for (Entry<String, List<Map<String, Object>>> entry : hashMapDept.
entrySet()) {
String body = "";
Writer out = new StringWriter();
Configuration cfg = new Configuration();
try {
cfg.setObjectWrapper(new DefaultObjectWrapper());
String templateStr = UUID.randomUUID().
toString().
replaceAll("-",
"");
logger.debug("templatename --> ",
templateStr);
freemarker.template.Template freemarkerTemplate = new freemarker.template.Template(
templateStr,
new StringReader(html),
cfg);
freemarkerTemplate.process(jsonObject,
out);
body = out.toString();
} catch (TemplateException | IOException | ApplicationException e) {
logger.debug("template pdf exception --> ",
e.getMessage());
out.flush();
e.printStackTrace();
cfg.clearTemplateCache();
} finally {
out.flush();
cfg.clearTemplateCache();
}
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withHtmlContent(body,
"pathforpdf");
builder.useFastMode();
builder.toStream(outputStream);
builder.run();
reportByte = outputStream.toByteArray();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return reportByte;
}