使用html-pdf节点模块在标题中添加图像

我使用这个将html转换为PDF。转换是非常好的。但问题是在PDF页面中添加页眉和页脚。在选项中,如果我添加标题文本,我得到了我期望的结果。

//Options var options = { "header": { "height": "45mm", "contents": "<div style='text-align: center;'>Author: Marc Bachmann</div>" // If i add image in content it wont work // sample i tried }, "footer": { "height": "28mm", "contents": "<span style='color: #444;'>{{page}}</span>/<span>{{pages}}</span>" } } // Tried this in contents <img src="image path" /> var result = <div class="container"> HTML CONTENT</div>'; pdf.create(result, options).toFile(fileName + ".pdf", function(err, res) { if (err) { console.error(err); callback(); } 

然后,如果我添加标题(内容)选项中的图像标记,我没有得到生成的PDF中的图像。 你能给我一个解决scheme,谢谢。

可以在选项标题中添加图像。 1.用“display:none”风格在html body中加载图片。 2.然后将图像添加到选项标题中通过这样做,图像被caching,并可以将图像附加到标题中。

  var options = { "format": 'Letter', "orientation": "portrait", "header": { "contents": "<img src='image path' />", "height": "30mm" }, "footer": { "contents": footer } } pdf.create("<div style='display:none'><img src='image path' /></div>", options).toFile("sample.pdf", function(err, res) { if (err) { console.error(err); callback(); } }); 

在github上引用这个问题 ,你不能把你的图片直接放在options.header ,你必须把它放在<div id="pageHeader"></div>

 var pdf = require('html-pdf'); var path = require('path'); // this is very important, you have to put file:// before your path // and normalize the resulting path var imgSrc = 'file://' + __dirname + '/350x120.png'; imgSrc = path.normalize(imgSrc); // or var imgSrc = path.join('file://', __dirname, '/350x120.png'); // Options var options = { "header": { "height": "45mm", "contents": "" }, "footer": { "height": "28mm", "contents": "<span style='color: #444;'>{{page}}</span>/<span>{{pages}}</span>" } } // put your entire header here and the content of the page outside the <div id="pageHeader"></div> var result = "<div id='pageHeader'><img src='" + imgSrc + "' /><div style='text-align: center;'>Author: Marc Bachmann</div></div>"; result += "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>"; var fileName = __dirname + '/test.pdf'; pdf.create(result, options).toFile(fileName, function(err, res) { if (err) { console.error(err); } }); 

有了这个代码,我得到这个pdf:

生成的PDF