Pdf reports from mongodb

09-27-2014 09:18

Rendering pdf reports from mongodb is a piece of cake with the new jsreport mongodb extension. You don't need any application with API above mongo. Only thing you need is jsreport and valid accessible mongodb endpoint.

How does it work...

First download and install jsreport to your machine or log into reports as a service solution. When downloading jsreport to your machine you need to also install mongodb extension which is pre installed in the online service solution.

Next you need to create a custom node.js script accessing your mongodb and loading data you need. The script creation and evaluation is handled by jsreport standard scripts extension and it can look like this:

//we can use mongodb node.js npm module
var MongoClient = require('mongodb').MongoClient; 
MongoClient.connect('mongodb://', function(err, db) {
    var collection = db.collection('people').find().toArray(function(err, results) {
        //override report incoming data with results from mongo
        request.data = results;
        //notify jsreport we are done

Custom script will prepare data for the report and now we can iterate over them and generate html from it using javascript templating engines jsreport supports.

{{for people}}

The output html will be then converted into pdf using jsreport default recipe using phantomjs.