Currently, I am receiving the base64 of an image URL. When passing the getImage function to the savepdf function and attempting to store the callback function's base64_data in a variable, an error is thrown:
An error occurs - Cannot set property 'base64logo' of undefined.
How can I successfully pass the value from the callback function to the variable? I hope my question is clear.
data() {
return {
base64logo: '',
}
},
methods: {
getImage(url, callback) {
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function () {
var canvas = document.createElement('CANVAS');
console.log('canvas', canvas)
var ctx = canvas.getContext('2d');
var dataURL;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL();
callback(dataURL);
canvas = null;
};
img.src = url;
},
savepdf() {
this.getImage(this.clientDetails.company_logo, function (base64_data) {
console.log('base64_data', base64_data);
this.base64logo = base64_data; //The Error Occurs Here
});
var doc = new jsPDF('p', 'pt', 'a4');
doc.setFontSize(9);
doc.addImage(this.base64logo, 'PNG', 150, 10, 40, 20); //Should be passed here.
}
}