GeneratePDF with JasperReports Library and MongoDB

前端 未结 2 1292
野性不改
野性不改 2020-12-06 23:52

Here is my GeneratePdf.java Import ...

  public class GeneratePdf {
        public static void main(String[] args) {
        try {
            JRDataSou         


        
相关标签:
2条回答
  • 2020-12-07 00:16

    Using the JRBeanCollectionDataSource is not the right way to go about using the MongoDB connector. Take a look at this test that comes with the Jaspersoft MongoDB Connector source:

    MongoDbDatasource/src/test/java/com/jaspersoft/mongodb/ReportTest.java

    Both the binary connector and the source are on the project page.

    To keep this answer self-contained, here's a code snippet showing how to fill a MongoDB report. It's a modified extract from the file I mention above.

    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();
      }
    }
    
    0 讨论(0)
  • 2020-12-07 00:26

    You need to add following line:

    JRProperties.setProperty("net.sf.jasperreports.query.executer.factory.MongoDbQuery", "com.jaspersoft.mongodb.query.MongoDbQueryExecuterFactory");
    

    Verify path in datasource jar file, e.g. js-mongodb-datasource-0.5.0

    0 讨论(0)
提交回复
热议问题