Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF export printing in Internet Explorer

protected static byte[] exportReportToPdf(JasperPrint jasperPrint) 
                                                      throws JRException {
  JRPdfExporter exporter = new JRPdfExporter();       
  ByteArrayOutputStream baos = new ByteArrayOutputStream();

  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
  exporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, 
              "this.print({bUI: true,bSilent: false,bShrinkToFit: true});");

  exporter.exportReport();        
  return baos.toByteArray();
}

We are using code like this to export a PDF document from a Jasper application.

The line

exporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, 
             "this.print({bUI: true,bSilent: false,bShrinkToFit: true});");

adds JavaScript to send the PDF document directly to the printer.

The expected behavior is that a print dialog will come up with a preview of the PDF document.

This works fine most of the time - except I am having problems about one out of every 5-6 times in Internet Explorer 8 and Firefox.

What happens is - the print preview dialog with the PDF document does not appear or it appears with a blank document in the preview window.

-I've tried a number of different JavaScripts (different params to this.print() via exporter.setParameter -I've tried setting different response headers such as

response.setContentType("application/pdf");
response.setHeader("Content-disposition","inline;  filename=\"" 
                   + reportName 
                   + "\"");
response.setContentLength(baos.size());

these did not seem to help

This seems to be an IE and FF issue. Has anyone ever dealt with this problem? I need to get it to work across all browsers 100% of the time. Perhaps a different approach to accomplish the goal of sending the PDF document export directly to the printer? or a third party library that will work across browsers?

like image 333
user619804 Avatar asked Nov 09 '12 17:11

user619804


1 Answers

Maybe it isn't getting a chance to update the UI. The following code delays the print perhaps giving it the chance it needs. I didn't test as I don't have your environment.

exporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT,
    "app.setTimeOut('this.print({bUI: true,bSilent: false,bShrinkToFit: true});',200);")
like image 85
mikeslattery Avatar answered Sep 29 '22 04:09

mikeslattery