Node-phantom显示生成的pdf页面数 [英] Node-phantom show number of generated pdf pages

查看:211
本文介绍了Node-phantom显示生成的pdf页面数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在phantomjs中获取生成的pdf页面的数量面临问题.基本上,我将phantomjs与nodejs一起使用,并且我想在第5页中显示总页数.

I am facing problem to get the number of generated pdf pages in phantomjs. Basically I am using phantomjs with nodejs and I want to show total number of pages in page 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

and also add this to 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;

如何显示特定页面内的已生成页面数.

How can I show the number of generated pages inside a specific page.

推荐答案

为什么不在页脚内容中检入您所在的页面,然后显示NumPages.像这样:

Why not checkin in the footer content which page you are, and then display NumPages. Something like this:

contents: "function(pageNum, numPages) { 
              if(pageNum === numPages)
                return '<h6>' + numPages + '</h6>'; 
              else
                return '<h6>' + pageNum + '</h6>'; 
}"

这篇关于Node-phantom显示生成的pdf页面数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