I am trying to convert a simple HTML to PDF using JSP. But I keep getting the following error:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:429)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.catalina.connector.Response.getWriter(Response.java:604)
org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:186)
org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118)
org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:77)
org.apache.jsp.Pdf_jsp._jspService(Pdf_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.
Apache Tomcat/6.0.14
The code that I am using is this:
<%@ page language="java" import="java.lang.String,org.apache.commons.lang.*,com.aspose.pdf.elements.Pdf,java.io.ByteArrayInputStream,java.net.URL,java.io.FileOutputStream" %> <% String fileName = "test.pdf"; String html = "Hello World !"; byte currentXMLBytes[] = html.getBytes(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(currentXMLBytes); Pdf pdf = new Pdf(); pdf.bindHTML( byteArrayInputStream,new URL("http://localhost/")); response.setHeader("content-disposition","attachment; filename=" + fileName); response.setContentType("application/pdf"); pdf.save(response.getOutputStream()); %>
Also I have tried similar thing with Aspose.Words and it is working perfectly. This is the code for words:
<%@ page language="java" import="com.aspose.words.*" %> <% Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); String html = "Hello World !"; builder.insertHtml(html); String fileName = "test.doc"; response.setHeader("content-disposition","attachment; filename=" + fileName); response.setContentType("application/msword"); doc.save(response.getOutputStream(), SaveFormat.DOC); %>
Can you plese provide some insight regarding this.
I think maybe you can start you web server first, and then write a java class file(not a jsp file) to generate the pdffile from html. You can see whether this problem is still exist in that situation and then make the further dicision.
Thanks for the response. Writing the code using Java Servlet could be a problem as the rest of the application is done using JSP. I was hoping there was a quick fix. Cann't you get some information from the Aspose.Words team as they have it working right. I will try with the servlet approach and see how it rurns out.
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fileName = "test.pdf"; ServletOutputStream stream = null; try{ stream = response.getOutputStream( ); response.setContentType("application/pdf"); response.addHeader("Content-Disposition","attachment; filename="+fileName ); String html = "Hello World !"; byte currentXMLBytes[] = html.getBytes(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(currentXMLBytes); Pdf pdf = new Pdf(); pdf.bindHTML( byteArrayInputStream,new URL("http://localhost/")); pdf.save(stream); } catch (IOException ioe){ throw new ServletException(ioe.getMessage( )); }catch(AsposeBaseException e){ System.out.println(e.getMessage()); }finally { if (stream != null) stream.close( ); } }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response); } }
Although I was able to generate the pdf but there is another issue. The html contains the string "Hello World !" but in the pdf I only get "World !". I have also attached the pdf that I got. Can you please tell me whats going wrong here, because as you can see the code is fairly simple. Am I missing something ?
I have been able to reproduce this error. I have logged this as PDFJAVA-4076 in our issue tracking system. Our developers will try their best to resolve this issue as soon as possible.
Sorry for replying to you so late. We found the reason for this error is that the output stream is not handled properly. Here is the modification suggestion:
String fileName = "test.pdf"; String html = "HelloWorld! "; byte currentXMLBytes[] = html.getBytes(); OutputStream os=response.getOutputStream(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(currentXMLBytes); Pdf pdf = new Pdf(); pdf.bindHTML( byteArrayInputStream,new URL("http://localhost/")); response.setHeader("content-disposition","attachment; filename=" + fileName); response.setContentType("application/x-download");//download mode //pdf.save(response.getOutputStream()); pdf.save(os); os.flush(); os.close(); os=null; response.flushBuffer(); out.clear(); out = pageContext.pushBody();