node-html-pdf not show image in PDF

3k Views Asked by At

I am trying generate PDF from simple code, but no show image. Help me please.

code

pdf.create("Hello <img src='j4.jpg' />").toFile('./pdf/cotizacion.pdf', function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log("File created successfully ");
    }
});

Result: enter image description here

Thanks!

2

There are 2 best solutions below

0
On

Can you something like this, since its not reading your image, its taking it as a string.

    <img src={{IMG_URL}} style="width:100%"/> 

Or you can put base64 of image like:

<img src="data:image/jpeg;base64, {{BASE64_DATA_OF_IMG}}" style="width:100%"/>

This will read your image in pdf.

0
On

After some testing I noticed the images are shown in pdf if they are served somewhere, every attempt with file://path_to_file or relative path failed.

Instead I serve the image on a server http://localhost:3000/images/logo.svg

and I use <img src="http://localhost:3000/images/logo.svg" style="width:100%"/>