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 use this code to export a PDF from a Jasper application.
Line
exporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, "this.print({bUI: true,bSilent: false,bShrinkToFit: true});");
Adds JavaScript to send a PDF document directly to the printer.
The expected behavior is that a preview of the PDF appears in the print dialog.
This works fine in most cases - except that I am having problems with each of every 5-6 times in Internet Explorer 8 and Firefox.
What happens - the print preview dialog with the PDF document is not displayed or appears with a blank document in the preview window.
- I tried several different JavaScripts (various parameters of this.print() via exporter.setParameter -I tried to set different response headers, for example
response.setContentType("application/pdf"); response.setHeader("Content-disposition","inline; filename=\"" + reportName + "\""); response.setContentLength(baos.size());
they did not help
This seems to be a problem with IE and FF. Has anyone ever addressed this issue? I need to make it work in all browsers in 100% of cases. Perhaps a different approach to achieving the goal of sending export PDF documents directly to the printer? or a third-party library that will work in browsers?
source share