Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ManfredGit/e8bb76abfa7a0240a8be to your computer and use it in GitHub Desktop.
Save ManfredGit/e8bb76abfa7a0240a8be to your computer and use it in GitHub Desktop.

Revisions

  1. @rponte rponte revised this gist Feb 27, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions GenerateSimplePdfReportWithJasperReports.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    package report;
    package rponte.report;

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    @@ -14,7 +14,8 @@
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.engine.export.JRPdfExporter;
    import report.ConnectionFactory;

    import rponte.report.ConnectionFactory;

    /**
    * You'll need these jar's below:
  2. @rponte rponte revised this gist Feb 27, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GenerateSimplePdfReportWithJasperReports.java
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ public static void main(String[] args) {
    exporter.exportReport();

    } catch (Exception e) {
    throw new RuntimeException("It's not possible to generate report.", e);
    throw new RuntimeException("It's not possible to generate the pdf report.", e);
    } finally {
    // it's your responsibility to close the connection, don't forget it!
    if (connection != null) {
  3. @rponte rponte revised this gist Feb 27, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions GenerateSimplePdfReportWithJasperReports.java
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ public static void main(String[] args) {

    String reportName = "myreport";
    Map<String, Object> parameters = new HashMap<String, Object>();
    connection = new ConnectionFactory().getConnection();
    connection = new ConnectionFactory().getConnection(); // opens a jdbc connection

    // compiles jrxml
    JasperCompileManager.compileReportToFile(reportName + ".jrxml");
    @@ -46,13 +46,14 @@ public static void main(String[] args) {
    // exports report to pdf
    JRExporter exporter = new JRPdfExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream(reportName + ".pdf")); // your output
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream(reportName + ".pdf")); // your output goes here

    exporter.exportReport();

    } catch (Exception e) {
    throw new RuntimeException("It's not possible to generate report.", e);
    } finally {
    // it's your responsibility to close the connection, don't forget it!
    if (connection != null) {
    try { connection.close(); } catch (Exception e) {}
    }
  4. @rponte rponte created this gist Feb 27, 2013.
    63 changes: 63 additions & 0 deletions GenerateSimplePdfReportWithJasperReports.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    package report;

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.util.HashMap;
    import java.util.Map;

    import net.sf.jasperreports.engine.JRException;
    import net.sf.jasperreports.engine.JRExporter;
    import net.sf.jasperreports.engine.JRExporterParameter;
    import net.sf.jasperreports.engine.JasperCompileManager;
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.engine.export.JRPdfExporter;
    import report.ConnectionFactory;

    /**
    * You'll need these jar's below:
    *
    * jasperreports-5.0.1.jar
    * iText-2.1.7.jar (needed to generate PDF)
    * jfreechart-1.0.12.jar (needed to graphics and charts)
    * jcommon-1.0.15.jar (needed to graphics and charts)
    * commons-beanutils-1.8.2.jar
    * commons-collections-3.2.1.jar
    * commons-digester-2.1.jar
    * commons-logging-1.1.jar
    */
    public class GenerateSimplePdfReportWithJasperReports {

    public static void main(String[] args) {

    Connection connection = null;
    try {

    String reportName = "myreport";
    Map<String, Object> parameters = new HashMap<String, Object>();
    connection = new ConnectionFactory().getConnection();

    // compiles jrxml
    JasperCompileManager.compileReportToFile(reportName + ".jrxml");
    // fills compiled report with parameters and a connection
    JasperPrint print = JasperFillManager.fillReport(reportName + ".jasper", parameters, connection);
    // exports report to pdf
    JRExporter exporter = new JRPdfExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream(reportName + ".pdf")); // your output

    exporter.exportReport();

    } catch (Exception e) {
    throw new RuntimeException("It's not possible to generate report.", e);
    } finally {
    if (connection != null) {
    try { connection.close(); } catch (Exception e) {}
    }
    }

    }

    }