angularjs - pdfmake图像错误插入

标签 angularjs typescript export-to-pdf pdfmake

我需要将图像插入 PDF,但插入时出现错误,告诉我格式不正确。我的代码如下。

public getBase64Image(img : any) {
        // Create an empty canvas element
        var canvas = document.createElement("canvas");
        canvas.width = img.width;
        canvas.height = img.height;

        // Copy the image contents to the canvas
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0);

        // Get the data-URL formatted image
        // Firefox supports PNG and JPEG. You could check img.src to
        // guess the original format, but be aware the using "image/jpg"
        // will re-encode the image.
        var dataURL = canvas.toDataURL("image/png");

        return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
    }

转换base64的函数。 我称之为函数。

exporterPdfHeader:{
                columns: [
                    {  
                        width: 300, alignment: 'left',
                        text: [ 
                                'Pedido de prueba\n\n',
                                'Centro de Trabajo: prueba. Serie: 00.\n\n',
                                'Numped: 38289',
                              ],
                        bold:true,
                        fontSize: 10,
                        margin: [40, 20, 0, 0],
                        height:120,
                    },
                    {
                        width: 140,
                        alignment: 'center',
                        image: 'data:image/png;base64, this.getBase64Image("../imagenes/logoExportarPdf.png")',
                        bold:true, 
                        fontSize: 10,
                        margin: [40, 20, 0, 0],
                        height:80,
                    }
                ]
            }, 

错误

enter image description here

对此,我有几个问题。您需要将其传递给 base64 吗? 我只想将照片插入PDF,这很简单。作为一个细节,不知道它是否重要,我使用 Angle ui -grid 。我用 Typescript 编程。

谢谢

最佳答案

您实际上是将字符串 'data:image/png;base64, this.getBase64Image("../imagenes/logoExportarPdf.png")' 作为图像传递,而不是调用该函数。

您应该使用'data:image/png;base64,'+ this.getBase64Image("../imagenes/logoExportarPdf.png")

关于angularjs - pdfmake图像错误插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35572061/

相关文章:

AngularJS 和 $document.height();?

javascript - AngularJS 从资源查询中访问数据

jasper-reports - 使用 JasperReports 在生成的 PDF 中缩进

fonts - 如何更改报告中的默认字体大小

jsPDF分页符

css - 在选择或页面更改时使用 UI Bootstrap 自动关闭导航栏

angularjs - 构建 angularJS 音频播放器的最佳方法是什么?

reactjs - typescript 文件 "is not a module"

angular - 有没有更好的方法在 Angular2 中获取祖先路由参数?

reactjs - 具有继承的对象分配在 typescript 中不起作用