使用print.js实现打印

本文介绍了如何在Vue.js项目中使用print.js库进行打印操作。首先从GitHub下载并引入print.js到项目的utils目录,然后在main.js中注册使用。在页面上创建打印弹框,并通过ref获取打印内容的DOM节点。输出Print方法用于调用打印功能,同时展示了如何设置打印样式的CSS,包括页面大小、方向和边距等。在实际应用中,需注意使用ref而不是id或class来避免webpack打包后的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下载print.js
https://github.com/xyl66/vuePlugs_printjs
下载后,放在utils路径下

导入
main.js文件中引入

import Print from '@/utils/print';
Vue.use(Print);

在页面上使用

// 打印的弹框
<el-dialog
  title="报告"
  width="1054px"
  :visible.sync="reportVisible"
  style="height: 800px; overflow: auto"
  :close-on-click-modal="false"
  :close-on-press-escape="false"
>

  <!-- 打印的内容 -->
  <reportPrintDia ref="reportPrint" :printData="baseData" id="pdfDom" />
  
  <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="getPdf()">PDF打印</el-button>
    <el-button type="primary" @click="outputPrint">打印</el-button>
    <el-button type="danger" @click="getBackInfo">返回</el-button>
  </div>
</el-dialog>

注意:打印获取dom节点需要使用ref,如果直接通过id或class获取,webpack打包部署后打印内容为空。

// 打印
outputPrint() {
  console.log(this.$refs.reportPrint.$refs.toPrint);
    const dom = window._.get(this, '$refs.reportPrint.$refs.toPrint', null);
    if (dom) {
        this.$print(dom, { 'no-print': '.no-want-print' });
    } else {
        console.error('dom找不到');
    }
},
// 打印样式 比如
@media print {
	/* @page规则允许指定页面盒子多个方面 */
	/* @page不要嵌套在容器中,与其他dom无关 */
	@page {
	  /* 纵向打印 */
	  // size: portrait;
	
	  /* 横向打印 */
	  size: landscape;
	  
	  /* 指定尺寸 5.5*8.5英尺 */
	  size: 5.5in 8.5in;
	  
	  /* 指定纸质尺寸 A4或legal*/
	  size: A4;
	
	  /* 去掉页眉页脚*/
	  margin-top: 0;
	  margin-bottom: 0;
	}
	
	/* 页面左边距,右边距 为装订边留出更多的空间,用户也可以不用 */
	/* 与布局是横向和纵向的也有关系 */
	@page :left {
	    margin-left: 2.5cm;
	    margin-right: 2.7cm;
	}
	
	@page :right {
	    margin-left: 2.7cm;
	    margin-right: 2.5cm;
	}
	/* @page :first 选文档第一页 */
	/* 首页上页边距设置为 10cm */
	@page :first {
	    margin-top: 10cm; 
	}

}
### 实现 Vue 3 中使用 Print.js 进行页面打印 为了在 Vue 3 项目中集成 `print.js` 来实现页面打印功能,可以遵循如下方法: #### 安装依赖库 首先,在项目根目录下通过 npm 或 yarn 安装 `print.js` 库。 ```bash npm install print-js --save ``` 或者 ```bash yarn add print-js ``` 这一步骤确保了开发环境中包含了必要的 JavaScript 打印工具[^1]。 #### 导入并初始化插件 接着,在项目的入口文件(通常是 main.js),导入刚刚安装好的 `print.js` 并注册全局组件或按需引入到特定组件里。 对于全局应用的情况可以在 `main.js` 文件中加入以下代码片段: ```javascript import { createApp } from 'vue' import App from './App.vue' // Importing the library globally import 'print-js/dist/print.min.css' // Optional CSS styling provided by print.js window.printJS = require('print-js') const app = createApp(App) app.mount('#app') ``` 如果只想局部加载,则可以直接在目标组件内部完成上述操作[^2]。 #### 创建打印按钮及其事件处理函数 最后,在希望触发打印行为的地方创建一个按钮,并绑定点击事件来调用 `print()` 方法。下面是一个简单的例子展示如何在一个名为 `PrintComponent.vue` 的单文件组件中设置此逻辑: ```html <template> <button @click="handlePrint">Print This Page</button> </template> <script setup lang="ts"> function handlePrint() { window.printJS({ printable: 'content-to-print', // ID of HTML element or URL string type: 'html', targetStyles: ['*'] // Copy all styles into iframe where content will be printed }) } </script> <style scoped> /* Add custom style here */ </style> ``` 以上就是在 Vue 3 项目中利用 `print.js` 插件执行页面打印任务的方法概述[^3].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值