package nable.ems.iReport.smsReport.util; import java.io.*; import java.sql.*; import java.util.*; import net.sf.jasperreports.engine.*; import net.sf.jasperreports.engine.base.*; import net.sf.jasperreports.engine.export.*; import net.sf.jasperreports.engine.util.*; /** *
Title: Jasper Exporter to WEB, XLS, PDF ... ...
*Description: Jasper Report Exporter to other service
*Copyright: Copyright (c) 2006 kkckc(kkckc@nablecomm.com)
*Company: nablecomm.com
* * use : * JasperExporter exp = JasperExporter(String _fileName, String _reportPATH, String _savePATH, String _readPATH); * // pdf * exp.pdfExporter(conn, map); // connection, HashMap parameters * exp.getPDFPATH(); * // xls * exp.xlsExporter(conn, map); * exp.getXLSPATH(); * // html * String htmlDATA = exp.htmlExporter(conn, map); * request.setAttribute("htmlDATA", htmlDATA); * * @author kkckc(kkckc@nablecomm.com * @version 1.0 */ public class JasperExporter { // Save PATH : Absolute Directory in OS (EX: /nable/ems/report/emsreport) private String savePATH; // read PATH : Web Direction in Web Service (EX: /report/ems_report) private String readPATH; // report PATH : Absoulte PATH that direct Jasper(Compiled JasperReport) File (EX: /nable/ems/Test.jasper) private String reportPATH; // File Name : Used for Save/Load File Name (Ex:Test) private String fileName; // save Temp html/pdf/xls/img File Dir private String htmlFile, pdfFile, xlsFile, imgFiles; // private JasperReport jasperReport = null; private JasperPrint jasperPrint = null; private boolean ismargin = false; /** * Init For Jasper Export * @param _fileName String FileName (Ex:viReport) * @param _reportPATH String .Jasper File PATH (EX: /nable/ems/Test.jasper) * @param _savePATH String Absolute Directory in OS (EX: /nable/ems/report/emsreport) * @param _readPATH String Web Direction in Web Service (EX: /report/ems_report) */ public JasperExporter(String _fileName, String _reportPATH, String _savePATH, String _readPATH) { savePATH = _savePATH; readPATH = _readPATH; reportPATH = _reportPATH; fileName = _fileName; htmlFile = savePATH + File.separator + fileName + ".jsp"; pdfFile = savePATH + File.separator + fileName + ".pdf"; xlsFile = savePATH + File.separator + fileName + ".xls"; imgFiles = readPATH + File.separator + fileName + ".jsp_files" + File.separator; try { jasperReport = (JasperReport)JRLoader.loadObject(reportPATH); } catch (Exception ex) { ex.printStackTrace(); } } /** * Make Margin for PDF Export */ public void setDefaultMargin() { int widthMargin = 30; int heightMargin = 30; try { java.lang.reflect.Field margin = JRBaseReport.class.getDeclaredField( "leftMargin"); margin.setAccessible(true); margin.setInt(jasperReport, jasperReport.getLeftMargin() + widthMargin); margin = JRBaseReport.class.getDeclaredField("rightMargin"); margin.setAccessible(true); margin.setInt(jasperReport, jasperReport.getRightMargin() + widthMargin); margin = JRBaseReport.class.getDeclaredField("topMargin"); margin.setAccessible(true); margin.setInt(jasperReport, jasperReport.getTopMargin() + heightMargin); margin = JRBaseReport.class.getDeclaredField("bottomMargin"); margin.setAccessible(true); margin.setInt(jasperReport, jasperReport.getBottomMargin() + heightMargin); margin = JRBaseReport.class.getDeclaredField("pageWidth"); margin.setAccessible(true); margin.setInt(jasperReport, jasperReport.getPageWidth() + widthMargin * 2); margin = JRBaseReport.class.getDeclaredField("pageHeight"); margin.setAccessible(true); margin.setInt(jasperReport, jasperReport.getPageHeight() + heightMargin * 2); } catch (Exception ex) { ex.printStackTrace(); } ismargin = true; } /** * Make No margin for Export */ public void setNoMargin() { int widthMargin = 0; int heightMargin = 0; try { java.lang.reflect.Field margin = JRBaseReport.class.getDeclaredField( "leftMargin"); margin.setAccessible(true); margin.setInt(jasperReport, jasperReport.getLeftMargin() + widthMargin); margin = JRBaseReport.class.getDeclaredField("rightMargin"); margin.setAccessible(true); margin.setInt(jasperReport, jasperReport.getRightMargin() + widthMargin); margin = JRBaseReport.class.getDeclaredField("topMargin"); margin.setAccessible(true); margin.setInt(jasperReport, jasperReport.getTopMargin() + heightMargin); margin = JRBaseReport.class.getDeclaredField("bottomMargin"); margin.setAccessible(true); margin.setInt(jasperReport, jasperReport.getBottomMargin() + heightMargin); margin = JRBaseReport.class.getDeclaredField("pageWidth"); margin.setAccessible(true); margin.setInt(jasperReport, jasperReport.getPageWidth() + widthMargin * 2); margin = JRBaseReport.class.getDeclaredField("pageHeight"); margin.setAccessible(true); margin.setInt(jasperReport, jasperReport.getPageHeight() + heightMargin * 2); } catch(Exception ex) { ex.printStackTrace(); } ismargin = false; } public void setJasperPrint(Connection conn, HashMap map) { try { // jasperPrint¸¦ »ý¼º jasperPrint = JasperFillManager.fillReport(jasperReport, map, conn); } catch (Exception ex) { ex.printStackTrace(); } finally { try { // ²÷À¸¸é ¾Æ´ÏµÈ´Ù. // conn.close(); } catch (Exception ex) { ex.printStackTrace(); } } } /** * htmlExporter : Caution // HTML contents must be export with parameter or attribute * @return String HTML contents of Report */ public String htmlExporter(Connection conn, HashMap map) { if (jasperPrint == null) { setJasperPrint(conn, map); } else { if (ismargin) { setNoMargin(); setJasperPrint(conn, map); } } JRHtmlExporter exporter = null; StringBuffer str = new StringBuffer(); String str2 = ""; try { // custom fixable String HEADERStr = "<%@ page contentType=\"text/html; charset=utf-8\" %><%response.setContentType(\"text/html; charset=UTF-8\");%>"; HEADERStr += "<% response.setHeader (\"Pragma\", \"No-cache\"); response.setDateHeader (\"Expires\", 0); response.setHeader (\"Cache-Control\", \"no-cache\"); %>"; exporter = new JRHtmlExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, HEADERStr); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imgFiles); exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, str); Map imagesMap = new HashMap(); exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap); exporter.exportReport(); String[] imgKeys = (String[])imagesMap.keySet().toArray(new String[0]); /** * Todo : have to fix this method but perhaps no way... */ exporter = new JRHtmlExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, HEADERStr); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, htmlFile); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imgFiles); exporter.exportReport(); // Used for reload str2 = convertImg(str.toString(), imgKeys); } catch (Exception ex) { ex.printStackTrace(); } return str2; } /** * Set Image Path From to for Refresh Image * @param str String Source HTML * @param imgKeys String[] image String * @return String Converted HTML */ private String convertImg(String str, String imgKeys[]) { for (int i = 0; i < imgKeys.length; i++) { str = str.replaceAll("/" + imgKeys[i]+"\"", "/" + imgKeys[i] + "\\?rnd=" + Integer.toString( (int) (Math.random() * 100))+"\""); } return str; } /** * PDF Exporter * @param conn Connection Default connection * @param map HashMap Parameters used for JasperReports */ public void pdfExporter(Connection conn, HashMap map) { try { // init if (jasperPrint == null) { setDefaultMargin(); setJasperPrint(conn, map); } else { if (!ismargin) { setDefaultMargin(); setJasperPrint(conn, map); } } JRPdfExporter exporte = new JRPdfExporter(); exporte.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporte.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, pdfFile); exporte.exportReport(); } catch (Exception ex) { ex.printStackTrace(); } } public void xlsExporter(Connection conn, HashMap map) { try { if (jasperPrint == null) { setJasperPrint(conn, map); } else { if (ismargin) { setNoMargin(); setJasperPrint(conn, map); } } JRXlsExporter exporte = new JRXlsExporter(); exporte.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporte.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, xlsFile); exporte.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); exporte.exportReport(); } catch (Exception ex) { ex.printStackTrace(); } } /** * Get The PATH of generated XLS File * @return String XLS PATH */ public String getXLSPATH() { return readPATH + File.separator + fileName + ".xls"; } /** * Get The PATH of generated XLS File * @return String PDF PATH */ public String getPDFPATH() { return readPATH + File.separator + fileName + ".pdf"; } public static void main(String argv[]) { } }