vue-print如何实现打印功能

 更新时间:2025年04月24日 10:52:22   作者:小泡泡c  
这篇文章主要介绍了vue-print如何实现打印功能问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

一、安装

1. Vue2

npm install vue-print-nb --save
import Print from 'vue-print-nb'
// Global instruction 
Vue.use(Print);

//or

// Local instruction
import print from 'vue-print-nb'

directives: {
    print   
}

2. Vue3

npm install vue3-print-nb --save
// Global instruction 
import { createApp } from 'vue'
import App from './App.vue'
import print from 'vue3-print-nb'
const app = createApp(App)
app.use(print)
app.mount('#app')

//or

// Local instruction
import print from 'vue3-print-nb'

directives: {
    print   
}

二、基本使用

1. 直接打印页面HTML

1)方法

  • ① 给要打印的部分设置一个 id
  • ② 在打印按钮中添加 v-print="'#id名'"

2)代码(以表格为例)

<template>
  <div>
    <a-button v-print="'#printMe'">打印</a-button>
    <a-table :columns="columns" :data-source="data" bordered id="printMe">
  	</a-table>
  </div>
</template>
<script>
const columns = [
  {
    title: 'Name',
    dataIndex: 'name',
  },
  {
    title: 'Cash Assets',
    className: 'column-money',
    dataIndex: 'money',
  },
  {
    title: 'Address',
    dataIndex: 'address',
  },
];

const data = [
  {
    key: '1',
    name: 'John Brown',
    money: '¥300,000.00',
    address: 'New York No. 1 Lake Park',
  },
  {
    key: '2',
    name: 'Jim Green',
    money: '¥1,256,000.00',
    address: 'London No. 1 Lake Park',
  },
  {
    key: '3',
    name: 'Joe Black',
    money: '¥120,000.00',
    address: 'Sidney No. 1 Lake Park',
  },
];

export default {
  data() {
    return {
      data,
      columns,
    };
  },
};
</script>

2. 个性化设置

1)方法

打印按钮的 v-print 绑定一个对象

2)代码

<template>
  <div class="box">
    <a-table :columns="columns" :data-source="data" bordered id="printMe"></a-table>
    <a-button v-print="printContent" class="btn no-print">打印</a-button>
  </div>
</template>
<script>
const columns = [
  {
    title: 'Name',
    dataIndex: 'name',
  },
  {
    title: 'Cash Assets',
    className: 'column-money',
    dataIndex: 'money',
  },
  {
    title: 'Address',
    dataIndex: 'address',
  },
];

const data = [
  {
    key: '1',
    name: 'John Brown',
    money: '¥300,000.00',
    address: 'New York No. 1 Lake Park',
  },
  {
    key: '2',
    name: 'Jim Green',
    money: '¥1,256,000.00',
    address: 'London No. 1 Lake Park',
  },
  {
    key: '3',
    name: 'Joe Black',
    money: '¥120,000.00',
    address: 'Sidney No. 1 Lake Park',
  },
];

export default {
  data() {
    return {
      data,
      columns,
      tableHead: '测试表格',
      printContent: {
        id: "printMe", // 打印的区域
        preview: false, // 预览工具是否启用
        previewTitle: '这是预览标题', // 预览页面的标题
        popTitle: '', // 打印页面的页眉
        extraCss: "https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.css, https://cdn.bootcdn.net/ajax/libs/hover.css/2.3.1/css/hover-min.css",
        extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
        previewBeforeOpenCallback() {
          console.log('正在加载预览窗口')
        },
        previewOpenCallback() {
          console.log('已经加载完预览窗口')
        },
        beforeOpenCallback(vue) {
          vue.printLoading = true
          console.log('打开之前')
        },
        openCallback(vue) {
          vue.printLoading = false
          console.log('执行了打印')
        },
        closeCallback() {
          console.log('关闭了打印工具')
        },
        clickMounted(vue){
          console.log('点击了打印按钮');
          vue.printContent.popTitle = vue.tableHead // 动态设置页眉
        }
      }
    }
  }
};
</script>

3)效果展示

① 预览工具

3. 打印URL

1)方法

  • ① 给 打印按钮的 v-print 绑定一个对象
  • ② 对象添加 url 属性

2)代码

<template>
  <div class="box">
    <a-table :columns="columns" :data-source="data" bordered></a-table>
    <a-button v-print="printContent" class="btn no-print" >打印</a-button>
  </div>
</template>
<script>
const columns = [
  {
    title: 'Name',
    dataIndex: 'name',
  },
  {
    title: 'Cash Assets',
    className: 'column-money',
    dataIndex: 'money',
  },
  {
    title: 'Address',
    dataIndex: 'address',
  },
];

