I am using Flying Saucer for pdf report creation and template using thymleaf but the problem is when I am converting the template I am getting this error.
org.thymeleaf.exceptions.TemplateProcessingException: Link base "/download-pdf/{refno}" cannot be context relative (/...) unless the context used for executing the engine implements the org.thymeleaf.context.IWebContext interface
In thymleaf template I am using this code: Download PDF Document
I have taken the reference of https://github.com/tuhrig/Flying_Saucer_PDF_Generation.
Before that, I have used Itext for Pdf report but using Itext PDF is converting but CSS is not supporting properly in the case of Itext.
This is the code for Flying Saucer I am using in Java.
@RequestMapping("download-pdf/{refno}")
public void generatePdf(@PathVariable("refno") Long refno) throws Exception {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding(UTF_8);
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
System.out.println(refno + " " + "downlodadaf");
complaintDto = complaintRepo.findById(refno).orElse(null);
System.out.println(complaintDto.getcMobile() + " dfdf " + complaintDto.getRefno());
Context context = new Context();
context.setVariable("complaintPdfBean", complaintDto);
String renderedHtmlContent = templateEngine.process("complant-privew", context);
String xHtml = convertToXhtml(renderedHtmlContent);
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("Code39.ttf", IDENTITY_H, EMBEDDED);
String baseUrl = FileSystems.getDefault().getPath("src", "main", "resources").toUri().toURL().toString();
renderer.setDocumentFromString(xHtml, baseUrl);
renderer.layout();
// And finally, we create the PDF:
OutputStream outputStream = new FileOutputStream("inline; filename=complantprivew.pdf");
renderer.createPDF(outputStream);
outputStream.close();
}
private String convertToXhtml(String html) throws UnsupportedEncodingException {
Tidy tidy = new Tidy();
tidy.setInputEncoding(UTF_8);
tidy.setOutputEncoding(UTF_8);
tidy.setXHTML(true);
ByteArrayInputStream inputStream = new ByteArrayInputStream(html.getBytes(UTF_8));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
tidy.parseDOM(inputStream, outputStream);
return outputStream.toString(UTF_8);
}