Jump to content

PDF problems from JSP page


Recommended Posts

By: Samir Lupski - huji456

PDF problems from JSP page

2003-10-09 07:42

Hello

 

I'm using a set of JSP pages to create various PDF reports, however I would like to have the JSP send the PDF stream directly to the browser (being IE6), instead of writing the PDF file somewhere and have the JSP redirect user to that url (because I do not want to store PDF files on my server).

 

It seems that I can't use a ServletOutputStream ouputStream = res.getOutputStream() syntax within my jsp, so could anyone give me an advice or maybe show me a piece of code ?

 

thanks

huji

 

 

 

 

 

By: Chuck Deal - cdeal

RE: PDF problems from JSP page

2003-10-09 07:56

I apologize that I don't have time to clean this up... but the following JSP source worked for me (I have since moved to a Servlet, which is VERY similar to the following code). You'll notice that this is very close to the webapps sample provided in the jasper-report package.

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<%

/*

* IMPORTANT!!!: You cannot use both out and response.getOutputStream on the same page

* Therefore, if any errors are cause by this page, verify that the generated

* .java file does not USE out to send output to the sreen.

*/

 

/* =======================================================

PAGE: jasper_reports.jsp

 

DESCRIPTION: This is the Jasper Reports JSP to disply PDF

========================================================*/

//System.out.println("jasper_reports.jsp");

 

ServletOutputStream outStrm = null;

String jasperPath = application.getRealPath("/JasperReports/");

String jasperReport = (request.getParameter("jasperReport") != null ? request.getParameter("jasperReport") : "");

String jasperParameters = (request.getParameter("jasperParameters") != null ? request.getParameter("jasperParameters") : "");

String jasperType = (request.getParameter("jasperType") != null ? request.getParameter("jasperType") : "PDF");

byte[] outputBytes = null;

 

com.csc.aims.reports.JasperController jc = new com.csc.aims.reports.JasperController();

jc.setJasperReport(jasperPath + jasperReport);

jc.setJasperParameters(jasperParameters + "|ReportPath~" + jasperPath);

//System.out.println(jc.getJasperParameters());

 

try {

// PDF Format

if (jasperType.equals("PDF")) {

response.setContentType("application/pdf");

outputBytes = jc.getPDF();

}

// HTML Format

// else if (jasperType.equals("HTML")) {

// }

// Rich Text Format

// else if (jasperType.equals("RTF")) {

// }

// Excel format

// else if (jasperType.equals("XLS")) {

// }

}

catch (Exception e) {

response.setContentType("text/html");

 

outStrm = response.getOutputStream();

outStrm.println("<html>");

outStrm.println("<head>");

outStrm.println("<title>JasperReports</title>");

outStrm.println("</head>");

 

outStrm.println("<body>");

 

outStrm.println("JasperReports encountered this error :");

outStrm.println("<pre>");

 

e.printStackTrace();

 

outStrm.println("</pre>");

 

outStrm.println("</body>");

outStrm.println("</html>");

 

return;

}

 

if (outputBytes != null && outputBytes.length > 0) {

response.setContentLength(outputBytes.length);

 

outStrm = response.getOutputStream();

outStrm.write(outputBytes);

outStrm.flush();

}

else {

response.setContentType("text/html");

 

outStrm = response.getOutputStream();

outStrm.println("<html>");

outStrm.println("<head>");

outStrm.println("<title>JasperReports</title>");

outStrm.println("</head>");

 

outStrm.println("<body>");

 

outStrm.println("Empty response. Contact AIMS System Administrators.");

 

outStrm.println("</body>");

outStrm.println("</html>");

 

return;

}

%>

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...