andrewloable / node-jasper-server

Host Jasper Reports in NodeJS
MIT License
10 stars 6 forks source link

jasper-node-server

Host Jasper Reports in NodeJS

Download

git clone git:github.com/andrewloable/jasper-node-server.git
npm install

Libraries

Put all Jasper Reports libraries (jar files) under the /libs folder. The easiest way to get the libraries is to copy all jar files from JasperReports Server under

<jasperserver>/apache-tomcat/webapps/jasperserver/WEB-INF/lib

Configure

All reports, drivers and connections are set in settings.json. A sample configuration is shown below.

{
    "reports":{
        "test1": {
            "jrxml": "/jrxml/test.jrxml"
        },
        "test2": {
            "jrxml": "/jrxml/test2.jrxml",
            "subreports":{
                "parametername1": {
                    "jrxml": "/jrxml/test2subreport1.jrxml"
                }
            }
        }
    },
    "drivers": {
        "postgresql": {
            "path": "/libs/postgresql-42.2.1.jar",
            "class": "org.postgresql.Driver"
        }
    },
    "connections": {
        "mytestdatabase": {
            "jdbc": "jdbc:postgresql://localhost:5432/mytestdatabase",
            "user": "myusername",
            "password": "mypassword"
        }
    }
}

Run

Execute the command below.

node index.js

This will run an express app that listens to port 3000 and will accept connections from localhost only.

API

Examples

To generate a report into PDF

{ "name": "test", "connection": "none", "parameters": { "Parameter1": "Parameter 1", "Parameter2": "PARAM2" } }


* **go**

package main

import ( "fmt" "strings" "net/http" "io/ioutil" )

func main() {

url := "http://localhost:3000/generate_pdf"

payload := strings.NewReader("{\n\t\"name\": \"test\",\n\t\"connection\": \"none\",\n\t\"parameters\": {\n\t\t\"Parameter1\": \"Parameter 1\",\n\t\t\"Parameter2\": \"PARAM2\"\n\t}\n}")

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("content-type", "application/json")
req.Header.Add("cache-control", "no-cache")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)

fmt.Println(res)
fmt.Println(string(body))

}


* **nodejs request**

var request = require("request");

var options = { method: 'POST', url: 'http://localhost:3000/generate_pdf', headers: { 'cache-control': 'no-cache', 'content-type': 'application/json' }, body: { name: 'test', connection: 'none', parameters: { Parameter1: 'Parameter 1', Parameter2: 'PARAM2' } }, json: true };

request(options, function (error, response, body) { if (error) throw new Error(error);

console.log(body); });


* **curl**

curl -X POST \ http://localhost:3000/generate_pdf \ -H 'cache-control: no-cache' \ -H 'content-type: application/json' \ -d '{ "name": "test", "connection": "none", "parameters": { "Parameter1": "Parameter 1", "Parameter2": "PARAM2" } }'


* **python requests**

import requests

url = "http://localhost:3000/generate_pdf"

payload = "{\n\t\"name\": \"test\",\n\t\"connection\": \"none\",\n\t\"parameters\": {\n\t\t\"Parameter1\": \"Parameter 1\",\n\t\t\"Parameter2\": \"PARAM2\"\n\t}\n}" headers = { 'content-type': "application/json", 'cache-control': "no-cache", }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)