【print-js】多页打印时,第一页多出一张空白页的问题

462 阅读1分钟

点击按钮调出浏览器打印的时候,审查页面元素发现iframe下面的body元素的margin有一个浏览器默认值8px image.png

所以,我们可以设置一下body的样式就可以了,样式如下:

import { toPng } from 'html-to-image'

const dataUrl = toPng(document.querySelector('#report'))

// 方式一: 出现第一页空白,其实只需要设置 margin-top:0 即可
const style = 'body {margin-top: 0; background-color: #fff}'

// 方式二: 设置html的 height:0 即可
// NOTE: 这里书写样式的时候,与style标签里面书写方式一样,选择器之间不需要逗号或者分号分割,否则样式将会失效
// const style = 'html {height: 0;} @page {size: A4;}' 

printJS({
  printable: dataUrl,
  type: 'image',
  documentTitle: 'Document',
  targetStyles: ['*'],
  style,
  imageStyle: 'width:100%;margin-top:0;margin-bottom:0;',
})