J S打印事件

<template>
  <div class="layout-container">
    <div style="text-align: left; padding: 15px;">
      <el-button @click="print">打印</el-button>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
  setup() {
    const print = () => {
      let headerStr = `
        <html>
          <head>
            <title></title>
            <style>
              table,table tr th, table tr td { border:1px solid #000; }
              table { width: 100%; min-height: 30px; line-height: 30px; text-align: center; border-collapse: collapse; margin-bottom: 20px;}
            </style>
          </head>
        <body>
      `
      let footerStr = "</body></html>"
      let printData = `
        <h1 style="text-align: center;">采购订单</h1>
        <div style="border-bottom: 1px solid #000; padding-bottom: 20px;">
          <div style="margin-right: 100px; display: inline-block;">订单号 ORD210000229</div>
          <div style="display: inline-block;">订单日期 2021-06-26</div>
        </div>
        <div style="padding: 10px 0;">
          <div style="margin: 10px 0;">
            <div style="display: inline-block; width: 49%;">供应商  其他厂家</div>
            <div style="display: inline-block; width: 49%;">购货方 这是一个名称</div>
          </div>
          <div style="margin: 10px 0;">
            <div style="display: inline-block; width: 49%;">联系方式  123456789</div>
            <div style="display: inline-block; width: 49%;">采购员 张三</div>
          </div>
          <div style="margin: 10px 0;">
            <div style="display: inline-block; width: 49%;">地址 上尚德缓刑枯栽春树暮云花飘万家雪</div>
            <div style="display: inline-block; width: 49%;">购货方 这是一个名称</div>
          </div>
          <div style="margin: 10px 0;">
            <div style="display: inline-block; width: 49%;">供应商  其他厂家</div>
            <div style="display: inline-block; width: 49%;">购货方 这是一个名称</div>
          </div>
        </div>
        <table>
          <tr>
            <th>商品编号</th>
            <th>商品名称</th>
            <th>单位</th>
            <th>数量</th>
            <th>单价</th>
            <th>金额</th>
            <th>零售价</th>
            <th>条码</th>
          </tr>
          <tr>
            <td>082002</td>
            <td>9803 防哂型女士旅行衣</td>
            <td>件</td>
            <td>110.0</td>
            <td>56.00</td>
            <td>6160.00</td>
            <td>148.00</td>
            <td>699902100000011</td>
          </tr>
        </table>
        <table>
          <tr>
            <th>商品编号</th>
            <th>商品名称</th>
            <th>单位</th>
            <th>数量</th>
            <th>单价</th>
            <th>金额</th>
            <th>零售价</th>
            <th>条码</th>
          </tr>
          <tr>
            <td>082002</td>
            <td>9803 防哂型女士旅行衣</td>
            <td>件</td>
            <td>110.0</td>
            <td>56.00</td>
            <td>6160.00</td>
            <td>148.00</td>
            <td>699902100000011</td>
          </tr>
        </table>
        <table>
          <tr>
            <th>商品编号</th>
            <th>商品名称</th>
            <th>单位</th>
            <th>数量</th>
            <th>单价</th>
            <th>金额</th>
            <th>零售价</th>
            <th>条码</th>
          </tr>
          <tr>
            <td>082002</td>
            <td>9803 防哂型女士旅行衣</td>
            <td>件</td>
            <td>110.0</td>
            <td>56.00</td>
            <td>6160.00</td>
            <td>148.00</td>
            <td>699902100000011</td>
          </tr>
        </table>
      `
      let wind = window.open("", "订单详情",
					"toolbar=no,scrollbars=yes,menubar=no")
      wind.document.body.innerHTML = headerStr + printData + footerStr
      wind.print()
      console.log(123)
      wind.close()
    }
    return {
      print
    }
  }
})
</script>

 

 

### 如何使用 PrintJS 进行打印 PrintJS 是一款轻量级的 JavaScript 库,用于简化网页中的打印功能。它能够轻松实现 HTML 和 PDF 文件的打印操作,并提供多种自定义选项来满足不同的需求。 以下是关于如何使用 PrintJS 的详细介绍: #### 安装 PrintJS 可以通过 npm 或者直接引入 CDN 链接安装 PrintJS: ```html <!-- 使用CDN链接 --> <script src="https://printjs-4de6.kxcdn.com/print.min.js"></script> <link href="https://printjs-4de6.kxcdn.com/print.min.css" rel="stylesheet"> ``` 或者通过 npm 安装到项目中: ```bash npm install print-js ``` #### 打印基本HTML页面 要打印一个简单的 HTML 页面,可以调用 `printJS` 函数并传入目标元素的选择器或 ID。 ```javascript // 打印整个文档 printJS({ printable: 'document', // 可以是一个选择器、ID 或 DOM 对象 type: 'html', header: 'This is the Header' // 设置页眉 }); ``` 此方法会自动捕获指定区域的内容并将其发送至打印机[^1]。 #### 打印PDF文件 如果需要打印 PDF 文件,则只需传递 PDF URL 即可完成配置。 ```javascript printJS('path/to/your/document.pdf'); ``` 上述代码片段将会加载给定路径下的 PDF 并启动打印对话框[^2]。 #### 自定义样式与属性 为了更好地控制输出效果,还可以设置更多参数比如方向(portrait vs landscape),单位(inches/cm/mm), 纸张大小(letter/A4/custom dimensions)等等。 ```javascript printJS({ printable: 'myElementId', type: 'html', targetStyles: ['*'], // 继承原有CSS样式 properties: [ { field: 'name', displayName: 'Name', type: 'string'}, { field: 'age', displayName: 'Age', type: 'number'} ], scanBarcodes: true, honorMarginPadding: false, ignoreElements: ['header','footer'] }); ``` 以上脚本不仅限于基础打印任务;还支持复杂表格数据处理以及条形码扫描等功能[^3]。 #### 注意事项 - 如果应用程序是由其他程序驱动执行批量作业时(例如后台服务触发打印动作),建议关闭命令提示符窗口暂停功能[-dNOPAUSE][-dBATCH]. - 当涉及到敏感信息安全问题时,请务必确认所使用的API接口已加密传输(HTTPs).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值