node.js - NodeJS Pdfmake 如何添加图像

标签 node.js express pdfmake

我在 nodeJS 中使用 api pdfmake 生成 pdf 文件 http://pdfmake.org/#/但我不知道如何将图像添加到文档中,并且总是出现以下错误

error invalid image, images dictionary should contain dataURL entries (or local file paths in node.js) 

这就是我添加图像的方式,它存在于代码的同一文件夹中:

body: [
    [{ rowSpan: 2, image: 'data:aa/png' }, 'Capteur', '', 'image', 'image', 'image'],
    ['', 'Health', '', 'Proximite', 'Lumiere', 'Geroscope'],
]

我需要一些帮助,谢谢。

最佳答案

在您的示例中,您没有指定有效的 base64 编码字符串。 您需要将其更改为正确的 base64 编码字符串(请注意,您可以使用例如 this websitepng 转换为 base64 编码字符串,或者让 Node 像他们一样进行转换 in this issue ):

const pdfDefinition = {
    content: [          
        { rowSpan: 2, image:'data:image/png;base64,<put-your-base64-string-here>'}          
    ]       
}

另请注意,目前仅支持 JPEG 和 PNG 图像。

关于node.js - NodeJS Pdfmake 如何添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62499131/

相关文章:

javascript - Mongoose 使用 Promise 或 Async/Await 的区别?

node.js - Heroku + Cloudflare 完全免费的 SSL

node.js - 如何为基于 Express 构建的 REST API 实现 CAS 身份验证

node.js - 在 express 中全局重定向所有尾部斜杠

node.js - API网关响应使用Node、PDFMake、Serverless下载文件

javascript - "Uncaught TypeError: Cannot read property ' 在pdfMake pdf引擎中使用自定义图像时嵌入' of undefined"

node.js - 如何从 node.js 中的 mysql SELECT 查询返回值

javascript - 为什么我们要在事件发射器的订阅方法声明中声明取消订阅方法?

javascript - Nodejs mongodb 动态集合名称

javascript - PDFMAKE:如何重复 'content' 中的数组 [ ] 项