'SpringBoot - Generate pdf using flying-saucer-pdf result into empty file
I'am tying to export a pdf file from html using fly-saucer, but the file is always empty and therefore can not be opened. Can anyone tell me what i am doing wrong.
Here is my current code:
String templateName = "test-pdf";
Context ctx = new Context();
String processedHtml = templateEngine.process(templateName, ctx);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(processedHtml);
renderer.layout();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
renderer.createPDF(baos, false);
// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentLength
response.setContentLength(baos.size());
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
System.out.println("PDF created successfully");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|