目录 使用print-js 打印渲染不出来 vue中printjs使用指南 使用攻略 参数 使用print-js 打印渲染不出来 vue 使用print-js 打印渲染不出来, 实际数据已经加载出来了。 遇到这问题,解决方向 1、
          目录
- 使用print-js 打印渲染不出来
- vue中printjs使用指南
- 使用攻略
- 参数
使用print-js 打印渲染不出来
vue 使用print-js 打印渲染不出来, 实际数据已经加载出来了。
遇到这问题,解决方向
1、是否是数据未赋值上
2、打印加载js是否渲染上
所以:在vue中可以使用id的形式和ref的形式进行绑定,如果id绑定渲染不成功的话可以尝试
<div id="printDiv"  ref="printDiv"/>
this.$refs.printDiv
print({
    printable: this.$refs.printDiv,
    type: 'html',
    scanStyles: false
})
vue中printjs使用指南
使用攻略
参考文档:https://printjs.crabbly.com/
分页:使用css的 page-break-after:always 来控制在某个DIV之后新开一个页面
<div style="page-break-after:always">我是封面</div> <div style="page-break-after:always">我是目录,你不知道我的内容有多长</div> <div>我是正文,我需要从一个新页面开始展示</div>
标题栏每页重复打印:因为打印时,表格中的thead和tfoot默认会在每一页重复打印,所以只要设置好表格结构就行。
html打印
<div id="printJS-table">
    <table class="printTable" style="width: 100%; text-align: center" border="0">
      <thead>
        <tr class="maintitle">
          <td :colspan="columns.length">
            <span style="font-size: 20px">{{ modelRef.hospitalName }} 标本阳性率报表</span>
          </td>
        </tr>
        <tr class="subtitle">
          <td :colspan="2">
            统计时间:{{ modelRef.inlineForm.timeSpan[0].format('YYYY/MM/DD') }}-{{
              modelRef.inlineForm.timeSpan[1].format('YYYY/MM/DD')
            }}
          </td>
          <td :colspan="columns.length - 4">制表人:{{ modelRef.username }}</td>
          <td :colspan="2">打印时间:{{ moment().format('YYYY/MM/DD') }}</td>
        </tr>
        <tr class="header">
          <th v-for="item in columns" width="">{{ item.title }}</th>
        </tr>
      </thead>
      <tbody>
        <tr
          v-for="(row, index) in printDatalist"
          :class="{ strongRow: row.sampletype == '合计' }"
        >
          <td
            :class="{ lastRow: printDatalist.length == index + 1 }"
            v-for="column in columns"
          >
            {{ row[column.dataIndex] }}
          </td>
        </tr>
      </tbody>
    </table>
  </div>
  const handlePrint1 = () => {
   printJS({
     printable: 'printJS-table',
     type: 'html',
     // targetStyles: ['*'],
     // headerStyle: 'font-size:20px',
     style: `
     @page {  
       margin: 0.5cm; margin-right: 0.5cm; margin-top: 0.5cm; margin-bottom: 0.3cm; padding-bottom: 0px;}
     .printTable thead .maintitle {
       text-align: center;
       font-size: 20px;
     }
     .printTable thead .subtitle {
       text-align: center;
       font-size: 13px;
     }
     .printTable thead .header th {
       border-top: 1px solid #000;
       border-right: 1px solid #000;
     }
     .printTable thead .header th:nth-child(1) {
       border-left: 1px solid #000;
     }
     tbody td {
       border: 1px solid #000;
     }
     .printTable tbody .strongRow {
       font-weight: bold;
     }
     tbody {
       text-align: center;
     }
     table {
       border-collapse: collapse;
     }
     `,
   });
 };
以json格式打印
    const handlePrint = () => {
      let printColumns = columns.value.map((ele: any) => {
        return {
          field: ele.dataIndex,
          displayName: ele.title,
        };
      });
      printJS({
        printable: dataSource.value,
        maxWidth: 2500,
        properties: printColumns,
        header: `
          <div class="titleDiv">
            <div class="title">${modelRef.hospitalName} 标本阳性率报表</div>
            <div class="subtitle">
              <div class="label">
                 统计时间:${modelRef.inlineForm.timeSpan[0].format(
                  'YYYY/MM/DD',
                )}-${modelRef.inlineForm.timeSpan[1].format('YYYY/MM/DD')}
              </div>
              <div class="label">制表人:${modelRef.username}</div>
              <div class="label">打印时间:${moment().format('YYYY/MM/DD')} </div>
            </div>
          </div>
        `,
        //size: landscape;
        style: `@page {  margin: 0.5cm; margin-right: 0.5cm; margin-top: 0.5cm; margin-bottom: 0.3cm; padding-bottom: 0px; }
          .title {
            margin-top: 10px;
            font-size: 20px;
            text-align: center;
            padding:10px;
          }
          .subtitle {
            display: flex;
            justify-content: space-between;
            font-size: 16px;
            padding:10px;
          }
          .subtitle .label {
            flex: 1;
            font-size: 14px;
          }
          .subtitle .label:nth-child(2) {
            text-align: center;
          }
          .subtitle .label:nth-child(3) {
            text-align: right;
          }
      
          `,
        gridStyle: 'text-align: center; border: 1px solid black;',
        gridHeaderStyle:
          'border-top: 1px solid black; border-right: 1px solid black; border-left: 1px solid black; border-bottom: 0px;',
        type: 'json',
      });
    };
参数
以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。
