JasperReports PDF Exporter does not support adding JavaScript to a PDF, so I need to use the iText PDF library to add the JavaScript and do some other processing. My previous examples showed how to push the PDF to the browser-- here's how to get it on the file system:
<%@page import="javax.sql.DataSource" %>
<%@page import="java.sql.Connection" %>
<%@page import="java.sql.CallableStatement" %>
<%@page import="java.sql.Statement" %>
<%@page import="java.sql.ResultSet" %>
<%@page import="java.util.Date" %>
<%@page import="javax.naming.InitialContext" %>
<%@page import="java.util.ArrayList" %>
<%@page import="java.lang.Integer" %>
<%@ page errorPage="error.jsp" %>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%
InitialContext context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myConnection/myOracleConnection");
Connection connection = dataSource.getConnection();
// Gets the .jasper file from the application location/reports/MyReport.jasper
File reportFile = new File(application.getRealPath("/reports/MyReport.jasper"));
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
Map parameters = new HashMap();
parameters.put("ReportTitle", "My Report");
parameters.put("BaseDir", reportFile.getParentFile());
JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperReport,
parameters,
connection
);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
JRPdfExporter exporter = new JRPdfExporter();
// Get a pointer to the directory /tmp on the file system
File d = new File("/tmp");
// Now create a file with the prefix MyReport and suffix .pdf in /tmp.
// The filename will be something like MyReport19309.pdf
File f = File.createTempFile("MyReport", ".pdf", d) ;
// Create an outputstream to pass to the JasperReports exporter
OutputStream pout = new BufferedOutputStream(new FileOutputStream(f));
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, pout);
exporter.exportReport();
pout.flush(); // write any data left in the buffer
%>
Done outputting file.
No comments:
Post a Comment