【问题标题】:Adding a header to a PDF generated by Node Webshot向 Node Webshot 生成的 PDF 添加标题
【发布时间】:2015-01-13 17:04:50
【问题描述】:

我正在尝试将标题添加到使用node-webshot 生成的 PDF 中,但没有显示任何内容。

我正在使用此代码:

webshot('https://www.google.com', 'google.pdf', options, function (err) {
    if(err) throw err;
    console.log('Saved to PDF');
});

使用这样的选项对象:

var options = {
        "paperSize": {
            "format": "A4", 
            "orientation": "portrait", 
            "border": "0.25cm",
            "header": {
                "height": "2cm",
                "contents": function () {
                    return phantom.callback(function(pageNum, numPages) {
                        return '<h1>' + pageNum + ' / ' + numPages + '</h1>';
                    });
                }
            }
        }
    };

我尝试在 PhantomJS documentation 上显示内容功能,但它不起作用,因为未定义幻像。我找不到任何关于如何使用 webshot 添加页眉/页脚的示例。

PDF 生成正确,但标题是空白的,上面没有写任何内容。

我该如何解决这个问题?

【问题讨论】:

  • 我的回答有帮助吗?有什么问题吗?如果它解决了你的问题,你可以accept 一个答案(只是让你知道)。

标签: javascript node.js pdf phantomjs


【解决方案1】:

由于选项作为对象传递给webshot,并且“回调”是直接的,所以没有办法修改webshot。

因此将行从here(文件node-webshot/lib/webshot.phantom.js)更改为:

optUtils.phantomPage.forEach(function(key) {
  if (toOverwrite[key]) page[key] = toOverwrite[key];
  if (key === "paperSize" && page[key] && page[key].header && page[key].header.contents) {
    page[key].header.contents = eval(page[key].header.contents);
  }
  if (key === "paperSize" && page[key] && page[key].footer && page[key].footer.contents) {
    page[key].footer.contents = eval(page[key].footer.contents);
  }
});

假设您将标题内容函数组成为这样的字符串:

"contents": "phantom.callback(function(pageNum, numPages) { return '<h1>' + pageNum + ' / ' + numPages + '</h1>'; })";

它不能作为函数传递,因为在此过程中选项对象被字符串化,这会删除所有函数。

【讨论】:

    【解决方案2】:

    我知道这个问题已有一年多的历史了,但对于其他有上述相同问题的人,eval() 对我不起作用。我最终做的是在同一位置添加以下代码:

    if (key === "paperSize" && page[key] && page[key].header && page[key].header.contents) {
        var header = {
          "height": page[key].header.height,
          "contents": phantom.callback(function() {return options.paperSize.header.contents;})
        }
        page.paperSize.header = header;
      }
      if (key === "paperSize" && page[key] && page[key].footer && page[key].footer.contents) {
        var footer = {
          "height": page[key].footer.height,
          "contents": phantom.callback(function() {return options.paperSize.footer.contents;})
        }
        page[key].footer = footer
      }
      if (key === "paperSize") {
        page.paperSize = {
          "format": options.paperSize.format,
          "orientation": options.paperSize.orientation,
          "margin": options.paperSize.margin,
          "header": header,
          "footer": footer
        }
      }
    

    原因是 webshot 对选项对象中的所有数据进行了字符串化,因此使用 phantomjs 呈现页眉和页脚所需的 phantom.callback 无法正确字符串化。相反,您只需要在选项对象的页眉/页脚中添加要使用的 html,然后将其插入到上面代码中的 phantom.callback 中。这种方法的唯一问题是您不能在回调中使用 numPages 和 pageNum 参数。

    【讨论】:

      猜你喜欢
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      相关资源
      最近更新 更多