Mohamed Borhene Hamedi
Mohamed Borhene Hamedi

Reputation: 139

NodeJS Pdfmake how to add an image

I'm using the api pdfmake in nodeJS to generate pdf files http://pdfmake.org/#/ but i do not not know how to add image to the document and always i get below error

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

This is how i add the image and it exist in the same folder of the code:

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

I need some helps and thank you.

Upvotes: 3

Views: 5474

Answers (1)

eol
eol

Reputation: 24565

In your example you're not specifing a valid base64-encoded string. You need to change it to a correct base64-encoded string (note that you can use e.g. this website to convert your png to a base64-encoded string or you let node do the conversion as they did in this issue):

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

Also note that only JPEG and PNG images are supported currently.

Upvotes: 5

Related Questions