I'm using JasperReports to generate a PDF and HttpServlet to send binary data to the browser. The issue is that the generated PDF downloaded from the client side, ends up being a corrupted pdf, hence the PDF viewer displays a blank PDF.
At first, I thought it could be a JasperReports issue but I saved the PDF to a file and it worked like a charm.
JasperExportManager.exportReportToPdfFile(jasperPrint, "/home/user/Desktop/Example.pdf");
// jasperPrint is the rendered JasperReport
So it might be a problem related to HttpServlet. The way I send data is as follows:
HttpServletResponse response;
byte[] out = JasperExportManager.exportReportToPdf(jasperPrint);
// jasperPrint is the rendered JasperReport
response.reset();
response.setHeader("Content-disposition", "attachment;filename=\"Example.pdf\"");
response.setContentType("application/pdf");
response.setHeader("Cache-Control", "max-age=0");
response.setContentLength(out.length);
response.setHeader("Content-Length", String.valueOf(out.length));
response.getOutputStream().write(out, 0, out.length);
response.getOutputStream().flush();
response.getOutputStream().close();
// Right after that, the browser asks me to download the file
// and this is the moment when I got a blank PDF
On top of that, I've checked this answer before I asked this question Blank PDF while downloading
One more thing: downloading a PDF works perfectly fine ( no blank PDF) on QA environment , running a JBoss Server on a Windows Machine. On my local environment I get a blank PDF as I mentioned before, running a JBoss Server on a Linux Machine.
So I was wondering : How can I check my Jboss settings to fix the problem? According to the previous question, it could be a matter of binary data that the server might be ignoring or compressing, I'm not quite sure.
Should I check out as well my web.xml file?
Thanks for any help!
I've finally found the solution! My code had nothing to do with this, it was a Grunt issue all along. For some reason, Grunt was converting the PDF sent to the browser to a wrong encoding. I had to change
"grunt-contrib-connect": "^0.8.0" to "grunt-contrib-connect": "~0.5.0".
Then I ran
npm install
That solved my problem. The solution was found here
https://github.com/drewzboto/grunt-connect-proxy/issues/73#issuecomment-59532082
I can't explain why the newest version of grunt-contrib-connect was encoding the PDF file on a wrong way. Now I'd like to know the reason why this works.