jsreport / jsreport-core

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

Having error while using JSRender with Phantom-pdf #32

Closed anaszgh closed 6 years ago

anaszgh commented 6 years ago

Hello,

I'm trying to use JsReport-core with Phantom-pdf to generate pdf out of html

Here is the implementation

module.exports = function (callback) {
    var jsreport = require('jsreport-core')({
        rootDirectory:'C:\\Eventerest\\Server\\Eventerest.Server\\Eventerest.Server.APIs'
    })

    jsreport.init().then(function () {
        return jsreport.render({
            template: {
                content: '<h1>Hello {{:foo}}</h1>',
                engine: 'jsrender',
                recipe: 'phantom-pdf'
            },
            data: {
                foo: "world"
            }
        }).then(function (resp) {
            callback(/* error */ null, resp.content.toJSON().data);
        });
    }).catch(function (e) {
        callback(/* error */ e, null);
    })
};

It seems that it always tried to run it from the TEMP folder, even when i specify the root directory And it returns this error (on both Windows & MacOS High Sierra)

Error when loading extension phantom-pdf
Error: ENOENT: no such file or directory, scandir 'C:\\Users\\AnasZgh\\AppData\\Local\\Temp\\node_modules'
 at crawlAvailiblePhantomVersions (C:\\Eventerest\\Server\\Eventerest.Server\\Eventerest.Server.APIs\\node_modules\\jsreport-phantom-pdf\\lib\\phantom.js:69:12)
    at Phantom.init (C:\\Eventerest\\Server\\Eventerest.Server\\Eventerest.Server.APIs\\node_modules\\jsreport-phantom-pdf\\lib\\phantom.js:81:10)
    at Object.module.exports (C:\\Eventerest\\Server\\Eventerest.Server\\Eventerest.Server.APIs\\node_modules\\jsreport-phantom-pdf\\lib\\phantom.js:244:36)
    at Object._useOne (C:\\Eventerest\\Server\\Eventerest.Server\\Eventerest.Server.APIs\\node_modules\\jsreport-core\\lib\\extensions\\extensionsManager.js:154:75)
    at Object._useMany (C:\\Eventerest\\Server\\Eventerest.Server\\Eventerest.Server.APIs\\node_modules\\jsreport-core\\lib\\extensions\\extensionsManager.js:73:20)
    at <anonymous>
Error: Error when loading extension phantom-pdf
Error: ENOENT: no such file or directory, scandir 'C:\\Users\\AnasZgh\\AppData\\Local\\Temp\\node_modules'
    at crawlAvailiblePhantomVersions (C:\\Eventerest\\Server\\Eventerest.Server\\Eventerest.Server.APIs\\node_modules\\jsreport-phantom-pdf\\lib\\phantom.js:69:12)
    at Phantom.init (C:\\Eventerest\\Server\\Eventerest.Server\\Eventerest.Server.APIs\\node_modules\\jsreport-phantom-pdf\\lib\\phantom.js:81:10)
   at Object.module.exports (C:\\Eventerest\\Server\\Eventerest.Server\\Eventerest.Server.APIs\\node_modules\\jsreport-phantom-pdf\\lib\\phantom.js:244:36)
   at Object._useOne (C:\\Eventerest\\Server\\Eventerest.Server\\Eventerest.Server.APIs\\node_modules\\jsreport-core\\lib\\extensions\\extensionsManager.js:154:75)
   at Object._useMany (C:\\Eventerest\\Server\\Eventerest.Server\\Eventerest.Server.APIs\\node_modules\\jsreport-core\\lib\\extensions\\extensionsManager.js:73:20)
   at <anonymous>
    at Object._useOne (C:\\Eventerest\\Server\\Eventerest.Server\\Eventerest.Server.APIs\\node_modules\\jsreport-core\\lib\\extensions\\extensionsManager.js:163:15)
   at <anonymous>

And here is the dependencies section of the package.json

  "dependencies": {
    "jsreport-core": "^2.0.1",
    "jsreport-jsrender": "^2.0.0",
    "jsreport-phantom-pdf": "^2.0.1"
  }
anaszgh commented 6 years ago

I managed to fix this issue, i have found that in the configuration of jsreport-core you pass a property called rootDirectory, but in the jsreport-phantom-pdf it uses appDirectory

From jsreport-phantom-pdf Line:65

var npm = path.join(this.reporter.options.appDirectory, 'node_modules')

so in the constructor of the jsreport-core i have passed these values in the constructor

var jsreport = require('jsreport-core')({
        rootDirectory:__dirname,
        tempDirectory:__dirname,
        appDirectory:__dirname
    })

and that fixed it 😄