当前位置: 首页 > 知识库问答 >
问题:

前端 - 如何原封不动地打印el-table和el-form?

凌征
2023-05-02

点击打印后可以打印表格和表单的数据,同时样式也在

<template>
  <div>
    <div id="printFrom" ref="printDom">
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column prop="date" label="日期" align="center"></el-table-column>
        <el-table-column prop="name" label="姓名" align="center"></el-table-column>
        <el-table-column prop="address" label="地址" align="center"></el-table-column>
        <el-table-column prop="address" label="地址" align="center"></el-table-column>
        <el-table-column prop="address" label="地址" align="center"></el-table-column>
        <el-table-column prop="address" label="地址" align="center"></el-table-column>
        <el-table-column prop="address" label="地址" align="center"></el-table-column>
        <el-table-column prop="address" label="地址" align="center"></el-table-column>
      </el-table>
      <el-form>
        <el-row>
          <el-col :span="12">
            <el-form ref="form" :model="form" label-width="100px">
              <el-form-item label="姓名">
                <el-input v-model="form.name"></el-input>
              </el-form-item>
              <el-form-item label="身份证">
                <el-input v-model="form.name"></el-input>
              </el-form-item>
              <el-form-item label="性别">
                <el-input v-model="form.name"></el-input>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="12">
            <el-table :data="tableData" style="width: 100%" border>
              <el-table-column prop="date" label="日期" align="center"></el-table-column>
              <el-table-column prop="name" label="姓名" align="center"></el-table-column>
              <el-table-column prop="address" label="地址" align="center"></el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <button @click="print">打印</button>
  </div>
</template>

<style lang="scss" scoped>
</style>


<script>
export default {
  data() {
    return {
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄',
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄',
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
        },
      ],
      form: {
        name: '',
      },
    }
  },
  methods: {
    //打印
    print() {},
  },
}
</script>

改用vue-print-nb解决了整个问题

<template>
  <div>
    <div id="printFrom" ref="printDom">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="活动区域">
          <el-select v-model="form.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动时间">
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.date1"
              style="width: 100%;"
            ></el-date-picker>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-time-picker placeholder="选择时间" v-model="form.date2" style="width: 100%;"></el-time-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="即时配送">
          <el-switch v-model="form.delivery"></el-switch>
        </el-form-item>
        <el-form-item label="活动性质">
          <el-checkbox-group v-model="form.type">
            <el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
            <el-checkbox label="地推活动" name="type"></el-checkbox>
            <el-checkbox label="线下主题活动" name="type"></el-checkbox>
            <el-checkbox label="单纯品牌曝光" name="type"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="特殊资源">
          <el-radio-group v-model="form.resource">
            <el-radio label="线上品牌商赞助"></el-radio>
            <el-radio label="线下场地免费"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="活动形式">
          <el-input type="textarea" v-model="form.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="no-print">立即创建</el-button>
          <el-button class="no-print">取消</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column prop="date" label="日期" align="center"></el-table-column>
        <el-table-column prop="name" label="姓名" align="center"></el-table-column>
        <el-table-column prop="address" label="地址" align="center"></el-table-column>
        <el-table-column prop="address" label="地址" align="center"></el-table-column>
        <el-table-column prop="address" label="地址" align="center"></el-table-column>
        <el-table-column prop="address" label="地址" align="center"></el-table-column>
        <el-table-column prop="address" label="地址" align="center"></el-table-column>
        <el-table-column prop="address" label="地址" align="center"></el-table-column>
      </el-table>
      <el-descriptions title="用户信息">
        <el-descriptions-item label="用户名">kooriookami</el-descriptions-item>
        <el-descriptions-item label="手机号">18100000000</el-descriptions-item>
        <el-descriptions-item label="居住地">苏州市</el-descriptions-item>
        <el-descriptions-item label="备注">
          <el-tag size="small">学校</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="联系地址">江苏省苏州市吴中区吴中大道 1188 号</el-descriptions-item>
      </el-descriptions>
    </div>
    <button v-print="printObj">打印</button>
  </div>
</template>

<style  lang="scss" scoped>
@page {
  size: auto;
  margin: 3mm;
}
@media print {
  /deep/ table {
    table-layout: auto !important;
  }
  /deep/ .el-table__header-wrapper .el-table__header {
    width: 100% !important;
  }
  /deep/ .el-table__body-wrapper .el-table__body {
    width: 100% !important;
  }
  /deep/ #print table {
    table-layout: fixed !important;
  }

  /deep/.el-table__fixed {
    display: none;
  }
  /deep/.el-table .el-table__cell.is-hidden > * {
    visibility: visible;
    font-size: 12px;
  }
  .no-print {
    display: none;
  }
}
</style>

<script>
import print from 'vue-print-nb'

export default {
  directives: {
    print,
  },
  data() {
    return {
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄',
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄',
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
        },
      ],
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: '',
      },

      printObj: {
        id: 'printFrom',
        popTitle: '打印', // 打印配置页上方标题
        extraHead: '', //最上方的头部文字,附加在head标签上的额外标签,使用逗号分隔
        preview: '', // 是否启动预览模式,默认是false(开启预览模式,可以先预览后打印)
        previewTitle: '', // 打印预览的标题(开启预览模式后出现),
        previewPrintBtnLabel: '', // 打印预览的标题的下方按钮文本,点击可进入打印(开启预览模式后出现)
        zIndex: '', // 预览的窗口的z-index,默认是 20002(此值要高一些,这涉及到预览模式是否显示在最上面)
        previewBeforeOpenCallback: () => {}, //预览窗口打开之前的callback(开启预览模式调用)
        previewOpenCallback: () => {}, // 预览窗口打开之后的callback(开启预览模式调用)
        beforeOpenCallback: () => {}, // 开启打印前的回调事件
        openCallback: () => {}, // 调用打印之后的回调事件
        closeCallback: () => {}, //关闭打印的回调事件(无法确定点击的是确认还是取消)
        url: '',
        standard: '',
        extraCss: '',
      },
    }
  },
  methods: {},
}
</script>

共有1个答案

艾翼
2023-05-02

用print.js
先安装:
npm install print-js

然后使用:

print() {
  printJS({
    printable: this.$refs.printDom.innerHTML,
    type: 'html',
    header: '打印预览', // 自定义打印预览的标题
    css: [
      process.env.BASE_URL + 'node_modules/element-ui/lib/theme-chalk/index.css',
      process.env.BASE_URL + 'path/to/your/custom/styles.css'
    ],
    scanStyles: false
  })
}

或者用Vue HTML to Paper其实你的功能需求就是弄成pdf
https://github.com/mycurelabs/vue-html-to-paper

import Vue from 'vue';
import VueHtmlToPaper from 'vue-html-to-paper';

const options = {
  name: '_blank',
  specs: ['fullscreen=yes', 'titlebar=yes', 'scrollbars=yes'],
  styles: [
    'https://unpkg.com/element-ui/lib/theme-chalk/index.css',
    // 在此处添加其他样式表路径
  ],
};

Vue.use(VueHtmlToPaper, options);

组件里:

methods: {
  print() {
    this.$htmlToPaper(this.$refs.printDom);
  },
},

详细可以看看官网和相关的文章

 类似资料: