Jump to content

Running A PDF file Error .Help!


sachinmdev

Recommended Posts

I am using JasperReports 2.0.5,Eclipse 3.3.2 and Tomcat 5.0.28. I have wrote a java class to compile the jrxml and i got two files namely .jasper and .jrprint. Then I am using servlet to get the .jasper file and i have to display the pdf file in browser.

But iam getting some string values in the browser. PDF is not displayed. Please help me to solve this problem.

 

My Servlet Code:

 

Code:
public class DemoReport extends HttpServlet implements Servlet {


public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletOutputStream outputStream = response.getOutputStream();
InputStream inputStream = getServletConfig().getServletContext().getResourceAsStream("/reports/FirstReport.jasper"«»);
try {
JasperRunManager.runReportToPdfStream(inputStream, outputStream, new HashMap(), new JREmptyDataSource());
response.setContentType("application/pdf"«»);
outputStream.flush();
outputStream.close();
} catch (JRException e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
response.setContentType("text/plain"«»);
response.getOutputStream().print(stringWriter.toString());
}
}
}

 

Iam getting this output only in browser. It seems to be the stream is not converted properly.

Code:
[code]%PDF-1.4 %â£ÓŠ3 0 obj <>stream x?3R𢲊Ე0Biab??˥令`d?qih?dq?"+1PHÎ…Ö¤Ô¥`?g?ШҨ?.Jç’ˆpqSp.JM,IMQ.MNN-.N+͉?Tm???Ÿè˜?Â¥tÌž#? endstream endobj 1 0 obj<>>>/MediaBox[0 0 595 842]>> endobj 5 0 obj[1 0 R/XYZ 0 854 0] endobj 2 0 obj<> endobj 4 0 obj<> endobj 6 0 obj<> endobj 7 0 obj<> endobj 8 0 obj<> endobj 9 0 obj<> endobj xref 0 10 0000000000 65535 f 0000000193 00000 n 0000000383 00000 n 0000000015 00000 n 0000000470 00000 n 0000000349 00000 n 0000000520 00000 n 0000000573 00000 n 0000000604 00000 n 0000000660 00000 n trailer <<6d225505f23facc80519044bb35bb78d>]/Root 8 0 R/Size 10/Info 9 0 R>> startxref 854 %%EOF
Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

You are getting pdf file, which is displayed in browser like text-plain.

 

Try my code:

 

Code:
JasperDesign jasperDesign = JRXmlLoader.load(jrxmlDir+jrxml);		JasperCompileManager.compileReportToFile(jasperDesign,fileName);
InputStream fileStream =new DataInputStream(new FileInputStream(fileName)); JasperRunManager.runReportToPdfStream(fileStream, servletOutputStream, map, dataSource);
fileStream.close();
servletOutputStream.flush();
servletOutputStream.close();

 

fileName is *.jasper file.

Link to comment
Share on other sites

Hi,

I got the same output with your code also.

I am using IE 6.0. I concluded the problem with IE only(Is it a bug?). Then Mozilla comes to my mind. I have checked it in mozilla. Both of our code works fine in Mozilla Firefox 2.0.0.12. So what is the compatible version of IE for JasperReports 2.0.5?

:)

Link to comment
Share on other sites

I have checked JasperReports 2.0.5 in IE 7. Its working. It doesn't open the PDF in browser,instead it opens a prompt. I think this is the behavior of IE. You have to save the file to .pdf format to view the file. Otherwise it will be saved in default format only.B)
Link to comment
Share on other sites

IE 6.0 might have a problem with PDFs when the response content length is not set. To set it, you would have to export the PDF into a temp file or into an in-memory buffer, use the temp/buffer size to do response.setContentLength() and then write the data into the response stream. See the source of net.sf.jasperreports.j2ee.servlets.PdfServlet.

 

HTH,

Lucian

Link to comment
Share on other sites

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