I am trying to generate a PDF file out of the banana dashboard.
I have made the following changes to the files:
<div ng-view id="myDiv"></div>
vendor
folder of banana applicationhtml2canvas.js
pdfmake.js
Updated the require.config.js
file to point to these two new js files as below:
html2canvas: ../vendor/html2canvas,
pdfmake: ../vendor/pdfmake
Updated the dashLoader.html
file to include another item in the showdown list as 'Export to PDF
'
<li ng-show="dashboard.current.loader.save_local">
<a href="" alt="Export to File" title="Export to PDF" class="link" ng-click="dashboard.to_pdf()">
<i class="icon-download"></i> Export to PDF</a>
<tip>Export layout and data to PDF file</tip>
</li>
Finally updated the dashboard.js
file as below:
this.to_pdf = function () {
var inclusions = document.getElementById('myDiv');
console.log(inclusions);
html2canvas(inclusions).then(function(canvas) {//this line is throwing error as html2canvas is not defined
inclusions.appendChild(canvas);
data_1 = canvas.toDataURL();
resolve(data_1);
console.log(inclusions);
});
return true;
};
But when I click on the Export to PDF
option I get the error “Error: html2canvas is not defined”
. Please refer the screenshot attached.
Any help on where I am going wrong would be very thankful!
A standalone Hello World html2canvas html program helped to fix this. The correct way to invoke html2canvas and then pdfmake (or any other pdf generation library) is as below:
html2canvas(document.getElementById('divId')).then(
canvas =>{
var data =canvas.toDataURL();
var docDefiniton ={
content:[{
image:data,
width:500
}]
};
pdfMake.createPdf(docDefinition).open();
}
);