npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-jasper-ts

v1.2.5

Published

JasperReports within Node.js

Downloads

92

Readme

node-jasper-ts

JasperReports within Node.js using typescript

Adaptation

Adapting the package: node-jasper: node-jasper

Install

Install via npm:

npm install --save node-jasper-ts

To use it inside your project just do:

import { JasperCompile, JasperConfig, JasperCompileFolder, JasperParameters, JasperParametersFolder } from 'node-jasper-ts';
import * as fs from 'fs';
require('dotenv').config();

var jasperReport = JasperConfig({
    reports: {
        'main': {
            jrxml: 'jrxml/test/rel_teste.jrxml',
            conn: 'default',
        },
        'sub': {
            jrxml: 'jrxml/test/rel_teste_subreport1.jrxml',
            conn: 'default',
        }
    },
    drivers: {
        'oracle': {
            path: 'jar/dist/ojdbc11g.jar',
            class: 'oracle.jdbc.driver.OracleDriver',
            type: 'oracle'
        }
    },
    conns: {
        'default': {
            user: 'USERDB',
            pass: 'PASSWORD',
            jdbc: 'jdbc:oracle:thin:@266.266.262.260:82821:ORCL',
            driver: 'oracle'
        }
    },
    defaultConn: 'default',
    tmpPath: 'jrxml/test',
    java: ["-Djava.awt.headless=true"]
})

jasperReport.compileJRXMLInDirSync({ dir: "jrxml/test" });

jasperReport.docx({
    report: 'main',
    data: {
        id: 1
    },
}).then((result) => {
    fs.writeFileSync("exported/test.docx", Buffer.from(result, 'binary'));
    console.log("Arquivo gerado com sucesso!");
    process.exit(0);
}).catch((err: any) => {
    console.log(err);
    process.exit(1);
})

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

  • java

    Instance of node-java that we are currently running.

  • compileJRXMLInDirSync({ dir, dstFolder? })

    Compiles all jrxml files into a jasper file within the specified folder, saving to the temp folder.

  • compileAllSync(dstFolder?)

    Compiles all jrxml of the configuration into a jasper file inside the temp folder.

  • compileSync(jrxmlFile, dstFolder?)

    Compiles a jrxml file into a jasper file, saving in the temp folder.

  • getParametersSync({ jrxml?, jasper? })

    Gets the file parameters, either jasper or jrxml.

  • getAllParametersSync({ path, grouped})

    Gets the file parameters, either jasper or jrxml.

  • add(name, report)

    Add a new report definition identified by name.

    In report definition one of jasper or jrxml must be present.

  • pdf(report)

    Alias for export(report, 'pdf')

  • html(report)

    Alias for export(report, 'html')

  • xml(report)

    Alias for export(report, 'xml', embeddingImages boolean)

  • docx(report)

    Alias for export(report, 'docx')

  • xlsx(report)

    Alias for export(report, 'xlsx')

  • pptx(report)

    Alias for export(report, 'pptx')

  • export(report, format)

    Returns the compiled report in the specified format.

    report can be of any of the following types:

    • A string that represents report's name. No data is supplied.. defaultConn will be applied to get data with reports internal query.

    • An object that represents report's definition. No data is supplied.. if conn is not present, then defaultConn will be applied to get data with reports internal query.

    • An object that represents reports, data and properties to override for this specific method call.

      {
        report: , //name, definition or an array with any combination of both
        data: {}, //Data to be applied to the report. If there is an array of reports, data will be applied to each.
        override: {} //properties of report to override for this specific method call.
        dataset: {} //an object to be JSON serialized and passed to the Report as fields instead of parameters (see the example for more info)
        query: '' // string to pass to jasperreports to query on the dataset
      }
    • An array with any combination of the three posibilities described before.

    • A function returning any combination of the four posibilities described before.

Example

import * as express from 'express';
var app = express();

    var jasper = JasperConfig({
                reports: {
                    'main': {
                        jrxml: 'jrxml/test/rel_teste.jrxml',
                        conn: 'default',
                    }
                },
                drivers: {
                    'oracle': {
                        path: 'jar/dist/ojdbc11g.jar',
                        class: 'oracle.jdbc.driver.OracleDriver',
                        type: 'oracle'
                    }
                },
                conns: {
                    'default': {
                        user: 'USERDB',
                        pass: 'PASSWORD',
                        jdbc: 'jdbc:oracle:thin:@266.266.262.260:12821:ORCL',
                        driver: 'oracle'
                    }
                },
                defaultConn: 'default',
                tmpPath: 'jrxml/test',
                java: ["-Djava.awt.headless=true"]
    });

    jasper.compileJRXMLInDirSync("jrxml/test");

	app.get('/pdf', function(req, res, next) {
		//beware of the datatype of your parameter.
		var report = {
			report: 'main',
			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!.