Reputation: 622
Here is my GeneratePdf.java Import ...
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 connection and get data ():
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;
}
}
I get this error but I dont understand it:
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
Upvotes: 1
Views: 5031
Reputation: 1
It resolved by adding proper dependencies.
MongoDbQueryExecuter required for MongoDbQuery used in your jrxml. and for that use below dependencies. https://mvnrepository.com/artifact/com.jaspersoft.connectors.mongodb/js-mongodb-datasource
make sure you have proper drives installed mongodb-driver-sync, mongodb-driver-legacy and mongodb-driver-core.
below is list of dependencies with versions I used in my project, and it was working fine.
// https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb implementation group: 'org.springframework.data', name: 'spring-data-mongodb', version: '4.1.2' // https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-sync implementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '4.10.2' // https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-legacy implementation group: 'org.mongodb', name: 'mongodb-driver-legacy', version: '4.10.2' // https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-core implementation group: 'org.mongodb', name: 'mongodb-driver-core', version: '4.10.2'
// https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports
implementation group: 'net.sf.jasperreports', name: 'jasperreports', version: '6.20.6'
// https://mvnrepository.com/artifact/com.github.librepdf/openpdf
implementation group: 'com.github.librepdf', name: 'openpdf', version: '1.3.30'
// https://mvnrepository.com/artifact/com.jaspersoft.connectors.mongodb/js-mongodb-datasource implementation group: 'com.jaspersoft.connectors.mongodb', name: 'js-mongodb-datasource', version: '3.10.2'
byte[] bytes = null;
String mongoURI = "mongodb://127.0.0.1:29002/yourdatabase";
MongoDbConnection connection = null;
try {
connection = new MongoDbConnection(mongoURI, null, null);
Map<String, Object> parameters = new HashMap<String, Object>();
final InputStream stream = this.getClass().getResourceAsStream("/test.jrxml");
final JasperReport report = JasperCompileManager.compileReport(stream);
final JasperPrint print = JasperFillManager.fillReport(report, parameters,connection);
bytes = JasperExportManager.exportReportToPdf(print);
}catch (Exception e){
System.out.println(e.getMessage());
}
Upvotes: 0
Reputation: 161
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
Upvotes: 0
Reputation: 9400
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();
}
}
Upvotes: 3