【发布时间】:2014-06-24 08:11:47
【问题描述】:
我在获取 phantomjs 中生成的 pdf 页面数量时遇到问题。基本上我使用 phantomjs 和 nodejs,我想在第 5 页显示总页数。
function generatePdf() {
// Load ejs template
fs.readFile(__dirname + '/../pdf' + pdfpath, 'utf8', function (err, data) {
var fileName = __dirname + '/pdfdata/' + f.formType + f.formId + '.pdf';
// Render the page to variable.
var html = ejs.render(data, pdfJSON);
fs.writeFile(__dirname + '/pdfdata/test.html', html, function (err) {
if (err) throw err;
});
// Set this html as the content for the pdf file.
page.set('content', html);
//page.set( 'paperSize', { width: 1200, height: 1500} );
page.set('paperSize', {
//format: 'A4',
width: 1200,
height: 1600,
header: {
height: "1cm",
contents: "function(pageNum, numPages) { return '<h6 style=text-align:right></h6>'; }"
},
footer: {
height: "1cm",
contents: "function(pageNum, numPages) { return '<h6 style=text-align:right;margin-right:60px;postion:absolute;margin-bottom:-20px>' + pageNum + '</h6>'; }"
}
}, function () {
page.open(__dirname + '/pdfdata/test.html', function () {
page.render(fileName, cb);
ph.exit();
// Response to client.
function cb() {
res.jsonp(200, responseJSON);
}
})
});
});
} // end of function
为了显示我添加了generatePdf function的页面
contents: "function(pageNum, numPages) { return '<h6 style=text-align:right;margin-right:60px;postion:absolute;margin-bottom:-20px>' + pageNum + '</h6>'; }"
并将其添加到 bridge.js
case 'pageSet':
eval('request[4].header.contents = phantom.callback(' + request[4].header.contents + ')');
eval('request[4].footer.contents = phantom.callback(' + request[4].footer.contents + ')');
page[request[3]] = request[4];
respond([id, cmdId, 'pageSetDone']);
break;
如何在特定页面中显示生成的页面数。
【问题讨论】:
-
你能澄清一下你使用的是什么版本的 phantomjs 以及错误是什么?它适用于 phantomjs 1.9.7
-
这不是一个解决方案,而是一个糟糕的解决方法:在您的 html 中强制打印分页符并将页码放在那里。 CSS
.pagebreak { page-break-before: always; }并仅在打印中显示页码,.print-only{ display:none } @media print{ .print-only{display:block} }
标签: node.js pdf-generation phantomjs