使用 pdfmake 生成 pdf 时出现类型错误

TypeError while generating pdf with pdfmake

本文关键字:类型 错误 pdfmake 生成 pdf 使用      更新时间:2023-09-26

这是我的代码:

<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>

我需要生成一个带有从服务器获得的图像的pdf文档,但是我有以下错误:TypeError:r未定义。

你能帮我解决这个问题吗?

正如

@AndréKool指出的那样,删除".replace(/^data:image/(png|jpg);base64,/,");"解决了这个问题。

这部分是必需的,以便pdfmake可以将其识别为base64图像格式。