Hi I am new to TypeScript. I am trying to generate bar code in canvas using JSBarcode and add it to JSpdf as image using addImage
. But I am getting the above error.
This is my html code. By click the generate button it has created the barcode. But when I convert my html to PDF it gives the above error.
<canvas id="barcode"></canvas>
<a id="download" download="barcode.png" (click)='Generate();'>Generate</a>
<button (click)='Generatepdf();'>PDF</button>
private Generate(): void {
JsBarcode("#barcode", "12345", {
width: 2,
height: 25
});
}
Generatepdf()
{
var pdf = new jsPDF('p', 'pt', 'letter');
let canvas =document.getElementById('barcode');
console.log(canvas);
let dataURL = canvas.toDataURL("image/jpeg");
pdf.addImage(dataURL, 'JPEG', 15, 40, 180, 160);
function (dispose) {
pdf.output('datauri');
}, margins);
}
Cast it to HTMLCanvasElement
:
let canvas = document.getElementById('barcode') as HTMLCanvasElement;
If you are using TypeScript version < 1.6:
let canvas = <HTMLCanvasElement> document.getElementById('barcode');
Now you will have access to toDataURL
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With