Empty PDF from Servlet

By: W Leong - wk_leong
Empty PDF from Servlet
2005-03-16 02:05

I've been bugged by this problem for a few days. Essentially, I can generate pdf preview from iReport fine. But if I stream it from servlet I always got an empty PDF. I have tried various methods of output ( JasperExportManager.exportReportToPdfStream, JRPdfExporter.exportReport, JasperRunManager.runReportToPdf) and none helps. However, if I were to stream the output to a file (instead of output), the pdf can be viewed fine.

Here's the code snippets:

File reportFile = new File(getServlet().getServletContext().getRealPath("/rptProfile.jasper"));
JasperReport jsRpt = (JasperReport)JRLoader.loadObject(reportFile.getPath());
JasperPrint jsPrn = JasperFillManager.fillReport(jsRpt, params, db.connection() );

ServletOutputStream out = res.getOutputStream();
res.setHeader("Content-Disposition", "inline; filename=test.pdf");

java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
// write to physical file. the pdf can be viewed fine
baos.writeTo(new java.io.FileOutputStream("newPdfFile.pdf"));
// dump to screen. pdf opens blank


I tried edit and compare the 2 (written and blank) pdf. 99% of the content are the same except a few corrupted? characters in the stream section. I am suspecting I need to set certain encoding on the output stream.

I have came across similar posting in forum but no fix has been suggested so far. Any helps is much appreaciated.

My current environment:
jasperreports 0.6.5
itext 1.0.1
tomcat 5.5.7
jdk 1.5.0 update 1

Thx & Regards

By: Greg Lloyd - haobaba1
RE: Empty PDF from Servlet
2005-03-25 08:50
I am having issues with serving up PDF content from a servlet as well. In my case it may be a browser issue( IE6) as this is the only browser that doesn't display the PDF. My reponse headers are as follows, these were taken from a developer friendly browsers:

Pragma: No-cache
Cache-Control: no-cache
Expires: Wed, 31 Dec 1969 19:00:00 EST
X-Powered-By: Servlet 2.4; Tomcat-5.0.28/JBoss-4.0.1RC2 (build: CVSTag=JBoss_4_0_1_RC2 date=200411302349)
content-disposition: inline; filename=report.pdf
Content-Type: application/pdf
Content-Length: 3763
Date: Fri, 25 Mar 2005 16:37:42 GMT
Server: Apache-Coyote/1.1

This is how my servlet outputs the pdf to the browser:

response.setContentType("application/pdf"); response.setHeader("Content-Disposition","inline; filename=report.pdf");

byte[] bytes= rbean.getPdf();
ServletOutputStream out= response.getOutputStream();

If anybody has any ideas they are appreciated.

By: podster - podster
RE: Empty PDF from Servlet
2005-03-28 05:54
I'm having the same problem where everything worked fine when my JBoss server was running on my local XP box but when I moved the web application to a Solaris 8 box the PDF will not display (I get a blank white page) on IE6 but WILL display using Firefox. I am not getting any error through JBoss or Apache. Does anybody have any experiece with this? Why IE6 doesn't stream PDF's through a servlet on Solaris? What is the workaround. I have attached my headers just in case.

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6) Gecko/20050225 Firefox/1.0.1
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

Content-Type: application/x-www-form-urlencoded
Content-Length: 65
HTTP/1.x 200 OK
Date: Mon, 28 Mar 2005 13:47:34 GMT
Server: Apache/1.3.29 (Unix) mod_jk/1.2.5 mod_gzip/ mod_ssl/2.8.16 OpenSSL/0.9.7c
Cache-Control: no-store
X-Powered-By: Servlet 2.4; Tomcat-5.0.28/JBoss-4.0.1sp1 (build: CVSTag=JBoss_4_0_1_SP1 date=200502160314)
Connection: close
Transfer-Encoding: chunked
Content-Type: application/pdf

By: Greg Lloyd - haobaba1
RE: Empty PDF from Servlet
2005-03-30 11:47
Imagine this, IE is screwed up. I figured out that IE doesn't like pdf's streamed to it from web servers unless they are ~> 6kb. I added a few images to my reports and walla IE now accepts them.

By: W Leong - wk_leong
RE: Empty PDF from Servlet
2005-04-14 02:52
didn't work for me. I have embedded image of 12k but nothing shows. This could be headache when my application goes to multi-user environment.

By: Denny Valliant - xenden
RE: Empty PDF from Servlet
2005-04-14 11:32
I had been getting the error when I tried to stream the pdf. Works fine if I write the file first, but it's blank if I try to go straight to the user.

The below code is working for me tho. I'm thinnking it might be the JRXmlLoader vs. the generic class...

This is written in ColdFusion, but it should be obvious how to convert it back to normal java. Mostly just use the stuff in the <cfscript> tags and get rid of the create object and replace it with the object. Eh. Please don't laugh at my weak kode'n skillz. ;-)

Good luck!

