TypeError while generating pdf with pdfmake

3.1k Views Asked by At

Here is my code:

<script type="text/javascript">
var url = 'localhost:8080/chartGenerator';
 function myFunction() {
 var docDef={ content: [
    'This is an sample PDF printed with pdfMake',
    {
        image: getBinaryResource(url)
    }
    ]


}
 pdfMake.createPdf(docDef).download('optionalName.pdf');
}

function getBinaryResource(url){
     var req = new XMLHttpRequest();
    req.open("GET", url, false);
    req.overrideMimeType('text/plain; charset=x-user-defined');

    req.send(null);
    if (req.status == 200) {
        return req.responseText.replace(/^data:image\/(png|jpg);base64,/, "");
    } else return null

}

</script>

I need to generate a pdf document with an image that I get from the server, but I have the following error :TypeError: r is undefined.

Could you help me to resolve this issue.

1

There are 1 best solutions below

0
On BEST ANSWER

As @AndréKool pointed out removing ".replace(/^data:image/(png|jpg);base64,/, "");" solved it.

This part is needed so pdfmake can recognise it as a base64 image format.