I am using pdfMake to export some html content and amCharts graphs to pdf. I used this amCharts demo which is perfect to me it presents the code I need. Well I copied exactly the code, I only added $scope to the function and now I have
$scope.exportReport = function() {
//the same code existing in the codepen.io
}
I got this error:
TypeError: Cannot read property 'capture' of undefined
at ChildScope.$scope.exportReport (reporting.controller.js:416)
at fn (eval at compile (angular.js:15126), <anonymous>:4:227)
at expensiveCheckFn (angular.js:16213)
at callback (angular.js:26592)
at ChildScope.$eval (angular.js:17994)
at ChildScope.$apply (angular.js:18094)
at HTMLInputElement.<anonymous> (angular.js:26597)
at HTMLInputElement.dispatch (jquery.min.js:3)
at HTMLInputElement.q.handle (jquery.min.js:3)
so the code won't work because chart["export"] is undefined and I don't know why!
chart["export"].capture({}, function() {
this.toJPG({}, function(data) {
// Save chart data into chart object itself
this.setup.chart.exportedImage = data;
// Reduce the remaining counter
charts_remaining--;
// Check if we got all of the charts
if (charts_remaining == 0) {
// Yup, we got all of them
// Let's proceed to putting PDF together
generatePDF();
}
});
});
I installed all the necessary libraries and files! My code :
$scope.exportReport = function() {
// So that we know export was started
console.log("Starting export...");
// Define IDs of the charts we want to include in the report
var ids = ["chartdiv1", "chartdiv2"];
// Collect actual chart objects out of the AmCharts.charts array
var charts = {},
charts_remaining = ids.length;
for (var i = 0; i < ids.length; i++) {
for (var x = 0; x < AmCharts.charts.length; x++) {
if (AmCharts.charts[x].div.id == ids[i])
charts[ids[i]] = AmCharts.charts[x];
}
}
// Trigger export of each chart
for (var x in charts) {
if (charts.hasOwnProperty(x)) {
var chart = charts[x];
chart["export"].capture({}, function() {
this.toJPG({}, function(data) {
// Save chart data into chart object itself
this.setup.chart.exportedImage = data;
// Reduce the remaining counter
charts_remaining--;
// Check if we got all of the charts
if (charts_remaining == 0) {
// Yup, we got all of them
// Let's proceed to putting PDF together
generatePDF();
}
});
});
}
}
function generatePDF() {
// Log
console.log("Generating PDF...");
// Initiliaze a PDF layout
var layout = {
"content": [],
"styles": {
"header": {
"fontSize": 18,
"bold": true,
"background": 'pink'
}
}
};
layout.content.push({
"columns": [{
"width": "33.33%",
"image": document.getElementById("logo").innerHTML,
"fit": [250, 300]
}, {
"width": "*",
"image": charts["chartdiv1"].exportedImage,
"fit": [250, 300]
}],
"columnGap": 10
});
// Trigger the generation and download of the PDF
// We will use the first chart as a base to execute Export on
chart["export"].toPDF(layout, function (data) {
this.download(data, "application/pdf", "reporting.pdf");
});
}}
Well I had to add
"export": {
"enabled": true
}
to all my charts!