2 Replies Latest reply on Jan 30, 2007 6:26 AM by Jens Peter

    Open PDF in IE

    Jens Peter Newbie

      Hello,

      I´m using Seam 1.1.0 and it´s great.
      There´s a Seam-Component for Japser Reports that´s working fine.

      However opening PDF (inline) in InternetExplorer requiers a hack.
      See http://xmlgraphics.apache.org/fop/0.93/servlets.html


      Notes on Microsoft Internet Explorer
      Some versions of Internet Explorer will not automatically show the PDF...However, Internet Explorer can still be used to download the PDF so that it can be viewed later....
      ..Use an URL ending in .pdf, like http://myserver/servlet/stuff.pdf. Yes, the servlet can be configured to handle this. If the URL has to contain parameters, try to have both the base URL as well as the last parameter end in .pdf, if necessary append a dummy parameter, like http://myserver/servlet/stuff.pdf?par1=a&par2=b&d=.pdf. The effect may depend on IEx version.


      I was trying to append a dummy=.pdf parameter to the url using pages.xml, but the dummy-parameter is not appended at the very end, because Seam´s Redirect Filter appends the ConversationId to the url.

      I know, that ist not the job of Seam to support an ugly workaround for IE to open PDF inline. But I hope, that someone can help me to solve this problem.

      Thanks
      jpb

      Here´s the code to produce the PDF on the fly:

      public String makePdf() {
       ...
       JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(items);
       Map paramMap = new HashMap();
      
       try {
       JasperPrint jasperPrint = JasperFillManager.fillReport(productReport, paramMap, jrDataSource);
       HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
       ServletOutputStream out = response.getOutputStream();
      
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       JasperExportManager.exportReportToPdfStream(jasperPrint, baos);
      
       response.setContentType("application/pdf");
       response.setHeader("Expires", "0");
       response.setHeader("Cache-Control",
       "must-revalidate, post-check=0, pre-check=0");
       response.setHeader("Pragma", "public");
       String filename = "Produktliste.pdf";
       response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
      
       // contentlength required for MSIE
       response.setContentLength(baos.size());
      
       baos.writeTo(out);
       out.flush();
       out.close();
      
       FacesContext.getCurrentInstance().responseComplete();
       ...
      
       return Constants.View.REDIRECT.getNav();
       }
      
      
      








        • 1. Re: Open PDF in IE
          Norman Richards Master

          Well, one quick hack would be to not apply the hack in the servlet and instead apply the hack in your own servlet filter that surrounds the other filter.

          • 2. Re: Open PDF in IE
            Jens Peter Newbie

            Thank you for the quick response.

            Here´s the workaround with a Wrapper for the SeamRedirectFilter.
            It appends dummy=dummy.pdf to the end of the url.

            public class RedirectFilterWrapper extends SeamRedirectFilter {
            
             public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
             throws IOException, ServletException {
             super.doFilter(request, wrapResponse((HttpServletResponse)response), chain);
             }
            
             private static ServletResponse wrapResponse(HttpServletResponse response) {
             return new HttpServletResponseWrapper(response) {
            
             public void sendRedirect(String url) throws IOException {
             String param = "dummy=dummy.pdf";
            
             url = new StringBuilder( url.length() + param.length() +1)
             .append(url)
             .append( url.contains("?") ? '&' : '?' )
             .append(param)
             .toString();
            
             super.sendRedirect(url);
             }
             };
             }
            }