我正在使用html-pdf
(NPM page)在我的Node.js应用中生成PDF文件。我这样做的方法是首先使用Handlebars呈现HTML内容,然后将生成的缓冲区提供给html-pdf
的{{1}}方法。像这样:
create()
我正在使用Express,我通过每种类型的路线提供我的资产。所以我将它们加载到像这样的常规HTML
var pdfProperties = {
format: "A4"
}
fs.readFile('views/project-evaluation-print.hbs', {encoding: 'utf8'}, function(err, data) {
if (err) return next(err);
var source = data;
var html = render(source, pageParameters);
htmlPdf.create(html, pdfProperties).toStream(function(err, stream) {
if (err) return next(err);
stream.pipe(res);
});
});
问题是,生成PDF时无法加载。我尝试这样做:
<link rel="stylesheet" href="/stylesheets/home-page.css">
<img src="/images/logos/app-logo.png">
我也尝试将<link rel="stylesheet" href="localhost:8080/stylesheets/home-page.css">
<img src="localhost:8080/images/logos/app-logo.png">
属性添加到base
pdfProperties
但它仍然不起作用。
答案 0 :(得分:0)
找到它。我应该将pdfProperties.base
设置为http://localhost:8080
而不是http://localhost:8080/
(没有尾随斜杠)。
或动态设置为req.protocol + '://' + req.get('host')
。