<cffunction name="jasperreport">
<cfargument name="jrxmlID" type="numeric" required="yes">
<cfargument name="dataSource" required="yes" hint="Datasource is required">
<cfargument name="exportType" required="yes" default="pdf">
<cfargument name="download" required="no" default="no">
<cfquery name = "getJR" datasource = "nmkids">
SELECT * FROM jrxmls
WHERE jrxmlID = <cfqueryparam cfsqltype="cf_sql_integer" value="#jrxmlID#">
<cfset daJRXML = xmlparse(toString(toBinary(getJR.jrxml)))>
<cfset daTitle = daJRXML.jasperReport.xmlAttributes.name>
<cfset daXM = toString(toBinary(getJR.jrxml))>
<cfset params = StructNew()>
<cfset params["certificate_ID"] = '9443'>
xmlBuffer = CreateObject("java","java.lang.String").init(daXM).getBytes();
//Turn the buffers into input streams
xmlInputStream = CreateObject("java","java.io.ByteArrayInputStream").init(xmlBuffer);
outStream = CreateObject("java","java.io.ByteArrayOutputStream").init();
parameters = CreateObject("java", "java.util.HashMap");
// move the param structure to an array to evaluate
ka = StructKeyArray(params);
for(i=1; i LTE ArrayLen(ka); i=i+1){
curr_ka = ka;
// take struct params and add them to report
parameters.put(curr_ka, Evaluate("params." & curr_ka));
dCFjasper = CreateObject("java","com.den.ColdFusion.CFjasper").init();
daFactory = CreateObject ("Java","coldfusion.server.ServiceFactory");
daConnection = daFactory.getDataSourceService().getDatasource(dataSource).getConnection();
//argy = dCFjasper.CFjasper(xmlInputStream,exportType,parameters,daConnection);
jRXmlLoader = CreateObject ("Java","net.sf.jasperreports.engine.xml.JRXmlLoader");
jasperDesign = jRXmlLoader.load(xmlInputStream);
jasperCompileManager = CreateObject("java","net.sf.jasperreports.engine.design.JRJdtCompiler");
jasperReport = jasperCompileManager.compileReport(jasperDesign);
// Fourth, create JasperPrint using fillReport() method
jasperFillManager = CreateObject("java","net.sf.jasperreports.engine.JasperFillManager");
jasperPrint = jasperFillManager.fillReport(jasperReport, parameters, daConnection);
jasperExportManager = CreateObject("java","net.sf.jasperreports.engine.JasperExportManager");
expparam = CreateObject("java", "net.sf.jasperreports.engine.JRExporterParameter");
if (exportType is "pdf") {
argy = jasperExportManager.exportReportToPdf(jasperPrint);
} else if(exportType is "rtf") {
argy = jasperExportManager.exportReportToRtf(jasperPrint);
} else if(exportType is "xls") {
argy = jasperExportManager.exportReportToXls(jasperPrint);
<cfsetting showdebugoutput="no">
<cfsetting enablecfoutputonly="yes">
<cfset request.cvalue = 'attachment;'>
<cfset request.cvalue = request.cvalue & "filename=#daTitle#.#exportType#">
<cfheader name="Content-Disposition" value="#request.cvalue#">
<cfcontent type="application/#exportType#"><cfoutput>#toString(argy)#</cfoutput>

By: Wal Millard - walmillard
RE: Empty PDF from Servlet
2005-04-14 23:36


you may like to have a look at usng the exporter class.

i have been using ie (5.x and 6.x) and firefox without any problems...

JRPdfExporter exporter = new JRPdfExporter();
JasperFillManager.fillReport(TemplatePath + "Main.jasper", parameters,
new JRXmlDataSource(new BufferedInputStream( new ByteArrayInputStream(Sb.toString().getBytes())),

exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, response.getOutputStream());

new Integer(PdfWriter.AllowCopy | pdfWriter.AllowPrinting));

exporter.exportReport(); // Create the PDF file within the stream

By: W Leong - wk_leong
RE: Empty PDF from Servlet
2005-03-17 19:53
More info: Tested with Firefox and IE6.0 and problem persist. Presume this is not a browser issue. May have more to do with servlet outputstream setting.

By: Tom Eugelink - tbee
RE: Empty PDF from Servlet
2005-08-03 11:44
This is ridiculous. I first write the PDF to a file and then copy that file contents to the response. In my browser I get an empty page, but opening the file I get my report.

JasperReport 1.0.0
Tomcat 4.1

By: Brad Balmer - bbalmer
RE: Empty PDF from Servlet
2005-08-03 12:01
This is what I'm doing. (I generate my pdf byte[] from my Action and then forward onto a servlet to stream it up to the browser.

In short:
byte[] reportData = JasperRunManager.runReportToPdf(jasperFile.getPath(), parameters, dataSource);

session.setAttribute("contentType", "application/pdf");
session.setAttribute("reportData", reportData);

Then in my servlet:

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
ServletOutputStream outputStream = response.getOutputStream();

String contentType = (String) session.getAttribute("contentType");
byte[] reportBytes = (byte[]) session.getAttribute("reportData");
if(reportBytes == null)
reportBytes = new byte[0];

outputStream.write(reportBytes, 0, reportBytes.length);

By: Tom Eugelink - tbee
RE: Empty PDF from Servlet
2005-08-03 12:49
I've tried writing to file, byte array, byte output stream and directly to stream, using the exporter, exportReportToPdf and your runReportToPdf.

(The advantage of using the file is that you can inspect the PDF after wards.)

By: kootjekip - kootjekip
RE: Empty PDF from Servlet
2005-08-04 00:32
why use a servlet if you can use a inline pdf?

By: Tom Eugelink - tbee
RE: Empty PDF from Servlet
2005-08-04 00:47
Could you explain that remark?

AFAIK somewhere there must be a place where the report is exported into PDF and the result put on the response' output stream. That requires a servlet.
2004 IR Help's picture
Joined: Aug 17 2006 - 3:49am
Last seen: 15 years 9 months ago

0 Answers:

No answers yet