Announcement Announcement Module
Collapse
No announcement yet.
render Pdf in IE 7 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • render Pdf in IE 7

    For some reasons, the application can render pdf in firefox but not in IE 7. I hope someone can tell me what I did wrong.

    my url: http://<localhost>/<applicationName>/listDoc/viewpdf.htm?filename=test.pdf
    Code:
    @RequestMapping(value="/listDoc/viewPdf",method=RequestMethod.GET)
    	public @ResponseBody String streamPdf(HttpServletRequest request,
    			HttpServletResponse response,			
    			@RequestParam(value = "fileName", required = false) String fileName,
    			SessionStatus status, ModelMap modelMap) throws Exception{
    		ServletOutputStream outstream = response.getOutputStream();
    		try{			 
    			logger.debug("Prepare to view pdf");
    			File tempDir = WebUtils.getTempDir(getServletContext());
    			String filePath = tempDir.getCanonicalPath()+"/"+fileName;			
    			File generatePdfFile = new File(filePath);
    			if ((generatePdfFile.exists() && (generatePdfFile.canRead()))){				
    				FileInputStream fileInputStream = new FileInputStream(generatePdfFile);
    				response.setContentType("application/pdf");				
    				response.setContentLength((int)generatePdfFile.length());
    			        response.setHeader("Content-Disposition","inline; filename=test.pdf");
    				int readBytes=0;
    				while((readBytes = fileInputStream.read()) != -1) {  
    					outstream.write(readBytes);					
    				}				
    				fileInputStream.close();				
    				outstream.flush();
    				outstream.close();				
    				
    			} else {
    				throw new Exception("Either document doesn't exist or can not be read");
    			}
    			
    			
    		}catch(Exception e){
    			e.printStackTrace();
    			logger.error(e.getMessage(),e.fillInStackTrace());
    			throw new Exception ("Unable to stream pdf.  Please contact your administrator");
    		}finally{
    			outstream.close();
    			outstream.flush();
    		}	
    		
    	}
    My web.xml
    Code:
    <servlet>
    		<servlet-name>sampleapplication</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>
    					classpath:sampleapplication-web.xml								
    				</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>sampleapplication</servlet-name>
    		<url-pattern>*.htm</url-pattern>
    	</servlet-mapping>
    Thank for reading...

  • #2
    Different version of IE behave differently when returning a raw stream to the browser.

    My experience has been that if you add a file extension to the url in question, IE will behave as expeced. I.E. (pun intended), set your url to /viewPDF.pdf and I suspect it will work.

    Comment


    • #3
      Thank for your quick reply...

      The IE browser still doesn't render pdf even I use pdf extension. I debug the application and it break at the right method but still doesn't work.

      However, when I use the same code logic outside the spring framework, it is working. Here is sample code that work(The servlet does not hook up to spring framework).
      Code:
      @Override
      	protected void doGet(HttpServletRequest req, HttpServletResponse response)
      			throws ServletException, IOException {
      		
      
      		String fileName = req.getParameter("fileName"); 
      		
      		File tempDir = WebUtils.getTempDir(getServletContext());
      		String filePath = tempDir.getCanonicalPath()+"/"+fileName;
      		
      		File generatePdfFile = new File(filePath);
      		if ((generatePdfFile.exists() && (generatePdfFile.canRead()))){			
      			
      			ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream(4096);			
      			
      			FileInputStream fileInputStream = new FileInputStream(generatePdfFile);			
      			ServletOutputStream outstream = response.getOutputStream();
      			response.setContentType("application/pdf");				
      			response.setContentLength((int)generatePdfFile.length());
      			response.setHeader("Content-Disposition","inline; filename=ssssssss.pdf");			
      			int readBytes=0;
      			while((readBytes = fileInputStream.read()) != -1) {  
      				outstream.write(readBytes);				
      			}			
      			fileInputStream.close();
      			outstream.flush();
      			outstream.close();			
      			
      		} else {
      			throw new ServletException("Either document doesn't exist or can not be read");
      		}
      		
      		
      	
      	}
      Any thought?? Do anyone know how to configure Spring to use different viewResolver? I guess InternalResourceViewResolver causes this problem. I need the way to override it.

      Thanks..

      Comment

      Working...
      X