Results 1 to 3 of 3

Thread: render Pdf in IE 7

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Location
    Washington, DC
    Posts
    60

    Default 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. #2
    Join Date
    Dec 2008
    Location
    New York City
    Posts
    134

    Default

    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.
    Andrew Thompson - Linked In

  3. #3
    Join Date
    Mar 2011
    Location
    Washington, DC
    Posts
    60

    Default

    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..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •