Announcement Announcement Module
Collapse
No announcement yet.
pdf view Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • pdf view

    I am trying to create a generic view class that is sent the file path and mimetype and the document is diplayed inline for text/pdf/doc etc and downloaded as binary for zip etc.
    It works fine for all types in Firefox but pdfs don't work in IE. It looks like Acrobat Reader loads but the browser stays blank. Here is the code.

    public class GenericDocView extends AbstractView
    {
    final static int BUFFER_SIZE = 4 * 1024; // 4K buffer

    protected void renderMergedOutputModel(Map model, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception
    {
    String path = (String) model.get("path");
    String mimeType = (String) model.get("mimeType");
    File file = new File(getServletContext().getRealPath(path));
    if(file.exists())
    {
    //returns the doc inline
    httpServletResponse.setContentType(mimeType);
    httpServletResponse.setHeader("Content-Disposition", "inline; filename=" + file.getName());
    ServletOutputStream out = httpServletResponse.getOutputStream();
    InputStream in = null;
    try
    {
    in = new BufferedInputStream(new FileInputStream(file));
    byte[] buf = new byte[BUFFER_SIZE];
    int bytesRead;
    while ((bytesRead = in.read(buf)) != -1)
    {
    out.write(buf, 0, bytesRead);
    }
    }
    finally
    {
    if (in != null) in.close();
    }
    out.flush();
    }
    else
    {
    throw new Exception("File no longer exists on the server.");
    }
    }
    }

  • #2
    it's not a Spring issue.

    http://www.lowagie.com/iText/faq.html#msie

    Regards,

    Comment


    • #3
      The above code works fine from a servlet but not as a view. I made these changes and it still doesn't work.

      final static int BUFFER_SIZE = 4 * 1024; // 4K buffer

      protected void renderMergedOutputModel(Map model, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception
      {
      String path = (String) model.get("path");
      String mimeType = (String) model.get("mimeType");
      File file = new File(getServletContext().getRealPath(path));
      if(file.exists())
      {
      //returns the doc inline
      httpServletResponse.setContentType(mimeType);
      httpServletResponse.setHeader("Content-Disposition", "inline; filename=" + file.getName());
      ServletOutputStream out = httpServletResponse.getOutputStream();
      InputStream in = null;
      try
      {
      in = new BufferedInputStream(new FileInputStream(file));
      byte[] buf = new byte[BUFFER_SIZE];
      int bytesRead;
      int totalBytesRead = 0;

      while ((bytesRead = in.read(buf)) != -1)
      {
      out.write(buf, 0, bytesRead);
      totalBytesRead += bytesRead;
      }
      httpServletResponse.setContentLength(totalBytesRea d);
      out.flush();
      }
      finally
      {
      if (in != null) in.close();
      }
      }
      else
      {
      throw new Exception("File no longer exists on the server.");
      }
      }

      Comment


      • #4
        FYI I was able to get this functionality by putting the code in the controller instead of a view. The code looks something like this.

        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
        {
        File file = new File(((WebApplicationContext)ctx).getServletContex t().getRealPath(path));
        response.setContentType(mimeType);
        response.setHeader("Content-disposition", "inline; filename=" + file.getName());
        ServletOutputStream out = response.getOutputStream();
        InputStream in = null;
        try
        {
        in = new BufferedInputStream(new FileInputStream(file));
        byte[] buf = new byte[BUFFER_SIZE];
        int bytesRead;
        int totalBytesRead = 0;

        while ((bytesRead = in.read(buf)) != -1)
        {
        out.write(buf, 0, bytesRead);
        totalBytesRead += bytesRead;
        }
        response.setContentLength(totalBytesRead);
        out.flush();
        }
        finally
        {
        if (in != null) in.close();
        }
        return null;
        }

        Comment

        Working...
        X