使用print.js实现打印

下载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; 
	}

}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值