使用 Print-js 实现打印、预览功能 前端部分

  • Post author:
  • Post category:其他



一、传送门:

官方文档



大神文档


二、使用方式

安装:

npm install print-js --save

引入:

import print from 'print-js'

直接使用:

// base64格式
let base64Data = res.data
print({
    printable: base64Data, type: 'pdf', base64: true })


三、封装

打印预览 组件封装:

<template>
  <div style="height:auto;">
    <el-button @click="printHtml" v-show="isPrint">打印</el-button>
    <iframe
      :class="{ 'iframe_scale': isScale }"
      :id="'show-iframe'+id"
      frameborder="0"
      scrolling="yes"
      style="background-color:transparent; position:absolute;width: 100%;
         height:100%; top: 0;left:0;bottom:0;"
    />
  </div>
</template>

<script>
import print from 'print-js'
import request from '@/utils/request'
import to from '@/to'
import * as API_Print from '@/api/print'
export default {
   
  name: 'Index',
  props: {
   
    id: {
   
      type: String,
      default: () => ''
    },
    // 通过链接打印
    htmlUrl: {
   
      type: String,
      default: () => ''
    },
    // 通过文本打印
    htmlText: {
   
      type: String,
      default: () => null
    },
    // 是否显示打印按钮
    isPrint: {
   
      type: Boolean,
      default: () => true



版权声明:本文为weixin_41668951原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。