ven*_*ats 12 pdf-generation node.js npm
我使用这个转换的HTML到PDF.The转换是真的好.不过问题是添加页眉和页脚的PDF pages.In的选项,如果我添加标题文本我得到的结果是什么我的预期.
//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();
}
Run Code Online (Sandbox Code Playgroud)
然后,如果我在标题(内容)选项中添加图像标记,我没有在生成的PDF中获取图像.能否请你帮我解决这个问题.
Vij*_*jay 10
可以在选项标题中添加图像.1.使用"display:none"样式在html正文中加载图像.2.然后在选项标题中添加图像通过执行此操作,图像被缓存并可以将图像附加到标题中.
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();
}
});
Run Code Online (Sandbox Code Playgroud)
在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);
}
});
Run Code Online (Sandbox Code Playgroud)
有了这段代码,我得到了这个pdf:

| 归档时间: |
|
| 查看次数: |
6548 次 |
| 最近记录: |