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