Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'toDataURL' does not exist on type 'HTMLElement'

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.

barcode.html

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>

barcode.ts

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);
}
like image 618
SrividhyaShama Avatar asked Mar 17 '17 06:03

SrividhyaShama


1 Answers

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.

like image 88
Saravana Avatar answered Sep 19 '22 13:09

Saravana