jsreport / jsreport-core

The minimalist jsreport rendering core
GNU Lesser General Public License v3.0
86 stars 24 forks source link

How to reference scripts in jsreport-core #6

Closed 3goats closed 8 years ago

3goats commented 8 years ago

So I have this code which is working.

var jsreport = require('jsreport-core')()
//jsreport.use(require('jsreport-electron-pdf')({ strategy: 'electron-ipc' }));

var fs = require('fs');
//var data = fs.readFileSync('content.handlebars');
var path = require('path');
// make sure to pass the path to your `helper.js`
var helpers = fs.readFileSync(path.join('/Development/jsreport-new/data/templates/Sample report', 'helpers.js'), 'utf8');

jsreport.init().then(function () {
    jsreport.render({
        template: {
            content: fs.readFileSync(path.join('/Development/jsreport-new/data/templates/Sample report', 'content.handlebars'), 'utf8'),
            helpers: helpers,
            engine: 'handlebars',
            recipe: 'phantom-pdf',
            phantom: {
                "orientation": "portrait",
                "format": "A3",
                "margin": "3cm",
                "headerHeight": "3cm"
            },
        },
        data: {
            "books": [
                {"name": "A Tale of Two Cities", "author": "Charles Dickens", "sales": 351},
                {"name": "The Lord of the Rings", "author": "J. R. R. Tolkien", "sales": 125},
                {"name": "The Da Vinci Code", "author": "Dan Brown", "sales": 255},
                {"name": "The Hobbit", "author": "J. R. R. Tolkien", "sales": 99},
                {"name": "Carlskii", "author": "J. R. R. Tolkien", "sales": 99}
            ]
        }
    }).then(function(resp) {
        //prints pdf with headline Hello world
        console.log(resp.content.toString())
        resp.result.pipe(fs.createWriteStream('helloworld4.pdf'));
        setTimeout(function() {
            process.exit();
        }, 3000)
    });
}).catch(function(e) {
    console.log(e)
});

However, I also have some scripts that get data from an external resource. How do I reference them in jsreport-core ?

pofider commented 8 years ago

Docs for scripts extension includes the API section which describes how to do it http://jsreport.net/learn/scripts

It would be something like this

 jsreport.render({ template : { ...
    scripts: [{
        content: "request.template.content='hello'; done();"           
    }]      
})
3goats commented 8 years ago

Thanks does this need to be content: "request.template.content='hello'; done(); to return some data back to the template instead of overriding the entire content ?

3goats commented 8 years ago

OK I guess it like this content: "request.data={endpoints: 'hello'}; done();"

pofider commented 8 years ago

OK I guess it like this content: "request.data={endpoints: 'hello'}; done();"

Yes that should work

3goats commented 8 years ago

So the question now is since this is:

var data = fs.readFileSync(path.join('/Users/cbourne/Development/jsreport-new', 'scratch.json').toString(), 'utf8');
var json = JSON.parse(data);

is outside of the jsreport.render function, how do I get pass the data into the content ?

Is there an easy way to do this or do I need to something special ?

3goats commented 8 years ago

OK so this works for anyone else that needs it. The problem was that my json variable was not global.

content: "request.data= " + JSON.stringify(json) + " ; done();"