Compile a template with local assets



  • Hi,
    I need to compile a template by jsreport-core, but I cannot do that because the assets are not found.

    This is my code:
    const jsreport = require('jsreport-core')()

    jsreport.use(require('jsreport-assets')({
        searchOnDiskIfNotFoundInStore: true,
        allowedFiles: '/.',
    publicAccessEnabled: true
    }))

    var fs = require('fs')

    jsreport.init().then(() => {
    return jsreport.render({
    template: {
    content: fs.readFileSync('main'),
    helpers: fs.readFileSync('helpers.js'),
    engine: 'handlebars',
    recipe: 'chrome-pdf'
    },
    data: JSON.parse(fs.readFileSync('data.json'))
    }).then((resp) => {
    fs.writeFileSync('./report.pdf', resp.content)
    console.log('Saved!')
      });
    }).catch((e) => {
    console.error(e)
    })
     
    The assets (for example some images) are placed in the same directory of the template "main".
    First problem: when I try the code as above, the assets are searched in the installation directory of jsreport, but I would like in the directory where is placed the template (or the script).
    Second problem: if I move all the assets in the installation directory, the script fails with the error: "Engine 'handlebars' not found."
    Actually, this error happens always when this part of code is enabled:

    jsreport.use(require('jsreport-assets')({
        searchOnDiskIfNotFoundInStore: true,
        allowedFiles: '/.',
    publicAccessEnabled: true
    }))
    but I need it to use local assets!

    I hope someone can figure out the problem.
    Thank you!
    Best regards


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.