You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 8, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Jul 8, 2026. It is now read-only.
image is not displayed after generating the pdf ? #44
Currently not in the header using the options.header variable.
You have to use <div id="pageHeader"></div>
But in the body it should work. There are some open issues. Please read through them if you don't figure it out by yourself. Please report back if you find your solution. I'll need to update the documentation with a bit more informations.
I am also having the same problem. I am not using the options.header variable. Its just a simple image in the body which is loaded locally. The generated pdf just shows an empty white box.
This is the html that I am trying to print. I have verified that the image exists and has the proper permissions.
I am also having the same problem. I am not using the options.header
variable. Its just a simple image in the body which is loaded locally. The
generated pdf just shows an empty white box.
This is the html that I am trying to print. I have verified that the image
exists and has the proper permissions.
Talk to us:
feedback@atchayam.in
Thank you! enjoy your meal!
—
Reply to this email directly or view it on GitHub #44 (comment)
.
Reacted by Sekharan Natarajan, Surendra Babu, Luat Vu, Ailson da Cruz Goncalves Tavares, Sharmishtha C, Kevin Christopher Chacón Campos, Jamie Corkhill, Maurice, José Luis Valencia Herrera, Avani Khabiya and 9 more
Reacted by Fazil Loladiya, SunandaPadawale, Ayush Verma and Faizah Aulia R
Reacted by Avani Khabiya
Reacted by José Luis Valencia Herrera, pabichw and Victor Tesoura Júnior
I am guessing you are on a linux system. So the part of 'file://' is just the prefix. If your file is in /imagen.jpg (which it should not btw), then your path should be 'file:///imagen.jpg' (Note the extra /).
@agnivade thanks for the answer.
The important thing is to link to the file using an absolute path. It can't be relative to your application. So it should look like file:///home/app/files/image.png
I was missing the extra " / " but now its working fine.
Another thing in <img src="file:///C:/home/app/files/image.jpg"/> the slashes need to be inverted like this: <img src="file:\\\C:\\home\\app\\files\\image.jpg" />
Reacted by Krishan Kulasinghe, Gujie-Novade, Qws, Mitchell Contreras and dihan48
Works for me
1.Get the path of the folder where images reside using "path.join"
2.Change the '\' backward slashes into forward slashes '/'.
3.Now use the base:"file:///"+path
provide src of the images just the public folder contains,
as: public/images/logo.png should be img src="images/logo.png"
Reacted by muthukumarHelios, Edvards, Aurélie, Nadeem Ramsing, Mazen — sa/acc, Serhii Sakal, pincobb, Candra Darmawan, Slimani Mohammed, Oanh and 5 more
Reacted by Chawki
Reacted by Slimani Mohammed and with-key
Reacted by Candra Darmawan, Slimani Mohammed, jankes44 and with-key
Reacted by muthukumarHelios, Mazen — sa/acc, pincobb, Candra Darmawan, Slimani Mohammed, Ezenwankwo Gabriel, jankes44, with-key and Abdelrahman Hussein
Reacted by Slimani Mohammed, Jonathan Cramer and with-key
I needed a solution which would make a relative path work for the image as my html is getting generated in Angular and the pdf in Node. But, the image source would not get appended to the base url from the "options" object inspite of trying multiple solutions provided above. Finally looked up on the example provided in the html-pdf npm package and replaced the "src" attribute from the html with the path as below.
let html1 = html.replace('branding','file:\\\\\\' + require.resolve('../../assets/images/branding.png').replace(/\//g,"\\\\"));
In the html, give the image name reference which is used for replacement.
I'm using a Mac, and what worked for me was: <imgsrc="file:///Users/melanierogoff/Desktop/nodegeneratorHW/assets/images/blog.png"> . What I needed to do was the file:// as mentioned previously, but when I copied my absolute filepath, it didn't work, until I added the extra '/' before Users. Hope that helps!
i had a template file index.js where i expoted pdfTemplate in this file i passed the imgSrc as a parameter to the exported function. Note if you wanted or need to add the image to the header or footer, the config did not work well with for me so i created my header and added the pageHeader to the top element also not to set your header height in your config file as directed above.
First it was working fine in devlopment mode as I was using the full http URL (http://localhost:3000/img/logo.png). Not only on local system, but on server too.
But as soon as we configured web-server proxy and renamed the full URL to https://www.example.com/img/logo.png, it stopped working. Basically won't resolve images from http URL.
So had to setup base option as mentioned in many answers here. But it only worked after using full directory path to images.
For example, on linux server we have application in default apache directory and base: file:///var/www/html/front-app/img/
worked as expected
Linux user here. If you're on a different machine, use appropriate path to your asset
If anyone still has an issue with the image being loaded, use the following config
constpdfOptions={format: "A4",// allowed units: A3, A4, A5, Legal, Letter, Tabloidorientation: "portrait",// portrait or landscape// Rendering options// base: assetPath, // Base path not neddedlocalUrlAccess: true,// set this to true to enable local file access};returnnewPromise((resolve,reject)=>{htmlPdf.create(html,pdfOptions).toBuffer((err,buffer)=>{if(err){reject(err);}else{resolve(buffer);}});});
Reacted by Phil Blenkinsop, Jeff Lam, Gaurav bhojwani, dihan48, zbarbuto, Baptiste, Charith Jayawardana, Tanapon Kongjaroensuk, Manon Guillard, Bruno and 3 more
Reacted by hypo-thesis
Reacted by Mathiaco-Lee Bessane, Tanapon Kongjaroensuk, Daniel Andújar, karein, Josué Catarino Ruiz, Bruno and João Victor Cesa Andrade
Linux user here. If you're on a different machine, use appropriate path to your asset
If anyone still has an issue with the image being loaded, use the following config
constpdfOptions={format: "A4",// allowed units: A3, A4, A5, Legal, Letter, Tabloidorientation: "portrait",// portrait or landscape// Rendering options// base: assetPath, // Base path not neddedlocalUrlAccess: true,// set this to true to enable local file access};returnnewPromise((resolve,reject)=>{htmlPdf.create(html,pdfOptions).toBuffer((err,buffer)=>{if(err){reject(err);}else{resolve(buffer);}});});
If anyone is still having problems, I recommend just displaying the image with a base64 using <img src="data:image/png;base64,IMAGE_BASE_64" /> because I tried every single answer here and none worked.
Using base64 is not what I want and not the best way, but worked.
but i still havind not showing images in final pdf.
If i try to show the original html on the browser before to be converted in pdf, i't showing fine ...
I'm really confused, there is another way to make it working on?
Linux user here. If you're on a different machine, use appropriate path to your asset
If anyone still has an issue with the image being loaded, use the following config
constpdfOptions={format: "A4",// allowed units: A3, A4, A5, Legal, Letter, Tabloidorientation: "portrait",// portrait or landscape// Rendering options// base: assetPath, // Base path not neddedlocalUrlAccess: true,// set this to true to enable local file access};returnnewPromise((resolve,reject)=>{htmlPdf.create(html,pdfOptions).toBuffer((err,buffer)=>{if(err){reject(err);}else{resolve(buffer);}});});
Thanks @atrvicky, this worked for me, I only needed to add the option localUrlAccess: true, and for me worked both file:///path and file:/path. I'm using linux (ubuntu).
Failed to insert an image in the header using host for an image. Works fine if I use absolute path for the image. However, I need to read it from host.
I have the same problem, could you resolve it? The image is displayed when it's not in header, but not if it's in.
<!-- This is displayed --><imgsrc="{{RESOURCES_BASE_URL}}img/header.png" alt="Header" width="100%" /></div><!-- This is not displayed --><divid="pageHeader"><imgsrc="{{RESOURCES_BASE_URL}}img/header.png" alt="Header" /></div>
My resources url are localhost or a server, I need it working for both.
Tried literally every solution in here and it would still not work on Windows.
If anyone's still stuck as I was, you could convert the image to base64 and render the base64 instead.
const background = fs.readFileSync(path.join(__dirname, 'image.jpg')); // use whatever you need here
const backgroundBase64 = background.toString('base64'); // converting it to base64
<img src="data:image/jpeg;base64,${backgroundBase64}" alt='img' /> // using it as base64
Activity
marcbachmann commentedon Jul 1, 2015
Currently not in the header using the
options.headervariable.You have to use
<div id="pageHeader"></div>But in the body it should work. There are some open issues. Please read through them if you don't figure it out by yourself. Please report back if you find your solution. I'll need to update the documentation with a bit more informations.
bgobi1991 commentedon Jul 1, 2015
Sorry , i didn't understood your statement , i have attached my code below, can you give some solution ?
var html = '
var options = { filename: './businesscard.pdf', format: 'Letter', width: '210mm', height: '297mm', border: '10mm', timeout: 120000 };
pdf.create(html, options, function(err, buffer) {
if (err) return console.log(err);
console.log("Converted successfully.");
});
agnivade commentedon Jul 7, 2015
Hi,
I am also having the same problem. I am not using the options.header variable. Its just a simple image in the body which is loaded locally. The generated pdf just shows an empty white box.
This is the html that I am trying to print. I have verified that the image exists and has the proper permissions.
marcbachmann commentedon Jul 7, 2015
You have to use the file:// protocol for local files. I believe
file:///home/agniva/email.pngworks.Sent from my iPhone
On Jul 7, 2015, at 10:45 PM, Agniva De Sarker notifications@github.com
wrote:
Hi,
I am also having the same problem. I am not using the options.header
variable. Its just a simple image in the body which is loaded locally. The
generated pdf just shows an empty white box.
This is the html that I am trying to print. I have verified that the image
exists and has the proper permissions.
—
Reply to this email directly or view it on GitHub
#44 (comment)
.
agnivade commentedon Jul 8, 2015
Works ! Thanks.
renancarrillo commentedon Oct 22, 2015
I dont know how to put this protocol in action, my code is this:
So when I make the pdf the image didn't show
agnivade commentedon Oct 23, 2015
I am guessing you are on a linux system. So the part of 'file://' is just the prefix. If your file is in /imagen.jpg (which it should not btw), then your path should be 'file:///imagen.jpg' (Note the extra /).
marcbachmann commentedon Oct 23, 2015
@agnivade thanks for the answer.
The important thing is to link to the file using an absolute path. It can't be relative to your application. So it should look like
file:///home/app/files/image.pngrenancarrillo commentedon Oct 23, 2015
Thank you @agnivade and @marcbachmann for your answers.
I was missing the extra " / " but now its working fine.
Another thing in
<img src="file:///C:/home/app/files/image.jpg"/>the slashes need to be inverted like this:<img src="file:\\\C:\\home\\app\\files\\image.jpg" />DiegOrtega commentedon Mar 7, 2017
I'm in a mac and I still can't create the pdf with the images, the images are broken.
My code is:
<divclass="col-lg-4" align="center"><imgsrc="file:\\Users\DiegOrtega\Desktop\cotizador\cotizador-3m\public\img\logoleo.png"class="img-responsivecenter-block" alt="Artículos Innovadores Leo" title="Artículos InnovadoresLeo"></div>Hardik21 commentedon Apr 27, 2017
Still not working for me.
My Code:
HTML:
<img src="logo.png" alt="Logo" />dukakent commentedon Jun 26, 2017
@DiegOrtega works for me on mac:
pawansaket commentedon Aug 3, 2017
Works for me

1.Get the path of the folder where images reside using "path.join"
2.Change the '\' backward slashes into forward slashes '/'.
3.Now use the base:"file:///"+path
provide src of the images just the public folder contains,
as: public/images/logo.png should be img src="images/logo.png"
fh4uweb commentedon Aug 17, 2017
I had the same problem. Then i try this:
$mpdf = new mPDF();$html= "img src='http://google.de/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png' alt=''>";$mpdf->WriteHTML($html);$mpdf->debug = true;$output = $mpdf->Output();die(__FILE__.':'.__LINE__);WTF... This works! Why???
The crucial line was:
$mpdf->debug = true;.I put that before my
$mpdf->Output();and the pictures were back.18 remaining items
harshikaS16 commentedon Jan 11, 2020
I needed a solution which would make a relative path work for the image as my html is getting generated in Angular and the pdf in Node. But, the image source would not get appended to the base url from the "options" object inspite of trying multiple solutions provided above. Finally looked up on the example provided in the html-pdf npm package and replaced the "src" attribute from the html with the path as below.
let html1 = html.replace('branding','file:\\\\\\' + require.resolve('../../assets/images/branding.png').replace(/\//g,"\\\\"));In the html, give the image name reference which is used for replacement.
<img style="height: 18px;" src="branding">MelanieRogoff commentedon Jan 28, 2020
I'm using a Mac, and what worked for me was:
<imgsrc="file:///Users/melanierogoff/Desktop/nodegeneratorHW/assets/images/blog.png"> . What I needed to do was the file:// as mentioned previously, but when I copied my absolute filepath, it didn't work, until I added the extra '/' before Users. Hope that helps!kausett commentedon Mar 24, 2020
After couple of days of trying i was able to come up with some. i was able to come up with something that works.
in my congig file i had
i had a template file index.js where i expoted pdfTemplate in this file i passed the imgSrc as a parameter to the exported function. Note if you wanted or need to add the image to the header or footer, the config did not work well with for me so i created my header and added the
pageHeaderto the top element also not to set your header height in your config file as directed above.in the file where i am creating the pdf i have below
this solution works for me i hope someone can make use of it also 😎
rampanwar commentedon Aug 6, 2020
Many people still finding this old issue.
First it was working fine in devlopment mode as I was using the full http URL (http://localhost:3000/img/logo.png). Not only on local system, but on server too.
But as soon as we configured web-server proxy and renamed the full URL to https://www.example.com/img/logo.png, it stopped working. Basically won't resolve images from http URL.
So had to setup
baseoption as mentioned in many answers here. But it only worked after using full directory path to images.For example, on linux server we have application in default apache directory and
base: file:///var/www/html/front-app/img/worked as expected
atrvicky commentedon Jun 30, 2021
Linux user here. If you're on a different machine, use appropriate path to your asset
If anyone still has an issue with the image being loaded, use the following config
In the html code, use this
jefflam commentedon Aug 16, 2021
This worked for me! I'm on a Mac. Thanks!
EduVencovsky commentedon Aug 24, 2021
If anyone is still having problems, I recommend just displaying the image with a base64 using
<img src="data:image/png;base64,IMAGE_BASE_64" />because I tried every single answer here and none worked.Using base64 is not what I want and not the best way, but worked.
inputitalia commentedon Nov 2, 2021
hi i've the same issue on my project.
last 'html-pdf' version
last phantomjs version
I've tryed all the options
base: 'file://' + relativePathTemplate
base: 'file://' + absolutePathTemplate
base: 'file:///' + relativePathTemplate
base: 'file:///' + absolutePathTemplate
<img src="file:///{relative path}/image.png"><img src="file:///{absolute path}/image.png"><img src="file:\\\{relative path}\image.png"><img src="file:\\\{absolute path}\image.png"><img src="file://{relative path}/image.png"><img src="file://{absolute path}/image.png">but i still havind not showing images in final pdf.
If i try to show the original html on the browser before to be converted in pdf, i't showing fine ...
I'm really confused, there is another way to make it working on?
praveen-sk commentedon Dec 8, 2021
Add a png file as baground image is not working
neuberoliveira commentedon Feb 25, 2022
my contribution here for someone else stucked with
baseoption, its required to have a trailing slash otherwise files wont loadwork
file:///path/to/my/assets/not work
file:///path/to/my/assetsUpdate README.md
josueveloper commentedon Jun 22, 2022
Thanks @atrvicky, this worked for me, I only needed to add the option
localUrlAccess: true, and for me worked bothfile:///pathandfile:/path. I'm using linux (ubuntu).This is my JS code:
My html:
stariluz commentedon Apr 5, 2024
I have the same problem, could you resolve it? The image is displayed when it's not in header, but not if it's in.
My resources url are localhost or a server, I need it working for both.
LuizBarros-Firyn commentedon Apr 17, 2024
Tried literally every solution in here and it would still not work on Windows.
If anyone's still stuck as I was, you could convert the image to base64 and render the base64 instead.