const data = [
  {
    key: '1',
    name: 'John Brown',
    money: '¥300,000.00',
    address: 'New York No. 1 Lake Park',
  },
  {
    key: '2',
    name: 'Jim Green',
    money: '¥1,256,000.00',
    address: 'London No. 1 Lake Park',
  },
  {
    key: '3',
    name: 'Joe Black',
    money: '¥120,000.00',
    address: 'Sidney No. 1 Lake Park',
  },
];

export default {
  data() {
    return {
      data,
      columns,
      tableHead: '测试表格',
      printContent: {
        url: 'http://localhost:8081/', // 打印的url
        preview: false, // 预览工具是否启用
        previewTitle: '这是预览标题',
        popTitle: '', // 打印页面的页眉
        extraCss: "https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.css, https://cdn.bootcdn.net/ajax/libs/hover.css/2.3.1/css/hover-min.css",
        extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
      }
    }
  },
};
</script>

三、API

ParameExplainTypeOptionalValueDefaultValue
idRange print ID, required valueString
standardDocument type (Print local range only)Stringhtml5/loose/stricthtml5
extraHeadAdd DOM nodes in the node, and separate multiple nodes with , (Print local range only)String
extraCssNew CSS style sheet , and separate multiple nodes with ,(Print local range only)String
popTitleContent of label (Print local range only)String
openCallbackCall the successful callback function of the printing toolFunctionReturns the instance of Vue called at that time
closeCallbackClose the callback function of printing tool successFunctionReturns the instance of Vue called at that time
beforeOpenCallbackCallback function before calling printing toolFunctionReturns the instance of Vue called at that time
urlPrint the specified URL. (It is not allowed to set the ID at the same time)String
asyncUrlReturn URL through ‘resolve()’ and VueFunction
previewPreview toolBooleanfalse
previewTitlePreview tool TitleString‘打印预览’
previewPrintBtnLabelThe name of the preview tool buttonString‘打印’
zIndexCSS of preview tool: z-indexString,Number20002
previewBeforeOpenCallbackCallback function before starting preview toolFunctionReturns the instance of Vue
previewOpenCallbackCallback function after fully opening preview toolFunctionReturns the instance of Vue
clickMountedClick the callback function of the print buttonFunctionReturns the instance of Vue

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • vue vant Area组件使用详解

    vue vant Area组件使用详解

    这篇文章主要介绍了vue vant Area组件使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • vue移动端设置全屏背景的项目实践

    vue移动端设置全屏背景的项目实践

    本vue移动端项目设置全屏背景,关键是要找对文件,然后添加background属性即可,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2023-08-08
  • Vue如何通过浏览器控制台查看全局data值

    Vue如何通过浏览器控制台查看全局data值

    在写vue项目时想到一个问题,项目里面的文件都是一个个的组件,如何在控制台中修改,查看组件data里的值呢,下面这篇文章主要给大家介绍了关于Vue如何通过浏览器控制台查看全局data值的相关资料,需要的朋友可以参考下
    2023-04-04
  • Vue3中引入scss文件的方法步骤

    Vue3中引入scss文件的方法步骤

    这篇文章主要给大家介绍了关于Vue3中引入scss文件的方法步骤,在实际项目中,各种样式往往有很多重复的情况,为了能够使样式的后续开发和维护更加惬意,将这些共同的代码进行命名然后调用这些变量是一个很好的选择,需要的朋友可以参考下
    2023-08-08
  • Vue elementui字体图标显示问题解决方案

    Vue elementui字体图标显示问题解决方案

    这篇文章主要介绍了Vue elementui字体图标显示问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • vue2.0+webpack环境的构造过程

    vue2.0+webpack环境的构造过程

    本文分步骤给大家介绍了vue2.0+webpack环境的构造过程的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下
    2016-11-11
  • vue路由组件按需加载的几种方法小结

    vue路由组件按需加载的几种方法小结

    这篇文章主要介绍了vue路由组件按需加载的几种方法小结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • 实例详解vue中的代理proxy

    实例详解vue中的代理proxy

    这篇文章主要介绍了vue中的代理proxy,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-02-02
  • 一文轻松理解Vuex

    一文轻松理解Vuex

    这篇文章主要介绍了Vuex及其使用方法,感兴趣的同学,可以参考下
    2021-04-04
  • Vue父子组件数据双向绑定(父传子、子传父)及ref、$refs、is、:is的使用与区别

    Vue父子组件数据双向绑定(父传子、子传父)及ref、$refs、is、:is的使用与区别

    这篇文章主要介绍了Vue父子组件数据双向绑定(父传子、子传父)及ref、$refs、is、:is的使用与区别,需要的朋友可以参考下
    2022-12-12

最新评论