点击按钮
调出浏览器打印的时候,审查页面元素发现iframe
下面的body元素的margin
有一个浏览器默认值8px
所以,我们可以设置一下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;',
})