生成pdf文件phantomjs,重复HEADER [英] Generate pdf files phantomjs, repeating HEADER

查看:101
本文介绍了生成pdf文件phantomjs,重复HEADER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用phantomjs生成pdf文件,但是我想使用HTML重复定义的标头,当没有图像时它可以工作,但是一旦添加它就行不通了

I'm generating pdf files using phantomjs but I would like to repeat a defined header with HTML, it works when there are no images but as soon I add it doesn't work

page.viewportSize = { width: 600, height: 600 };
page.paperSize = {
  format: 'A4', orientation: 'portrait', margin: '0px',
  header: {
    height: "1.2cm",
    contents: phantom.callback(function(pageNum, numPages) {
      return '<img src="https://www.google.com.bo/images/srpr/logo4w.png" height="0.95cm"/>';
    })
  },
  footer: {
    height: "0.7cm",
    contents: phantom.callback(function(pageNum, numPages) {
      return '<h3 class="header">Footer</h>';
    })
  }
}

推荐答案

很复杂的方法是,通过将img放在正文中并显示以下内容来在页面上预先缓存图像:none;.

Hacky way around this, pre-cache the image on your page by putting the img in body with a display: none;.

<img src="https://www.google.com.bo/images/srpr/logo4w.png" height="0.95cm" style="display: none;"/>

然后在页眉和/或页脚中也包含img标签.

Then have the img tag in your header and/or footer as well.

这是必需的,因为页眉和页脚在继续光栅化之前不等待html完成加载.需要进行改进以提高异步性,这是一个已知的错误.

This is required because the header and footer do not wait for the html to be finished loading before continuing to raster. It needs to be improved to be more Async and is a known bug.

由于在页面完全加载后处理了页眉和页脚,因此图像也可供他们使用.这也适用于base64图像源.

Since header and footer are processed after the page has fully loaded the image will be available for them too use. This also works with base64 image source.

这篇关于生成pdf文件phantomjs,重复HEADER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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