vue 使用浏览器自带打印机打印功能

本文主要介绍了vue项目中使用print.js打印,解决多页,分页,打印预览样式修改等问题。

引入安装vue-print.js

cnpm i vue-printjs --save-dev

解决打印多页只出现一页问题
由于打印插件存在问题,如果打印文件超出一页,只显示一页,所以我们需要修改print.js源文件,所以只能手动下载vue-print.js到本地,做一些修改,然后引入到项目中,不能使用npm安装

下载 print.js
https://github.com/zxc19890923/print/blob/master/print.js
在src目录下面创建plugins/print/Print.js文件保存插件内容

修改 print.js

// 搜索getStyle方法,添加:str += "<style>html,body,div{height: auto!important;font-size:14px}</style>";

 getStyle: function () {
  var str = "",
   styles = document.querySelectorAll('style,link');
  for (var i = 0; i < styles.length; i++) {
   str += styles[i].outerHTML;
  }
  str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
  str += "<style>html,body,div{height: auto!important;font-size:14px}</style>";

  return str;
 },

main.js中引入插件

import Print from './plugins/print/Print'
Vue.use(Print)

vue文件中的使用

<template>
  <div ref="print" class="app-container" id="print">
    <el-form>
      <el-form-item>
        <div>
          <b>姓 名</b>
        </div>
        <el-input v-model="user.name"></el-input>
      </el-form-item>

      <el-form-item>
        <div>
          <b>身份证号</b>
        </div>
        <el-input v-model="user.card_id"></el-input>
      </el-form-item>

      <el-form-item>
        <div>
          <b>护 照 号</b>
        </div>
        <el-input v-model="user.passport_id"></el-input>
      </el-form-item>

      <el-row>
        <el-col :span="4" style="margin-right:20px;">
          <div>
            <b>收样时间</b>
          </div>
          <el-date-picker
            class="date-box"
            type="date"
            v-model="user.receive_date"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-col>
        <el-col :span="4" style="margin-right:20px;">
          <div>
            <b>检测时间</b>
          </div>
          <el-date-picker type="date" v-model="user.detect_date" value-format="yyyy-MM-dd"></el-date-picker>
        </el-col>
      </el-row>

      <el-form-item>
        <div>
          <b>检测方法</b>
        </div>
        <el-select style="width:100%;" v-model="user.detect_way" placeholder="请选择检测方法">
          <el-option v-for="item of way" :label="item.label" :value="item.name"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <div>
          <b>检测结果</b>
        </div>
        <el-select style="width:100%;" v-model="user.detect_fruit" placeholder="请选择检测结果">
          <el-option v-for="item of result" :label="item.label" :value="item.name"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item class="no-print">
        <el-button type="primary" @click="submit()">只提交</el-button>
        <el-button type="primary" @click="printContext">打印并提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
methods: {
    //打印
    printContext() {
      this.$print(this.$refs.print);
    }
    // 不打印方法1. 添加no-print样式类
    // 不打印方法2. this.$print(this.$refs.print,{'no-print':'.do-not-print-div'})
  }

打印结果
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值