使用JasperReports Library和MongoDB生成PDF

时间:2012-02-17 16:58:44

标签: java mongodb jasper-reports mongo-java

这是我的GeneratePdf.java     导入......

  public class GeneratePdf {
        public static void main(String[] args) {
        try {
            JRDataSource ds = getDatasource();
            // - Chargement et compilation du rapport
line32      JasperDesign jasperDesign = JRXmlLoader.load("/home/gocoffee.jrxml");
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

            // - Paramètres à envoyer au rapport
            Map parameters = new HashMap();
            parameters.put("Titre", "Titre");

            // - Execution du rapport
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
parameters, ds);

            // - Création du rapport au format PDF
            JasperExportManager.exportReportToPdfFile(jasperPrint, "home/test2.pdf");
        } catch (JRException e) {
        }
    }

Mongo连接并获取数据():

private static JRDataSource getDatasource() {
        // Retrieve session
        try{
            Mongo m = new Mongo("localhost", 27017);
            DB db = m.getDB("test");
            DBCollection t = db.getCollection("test");
            List<DBObject> list = t.getIndexInfo();
            JRDataSource ds = new JRBeanCollectionDataSource(list);
            return ds;
        } catch (UnknownHostException e) {
            System.out.println("Error mongo connection");
        } catch (Exception e) {
            System.out.println("Other Exception");
        }
        return null;
    }
}

我收到此错误但我不明白:

run:
17-Feb-2012 17:07:26 org.apache.commons.digester.Digester endElement
SEVERE: End event threw exception
java.lang.reflect.InvocationTargetException
....
....
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:156)
    at GeneratePdf.main(GeneratePdf.java:32)

Caused by: net.sf.jasperreports.engine.JRRuntimeException: No query executer factory registered for the 'MongoDbQuery' language.
    at net.sf.jasperreports.engine.util.JRQueryExecuterUtils.getQueryExecuterFactory(JRQueryExecuterUtils.java:64)
    at net.sf.jasperreports.engine.design.JRDesignDataset.queryLanguageChanged(JRDesignDataset.java:1122)
    at net.sf.jasperreports.engine.design.JRDesignDataset.setQuery(JRDesignDataset.java:600)
    at net.sf.jasperreports.engine.design.JasperDesign.setQuery(JasperDesign.java:789)
    ... 28 more

2 个答案:

答案 0 :(得分:3)

使用JRBeanCollectionDataSource不是使用MongoDB连接器的正确方法。看看Jaspersoft MongoDB Connector源代码附带的这个测试:

MongoDbDatasource / SRC /测试/ JAVA / COM /的JasperSoft / mongodb的/ ReportTest.java

二进制连接器和源都位于project page

为了使这个答案保持独立,这里有一段代码片段,展示如何填写MongoDB报告。它是我上面提到的文件的修改后的摘录。

String mongoURI = "mongodb://bdsandbox6:27017/test";
MongoDbConnection connection = null;
Map<String, Object> parameters = new HashMap<String, Object>();
try {
  connection = new MongoDbConnection(mongoURI, null, null);
  parameters.put(MongoDbDataSource.CONNECTION, connection);
  File jasperFile;
  jasperFile = new File("MongoDbReport.jasper");
  JasperCompileManager.compileReportToFile("MongoDbReport.jrxml", "MongoDbReport.jasper");
  JasperFillManager.fillReportToFile("MongoDbReport.jasper", parameters);
  JasperExportManager.exportReportToPdfFile("MongoDbReport.jrprint");
}
} catch (Exception e) {
  e.printStackTrace();
} finally {
  if (connection != null) {
    connection.close();
  }
}

答案 1 :(得分:0)

您需要添加以下内容:
JRProperties.setProperty(“net.sf.jasperreports.query.executer.factory.MongoDbQuery”,“com.jaspersoft.mongodb.query.MongoDbQueryExecuterFactory”); 验证数据源jar文件中的路径,例如JS-mongodb的-数据源-0.5.0