agmoyano / node-jasper

JasperReports from Node.js
83 stars 50 forks source link

node-jasper

JasperReports within Node.js

Install

Install via npm:

npm install --save node-jasper

To use it inside your project just do:

var jasper = require('node-jasper')(options);

Where options is an object with the following signature:

options: {
    path: , //Path to jasperreports-x.x.x directory (from jasperreports-x.x.x-project.tar.gz)
    reports: {
        // Report Definition
        "name": {
            jasper: , //Path to jasper file,
            jrxml: , //Path to jrxml file,
            conn: , //Connection name, definition object or false (if false defaultConn won't apply or if ´in_memory_json´ then you can pass an JSON object in the ´dataset´ property for in-memory data sourcing instead of database access

        }
    },
    drivers: {
        // Driver Definition
        "name": {
            path: , //Path to jdbc driver jar
            class: , //Class name of the driver (what you would tipically place in "Class.forName()" in java)
            type: //Type of database (mysql, postgres)
        }
    },
    conns: {
        // Connection Definition
        "name": {
            host: , //Database hostname or IP
            port: , //Database Port
            dbname: , //Database Name
            user: , //User Name
            pass: , //User Password
            jdbc: , //jdbc connection String. If this is defined, every thing else but user and pass becomes optional.
            driver: //name or definition of the driver for this conn
        }
    },
    defaultConn: ,//Default Connection name
    java: ,//Array of java options, for example ["-Djava.awt.headless=true"]
    javaInstnace: //Instance of node-java, if this is null, a new instance will be created and passed in 'java' property
 }

API

Example

var express = require('express'),
    app = express(),
    jasper = require('node-jasper')({
        path: 'lib/jasperreports-5.6.0',
        reports: {
            hw: {
                jasper: 'reports/helloWorld.jasper'
            }
        },
        drivers: {
            pg: {
                path: 'lib/postgresql-9.2-1004.jdbc41.jar',
                class: 'org.postgresql.Driver',
                type: 'postgresql'
            }
        },
        conns: {
            dbserver1: {
                host: 'dbserver1.example.com',
                port: 5432,
                dbname: 'example',
                user: 'johnny',
                pass: 'test',
                driver: 'pg'
            }
        }
        defaultConn: 'dbserver1'
    });

    app.get('/pdf', function(req, res, next) {
        //beware of the datatype of your parameter.
        var report = {
            report: 'hw',
            data: {
                id: parseInt(req.query.id, 10)
                secundaryDataset: jasper.toJsonDataSource({
                    data: ...
                },'data')
            }
            dataset: //main dataset
        };
        var pdf = jasper.pdf(report);
        res.set({
            'Content-type': 'application/pdf',
            'Content-Length': pdf.length
        });
        res.send(pdf);
    });

    app.listen(3000);

That's It!.