我在获取 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;
如何显示特定页面内生成的页面数。