vxe-table 實現列印出貨單、自定義單據列印

可不简单發表於2024-11-25

vxe-table 內建非常強大 H5 列印,只需要傳入 html 程式碼和 css 樣式,就可以實現任何複雜的列印

官網:https://vxeui.com

<template>
  <div>
    <vxe-button @click="printEvent">點選列印</vxe-button>

    <div style="border: 1px solid #409eff; padding: 16px">
      <div ref="topElRef">
        <div style="margin-bottom: 8px;">
          <div style="display: inline-block;width: 100%;">
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">商品名稱:Vxe 企業授權</div>
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">發貨單號:X2665847132654</div>
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">發貨日期:2024-12-01</div>
          </div>
          <div style="display: inline-block;width: 100%;">
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">收貨姓名:小徐</div>
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">收貨地址:火星第七區18號001</div>
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">聯絡電話:10086</div>
          </div>
        </div>
      </div>

      <vxe-grid ref="gridRef" v-bind="gridOptions"></vxe-grid>

      <div ref="bottomElRef">
        <div style="margin-top: 30px;text-align: right;">
          <div style="display: inline-block;width: 100%;">
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;"></div>
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">建立人:小徐</div>
            <div style="float: left; width: 33.33%;height: 28px;line-height: 28px;">建立日期:2024-12-01</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { VxeUI } from 'vxe-table'

export default {
  data () {
    const gridOptions = {
      border: true,
      columns: [
        { type: 'seq', width: 60 },
        { field: 'name', title: '名稱' },
        { field: 'code', title: '編碼' },
        { field: 'num', title: '數量' },
        { field: 'price', title: '價格' },
        { field: 'taxRate', title: '稅率' }
      ],
      data: [
        { id: 10001, name: '篩選外掛', code: 'T1', price: 1200, num: 1, taxRate: '3%' },
        { id: 10002, name: '區域選取外掛', code: 'T2', price: 16840, num: 3, taxRate: '3%' },
        { id: 10003, name: '單元格選取外掛', code: 'T3', price: 8460, num: 3, taxRate: '3%' },
        { id: 10004, name: 'Excel 篩選外掛', code: 'T4', price: 980, num: 2, taxRate: '3%' },
        { id: 10005, name: '零程式碼平臺模板', code: 'T5', price: 23199, num: 5, taxRate: '3%' },
        { id: 10006, name: '後臺管理系統模板', code: 'T6', price: 899, num: 7, taxRate: '3%' },
        { id: 10007, name: '低程式碼設計器外掛', code: 'T7', price: 1688, num: 4, taxRate: '3%' },
        { id: 10008, name: '視覺化拖拽外掛', code: 'T8', price: 1299, num: 4, taxRate: '3%' }
      ]
    }
    return {
      gridOptions
    }
  },
  methods: {
    async printEvent () {
      const $grid = this.$refs.gridRef
      if ($grid) {
        const printRest = await $grid.getPrintHtml()
        const topEl = this.$refs.topElRef
        const bottomEl = this.$refs.bottomElRef
        const topHtml = topEl ? topEl.innerHTML : ''
        const bottomHtml = bottomEl ? bottomEl.innerHTML : ''
        VxeUI.print({
          title: '出貨單據',
          pageBreaks: [
            // 第一頁
            {
              bodyHtml: topHtml + printRest.html + bottomHtml
            }
          ]
        })
      }
    }
  }
}
</script>

https://github.com/x-extends/vxe-table

相關文章