Jasper engine dynamical margins
Today I had to create reports in jasper that can dynamical change page margins, I try to create parameters that will fill page margins and try style, but in the end it didn't work. So I create a small j2ee web aplications that has HttpServlet request wich generates reports based on margins params that servlet gets. i had to include jasper report libs in project in eclipse.(commons-beanutils-1.8.2.jar, commons-collections-3.2.1.jar, commons-digester-2.1.jar, commons-logging-1.1.1.jar, iText-2.1.7.jar, jasperreports-4.6.0.jar, jasperreports-extensions-3.5.3.jar, jasperreports-fonts-4.5.0.jar, ojdbc6.jar)
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");/*
* get params margins for report
*/
String _LMarg = request.getParameter("LeftMarg");
String _RMarg = request.getParameter("RightMarg");
String _TMarg = request.getParameter("TopMarg");
String _BMarg = request.getParameter("BottomMarg");
String _jxmLTemplate = request.getParameter("jxmlTemplate");/*
* get jdbc connection.
*/
Connection connection = DB.getConnection();
/*
* find the tamplete on the disc, template are in *WEB-INF/jasper_template/ folder, base on param
* jxmlTemplate load that template in JasperDesign object
*/
File JTempleate = getFileTmp(_jxmLTemplate);
if (!JTempleate.exists()) {
throw new Exception("No report found");
}
JasperDesign jasTemplete = JRXmlLoader.load(JTempleate.getPath());
/*
*set up margins
*/
if (_TMarg != null && _TMarg != "") {
jasTemplete.setTopMargin(Integer.parseInt(_TMarg));
}
if (_LMarg != null && _LMarg != "") {
jasTemplete.setLeftMargin(Integer.parseInt(_LMarg));
}
if (_BMarg != null && _BMarg != "") {
jasTemplete.setBottomMargin(Integer.parseInt(_BMarg));
}
if (_RMarg != null && _RMarg != "") {
jasTemplete.setRightMargin(Integer.parseInt(_RMarg));
}
/*
* and this is trick part, when you set margins you also must set *column width, otherwise jasper cannot compile report for pdf
*/
/**
* sums to margins end from template column width reduce the width
**/
int VerticalMarg = getVerticalMarg(_LMarg,_RMarg);
jasTemplete.setColumnWidth(jasTemplete.getColumnWidth() - VerticalMarg);
/*
* compile report and save temp on disc
*/
JasperCompileManager.writeReportToXmlFile(jasTemplete, destJRXML);
JasperCompileManager.compileReportToFile(destJRXML, destination);
byte[] bytes = null;
/**
* add parameters from post
**/
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("_ID", request.getParameter("_id"));
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File(destination));
/*
*run report to pdf
*/
bytes = JasperRunManager.runReportToPdf(jasperReport, parameters,connection);
/*
*stream report back to client
*/
response.setHeader("Content-Disposition", content);
response.setContentType("application/pdf");
response.setCharacterEncoding("UTF-8");
ServletOutputStream servletOutputStream = response.getOutputStream();
response.setContentLength(bytes.length);
servletOutputStream.write(bytes, 0, bytes.length);
Comments
Post a Comment