Reputation: 3560
I have a problem using iTextPdf in a WEB Context application.
I have a web servlet that downloads a PDF after I have edited it in runtime.
If I call my method that prepare the PDF in a NON-WEB context application, the PDF is created perfectly and it opens without any problem.
But in a WEB-Context application, the file is created, but when I make the download from my test page it's corrupted: when I open the downloaded file, it's without images and appears an error message about a font that is not found.
If I open the "clean" file and the "corrupted" file with a text editor, they have the same number of lines, but in fact the content appears different.
So I suspect that the problem is the rendering of the content-type in the web response, or something similar.
I have followed this documentation: http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-9
This is the Spring MVC controller implementation:
@RequestMapping(path = "/downloadPDF", method = RequestMethod.GET)
public void downloadPDF(HttpServletResponse response){
try{
response.setContentType("application/pdf;charset=UTF-8");
ByteArrayOutputStream baos = myPDFHandler.getPdf('filetest.pdf');
OutputStream outputStream = response.getOutputStream();
baos.writeTo(outputStream);
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
These are the response headers:
Response Headers
view source
Cache-Control:no-store
Cache-Control:no-cache
Content-Type:application/pdf;charset=UTF-8
Date:Tue, 24 May 2016 10:20:56 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Pragma:no-cache
Server:Apache-Coyote/1.1
Strict-Transport-Security:max-age=31536000 ; includeSubDomains
Transfer-Encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
The problem is not in " myPDFHandler.getPdf" method, because in a NON-WEB context it works perfectly.
Any idea? Thanks.
Upvotes: 1
Views: 520
Reputation: 3560
I have found the solution.
The problem was the return type of the @Controller
, changing void
to @ResponseBody byte[]
worked.
I also removed "charset=UTF-8" and I have added the header "content-disposition".
@RequestMapping(path = "/downloadPDF", method = RequestMethod.GET)
public @ResponseBody byte[] downloadPDF(HttpServletResponse response){
try{
response.setContentType("application/pdf");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=somefile.pdf");
ByteArrayOutputStream baos = myPDFHandler.getPdf('filetest.pdf');
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 1