jsreport / jsreport-core

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

jsreport chart-pdf #19

Closed vinithios closed 7 years ago

vinithios commented 7 years ago

I am getting empty pdf page while using jsreport. Below is my code which am i using in my client side,

<script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
      type: 'line',
      data: {
        labels: ['M', 'T', 'W', 'T', 'F', 'S', 'S'],
        datasets: [{
          label: 'apples',
          data: [12, 19, 3, 17, 6, 3, 7],
          backgroundColor: "rgba(153,255,51,0.4)"
        }, {
          label: 'oranges',
          data: [2, 29, 5, 5, 2, 3, 10],
          backgroundColor: "rgba(255,153,0,0.4)"
        }]
      },
      options: {
            animation: {
                onComplete: function () {
                    window.JSREPORT_READY_TO_START = true
            }
        }
    });
</script>     

Server side code: router.get('/', function(req, res, next){ var data = { template: { content: fs.readFileSync(path.join(rootPath, "public/views/chart.html"), 'utf8'), recipe: 'phantom-pdf', engine: 'handlebars' }, data: { country: "Brazil",

    },
    options: {
        preview: true
    }
};
var options = {
    uri: 'http://localhost:8001/api/report',
    method: 'POST',
    json: data
};
request(options).pipe(res);

});

I configured jsreport config file as below, "phantom": { "strategy": "dedicated-process", "timeout": 180000, "waitForJS": true }, "tasks": { "strategy": "dedicated-process", "timeout": 1000000, "allowedModules": "" }, "scripts": { "allowedModules": "", "timeout": 1000000 },

But , i am getting empty pdf.Can anyone help me to solve this issue??

bjrmatos commented 7 years ago

phantom's waitForJS option is a template level option, if you are sending your template content with json, you need to set that option in your template property.

change this:

template: {
content: fs.readFileSync(path.join(rootPath, "public/views/chart.html"), 'utf8'),
recipe: 'phantom-pdf',
engine: 'handlebars'
}

to this:

template: {
  content: fs.readFileSync(path.join(rootPath, "public/views/chart.html"), 'utf8'),
  recipe: 'phantom-pdf',
  engine: 'handlebars',
  phantom: {
    // template level options for phantomjs
    "waitForJS": true
  }
}
vinithios commented 7 years ago

Thanks for your reply. After i changed my code, i am getting the below error,

{"message":"Error during rendering report: Timeout when executing in phantom","stack":"Error: Timeout when executing in phantom\n at Timeout._onTimeout (/home/ionixx/jsReports/node_modules/phantom-html-to-pdf/lib/dedicatedProcessStrategy.js:81:26)\n at ontimeout (timers.js:488:11)\n at Timer.unrefdHandle (timers.js:599:5)"}

I just set the phantom's timeout property as 1000

Code: "phantom": { "strategy": "dedicated-process", "timeout": 1000 }, "tasks": { "strategy": "dedicated-process", "timeout": 1000000, "allowedModules": "" }, "scripts": { "allowedModules": "", "timeout": 1000000 },

vinithios commented 7 years ago

Is there any fix for this issue??

vinithios commented 7 years ago

Last issue was fixed..I made silly mistake at javascript.(parenthesis not properly closed).After i make it close, the issue has been resolved. Thanks @bjrmatos, for your quick response.