浏览器打印长table时按页拆分

需求
  1. 浏览器中打印 高度未知的复杂table, 如果不做任何处理,使用 window.print() 打印 则打印中因为不存在断行. 多张纸中不美观. 需要按A4纸高度 拆分table的行进行打印
解决方案
  1. 获取所有的行标签tr元素
  2. 通过循环tr元素 和 getBoundingClientRect 获取 元素与整个table 左上角的距离
  3. 如果距离高度超过了1页纸的高度 则直接在此tr元素前插入一个指定高度的div. 达到分页效果
基础知识
function splitPage(config){
  let totalDom = document.getElementById('printDom'); // 需要打印的table
  const {
    insertDomHeight = '3cm',
    paperHeight = 29,
    printPageIds = [], // table元素id 支持多个table 同时打印. 
  } = config;

  const onePageHeightPixes = Math.ceil(96 * paperHeight / 2.54);   // 一张纸 的高度(像素)
  // https://developer.mozilla.org/zh-CN/docs/Web/CSS/length  1cm = 96px / 2.54
  // A4纸 29.7cm 高

  const { top : totalTop, height : totalHeight  } = totalDom.getBoundingClientRect();
  let insertHeight = onePageHeightPixes;
for (let item of printPageIds) {
    const trList = document.querySelectorAll(`tr[data-tr-id*="${item}"]`);
    if (trList) {
      trList.forEach((x) => {
        const nodePosition = x.getBoundingClientRect();
        const { top, height } = nodePosition;
        const distance = top + height - totalTop; // 目标元素左下角相对于整个表单左上角的距离;
        if (distance > insertHeight) {
          const divHeight = top - insertHeight + height + insertDomHeight;
          console.log(divHeight);
          insertDiv(x, divHeight + 'px');
          insertHeight = insertHeight + onePageHeightPixes;
        }
      });
    }
  }

}


function insertDiv(dom, height = '5cm'){
  if (!dom) {
    return;
  }
  let parent = dom.parentNode;
  let newNode = document.createElement("div");
  newNode.style.height = height;
  parent.insertBefore(newNode, dom);
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值