Monday, February 21, 2011

ContentType is application/pdf but it still generates Excel sheet

I am using the following code to generate a PDF document from a JSP, but it generates an Excel sheet.

<%@ page buffer="7024kb" %>
<%@ page contentType="application/pdf"%>
<%
  String reportType=request.getParameter("reportType");
  String fileName=reportType;
  response.addHeader("Content-disposition", "attachment; filename="+fileName);
%>
<%= request.getParameter("file") %>

Can you tell me why this is happening? How can I rectify this?

From stackoverflow
  • Delivering a file from a Servlet rather than a JSP is a much easier approach.

    Get your PDF into a byte array and then you can do something like this:

    Servlet Code:

    byte[] pdf = PDFObject.getBytes(); // You may need to use a ByteArrayOutputstream or similar depending on the PDF Object
    
    out.write(pdf, 0, pdf.length);
    

    You can add your output headers to the HttpServletResponse as usual.

    But as its delivering as an Excel/CSV sheet rather than a PDF are you sure that your object is actually a PDF? are the contents readable in Excel?

0 comments:

Post a Comment