【问题标题】:Phantomjs node set header,footerPhantomjs 节点设置页眉、页脚
【发布时间】:2014-05-10 04:57:57
【问题描述】:

如何使用带有节点的 phantomjs 设置页面页眉和页脚,基本上我是从 html 生成 pdf 并且我愿意从节点添加页眉和页脚,我尝试了以下操作,但 pdf 没有显示任何数据,我正在阅读空白页面并愿意添加页眉和页脚,这是我的代码:

// Load ejs template
fs.readFile(__dirname + '/../pdf' + pdfpath, 'utf8', function (err, data) {

  // Render the page to variable.
  var html = ejs.render(data, pdfJSON);

  // Set this html as the content for the pdf file.

  page.set('content', html);

  page.set('generatePDF', function (pageNum, numPages) {

    if (pageNum == 1) {
      return "";
    }
    return "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";
  });

  page.set('paperSize', {
    width: 1200,
    height: 1500,
    header: {
      height: "1cm",
      contents: phantom.generatePDF
    }
  });

  console.log(phantom.generatePDF);//return undifned

  page.set('paperSize', {
    width: 1200,
    height: 1500
  });

  // Generate the pdf.
  var fileName = __dirname + '/pdfdata/' + f.formType + f.formId + '.pdf';
  page.render(fileName, cb);

});

我该如何解决这个问题?

【问题讨论】:

    标签: node.js express phantomjs ejs


    【解决方案1】:

    看看这些例子:

    https://github.com/ariya/phantomjs/blob/master/examples/printheaderfooter.js

    编辑:抱歉,没有注意到您要求使用 Node,您也可以在这里找到一些解决方案和答案:

    Footer's contents don't seem to work

    【讨论】:

    • 不鼓励仅链接的答案,因为它们经常中断。我建议您编辑您的答案以自给自足。
    • 其他人来这里。 2021 年第一个链接使用 phantomjs 不起作用
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2016-04-14
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多