agmoyano / node-jasper

JasperReports from Node.js
83 stars 51 forks source link

Could you pls provide a example of how to user the dataset attribute in report JSON. #52

Open joseespinozac opened 4 years ago

joseespinozac commented 4 years ago

I would love a more detailed example on how to use or declare the dataset in the report JSON

let report = {
        report: 'usersreport',
        data: {
            name: 'Joe'
        },
        dataset: jasper.toJsonDataSource({  how to send this, and how I need to build my jrxml to use the dataser I declare here
            name: 'Joe',
            hola: 'mundo'
        },'data')
    };

Sorry for my english And please tell me if I can send arrays? and how to do it

dcsline commented 3 years ago

look in my tested content for sample js: var fs = require('fs');

var jasper = require('node-jasper')({ path: '../lib/jasperreports-5.6.0', reports: { stock_ofertas: { jasper: dirname + '/sample.jasper', jrxml: dirname + '/sample.jrxml', conn: 'in_memory_json' } } });

jasper.ready(function() { var r = jasper.export( { report: 'stock_ofertas', data: { language: 'spanish', // on jasper make a parameter named "dataset2" and use on a subreport: // ((net.sf.jasperreports.engine.data.JsonDataSource)$P{dataset2}) dataset2: jasper.toJsonDataSource( { dados: [{ value: 1, value: 2 }] }, 'dados' ) }, dataset: [ { name: 'Gonzalo', lastname: 'Vinas' // TODO: check on UTF-8 }, { name: 'Agustin', lastname: 'Moyano' } ] }, 'pdf' );

//fs.writeFile(__dirname + '/sample.pdf', r);
fs.writeFile(__dirname+ '/sample.pdf', r, (error)=>{
    if(error) console.log(error);
})

});

dcsline commented 3 years ago

Why you want build an json from an array? If you want do this, then look for tools which are build the json from an array.