#第一步安装插件
npm install print-js --save
//查看是否安装成功 package.json文件中查看print-js依赖。
"dependencies": {
"axios": "^0.19.2",
"babel-polyfill": "^6.26.0",
"element-ui": "^2.15.6",
"file-saver": "^2.0.5",
"pinyin-match": "^1.2.2",
"print-js": "^1.6.0",
"vue": "^2.5.2",
"vue-router": "^3.0.1",
"vuex": "^3.1.2",
"xlsx": "^0.17.0",
"xlsx-style": "^0.8.13"
},
#第二步 在要打印的页面引入依赖
import printJS from 'print-js'
#第三步 写好页面要打印的内容 以id print为打印内容
<div class="data-card" id="print">
<el-table size="mini" :key="num" id="mytable"
ref="table"
:data="tableDataDetail"
border
style="width: 100%">
<el-table-column
align="center"
:label="title">
</el-table-column>
</el-table>
</div>
#打印事件及方法
<el-button type="primary" @click="handlePrint">打印</el-button>
// body{zoom:80%;} 如果需要缩放把这个放入style中
handlePrint() {
let userAgent = navigator.userAgent;
//判断是否Firefox浏览器
if (userAgent.indexOf("Firefox") > -1) {
console.log('Firefox')
printJS({
printable: 'mytable1', // 标签元素id
type: 'html',
header: '',
style: `@page {size:auto;margin-top:100px; margin-left:15px; margin-right: 15px;}
thead th {
border-top: 1px solid #000;
border-right: 1px solid #000;
border-left: 1px solid #000;
}
tbody td {
border: 1px solid #000;
}
tbody {
text-align: center;
}
table {
border-collapse: collapse;
}`,
});
}
//判断是否chorme浏览器
if (userAgent.indexOf("Chrome") > -1) {
console.log('Chrome')
printJS({
printable: 'mytable1', // 标签元素id
type: 'html',
maxWidth: 1150,
scanStyles: false,//设置为false时,库将不会处理应用于正在打印的html的样式。使用css参数时很有用。不然设置css没啥卵用
// 继承原来的所有样式
targetStyles: ["*"],
css: "@/components/print.css",
header: `<div style="display: flex;justify-content: space-between;">
<div style="font-size:26px;line-height:40px;color:#272620">瑞佰快跑调度单</div>
<div style="line-height: 40px;font-size: 16px;color:#272620">运单号:505554564645</div>
</div> <div style="line-height: 22px;">客户名称:福州恒泰联创食品有限公司(电商)</div>
<div style="line-height:22px;">发货地址:四川省成都市双流区九江街道万家社区8组220号</div>
<div style="display: flex;line-height:22px"><span style="margin-right: 20px;">现场联系:张武安(13980075384)</span><span style="margin-right: 20px;">送货方式:公路运输</span><span>运货方式:汽运</span></div>`,
documentTitle: '',
style: `@page {size:auto;margin-top:20px; margin-left:30px; margin-right: 30px;}
thead th {
border-top: 1px solid #000;
border-right: 1px solid #000;
border-left: 1px solid #000;
}
tbody td {
border: 1px solid #000;
}
tbody {
text-align: center;
}
table {
border-collapse: collapse;
}`,
});
}
//判断是否IE浏览器
if (!!window.ActiveXObject || "ActiveXObject" in window) {
console.log('IE')
printJS({
printable: 'mytable1', // 标签元素id
type: 'html',
header: '',
style: `@page {size:auto;margin-top:100px; margin-left:15px; margin-right: 15px;}
thead th {
border-top: 1px solid #000;
border-right: 1px solid #000;
border-left: 1px solid #000;
}
tbody td {
border: 1px solid #000;
}
tbody {
text-align: center;
}
table {
border-collapse: collapse;
}`,
});
}
},
#全文
<template>
<el-dialog :visible.sync="visible" title="打印" width="1051px" :close-on-click-modal="false" @open="handleOpen"
:before-close="closeDialog">
<div>
<div style="display: flex;justify-content: space-between;">
<div style="font-size:26px;line-height:40px;color:#272620">瑞佰快跑调度单</div>
<div style="line-height: 40px;font-size: 16px;color:#272620">运单号:505554564645</div>
</div>
<div id="printAre">
<div style="line-height: 22px;">客户名称:福州恒泰联创食品有限公司(电商)</div>
<div style="line-height:22px;">发货地址:四川省成都市双流区九江街道万家社区8组220号</div>
<div style="display: flex;line-height:22px"><span
style="margin-right: 20px;">现场联系:张武安(13980075384)</span><span
style="margin-right: 20px;">送货方式:公路运输</span><span>运货方式:汽运</span></div>
<div class="data-card" id="mytable1">
<div class="content">
<el-table :data="tableData" border style="width: 100%">
<el-table-column type="index" label="NO" width="50">
</el-table-column>
<el-table-column prop="name" label="订单号" width="120">
</el-table-column>
<el-table-column prop="age" label="原始单号" width="120">
</el-table-column>
<el-table-column prop="gender" label="收货人" width="120">
</el-table-column>
<el-table-column prop="province" label="电话" width="120">
</el-table-column>
<el-table-column prop="city" label="收货地址" width="400">
</el-table-column>
<el-table-column prop="address" label="货物数量" width="80">
</el-table-column>
</el-table>
<div style="display: flex;justify-content: space-between;line-height: 30px;">
<div>毛重合计(吨):3.300</div>
<div>体积(m³):390.500</div>
<div>总合计:500</div>
</div>
<div style="display: flex;justify-content: space-between;line-height: 30px;font-size: 12px;">
<div>打印次数:首次打印</div>
<div>货车司机:张三</div>
<div>车牌号:川A99999</div>
<div>温控仪:3882(TR-2B)</div>
</div>
<div style="display: flex;font-size: 12px;">
<div style="margin-right: 50px;">打印:王师傅 2023.05.05</div>
<div>敬请点清品种和数量后签收,如有问题请电至028-35552147</div>
</div>
</div>
</div>
</div>
<!-- <el-button type="primary" v-print="printAre">打印</el-button> -->
<el-button type="primary" @click="handlePrint">打印</el-button>
</div>
</el-dialog>
</template>
<script>
import printJS from 'print-js'
import {
ti_payment
} from "@/api/common";
export default {
name: "Popup",
props: {
visible: {
type: Boolean,
required: true,
},
rowdata: {
type: [Object, String],
default: "",
},
},
data() {
return {
order: "WRB1683615783653147",
// 打印设置
printAre: {
id: "printAre",
popTitle: "页眉部分",
extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
},
tableData: [{
name: '2790027913',
age: "2790027913",
gender: '独孤求败',
province: '13554152623',
city: '四川省成都市双流区九江北地铁站c口旁边往前两百米',
address: '450',
},
{
name: '2790027913',
age: "2790027913",
gender: '独孤求败',
province: '13554152623',
city: '四川省成都市双流区九江北地铁站c口旁边往前两百米',
address: '450',
},
{
name: '2790027913',
age: "2790027913",
gender: '独孤求败',
province: '13554152623',
city: '四川省成都市双流区九江北地铁站c口旁边往前两百米',
address: '450',
},
{
name: '2790027913',
age: "2790027913",
gender: '独孤求败',
province: '13554152623',
city: '四川省成都市双流区九江北地铁站c口旁边往前两百米',
address: '450',
},
{
name: '2790027913',
age: "2790027913",
gender: '独孤求败',
province: '13554152623',
city: '四川省成都市双流区九江北地铁站c口旁边往前两百米',
address: '450',
},
{
name: '2790027913',
age: "2790027913",
gender: '独孤求败',
province: '13554152623',
city: '四川省成都市双流区九江北地铁站c口旁边往前两百米',
address: '450',
},
],
}
},
mounted() {
clearInterval(this.timer);
},
methods: {
// body{zoom:80%;} 缩放比例
handlePrint() {
let userAgent = navigator.userAgent;
//判断是否Firefox浏览器
if (userAgent.indexOf("Firefox") > -1) {
console.log('Firefox')
printJS({
printable: 'mytable1', // 标签元素id
type: 'html',
header: '',
style: `@page {size:auto;margin-top:100px; margin-left:15px; margin-right: 15px;}
thead th {
border-top: 1px solid #000;
border-right: 1px solid #000;
border-left: 1px solid #000;
}
tbody td {
border: 1px solid #000;
}
tbody {
text-align: center;
}
table {
border-collapse: collapse;
}`,
});
}
//判断是否chorme浏览器
if (userAgent.indexOf("Chrome") > -1) {
console.log('Chrome')
printJS({
printable: 'mytable1', // 标签元素id
type: 'html',
maxWidth: 1150,
scanStyles: false,//设置为false时,库将不会处理应用于正在打印的html的样式。使用css参数时很有用。不然设置css没啥卵用
// 继承原来的所有样式
targetStyles: ["*"],
css: "@/components/print.css",
header: `<div style="display: flex;justify-content: space-between;">
<div style="font-size:26px;line-height:40px;color:#272620">瑞佰快跑调度单</div>
<div style="line-height: 40px;font-size: 16px;color:#272620">运单号:505554564645</div>
</div> <div style="line-height: 22px;">客户名称:福州恒泰联创食品有限公司(电商)</div>
<div style="line-height:22px;">发货地址:四川省成都市双流区九江街道万家社区8组220号</div>
<div style="display: flex;line-height:22px"><span style="margin-right: 20px;">现场联系:张武安(13980075384)</span><span style="margin-right: 20px;">送货方式:公路运输</span><span>运货方式:汽运</span></div>`,
documentTitle: '',
style: `@page {size:auto;margin-top:20px; margin-left:30px; margin-right: 30px;}
thead th {
border-top: 1px solid #000;
border-right: 1px solid #000;
border-left: 1px solid #000;
}
tbody td {
border: 1px solid #000;
}
tbody {
text-align: center;
}
table {
border-collapse: collapse;
}`,
});
}
//判断是否IE浏览器
if (!!window.ActiveXObject || "ActiveXObject" in window) {
console.log('IE')
printJS({
printable: 'mytable1', // 标签元素id
type: 'html',
header: '',
style: `@page {size:auto;margin-top:100px; margin-left:15px; margin-right: 15px;}
thead th {
border-top: 1px solid #000;
border-right: 1px solid #000;
border-left: 1px solid #000;
}
tbody td {
border: 1px solid #000;
}
tbody {
text-align: center;
}
table {
border-collapse: collapse;
}`,
});
}
},
handleRemove(file, fileList) {
fileList.forEach((item, index) => {
if (index == 0) {
this.from.paymen_file = item.response.data
} else {
this.from.paymen_file += ',' + item.response.data
}
})
},
handlePictureCardPreview(file) {
this.dialogImageUrl.push(file.url)
this.dialogVisible = true;
console.log(this.dialogImageUrl)
},
onsuccess(response, file, fileList) {
fileList.forEach((item, index) => {
if (index == 0) {
this.from.paymen_file = item.response.data
} else {
this.from.paymen_file += ',' + item.response.data
}
})
console.log(this.from.paymen_file)
},
submit(e) {
this.from.id = this.rowdata.id
this.$refs.from.validate((valid) => {
if (valid) {
this.from.id = this.rowdata.id
ti_payment(this.from).then(res => {
if (res.code == 200) {
this.$emit("isnew", 12);
this.$message.success(res.msg)
this.$emit("update:visible", false);
} else {
this.$message.error(res.msg)
}
}).catch(err => {
this.$message.error(err.msg)
})
}
})
},
handleClick(tab, event) {
console.log(tab, event);
},
// // 选择类型
onType(id) {
this.ruleForm.type = id;
let { money, type } = this.ruleForm;
if (money && type) return this.getCode();
},
// // // 输入框监听
inputChange() {
if (this.rowdata.ti_money != this.from.ti_solid_money && this.from.ti_solid_money !== '') {
this.isfalssf = true
} else {
this.isfalssf = false
}
},
// // // 打开popup时的回调
handleOpen() {
this.logotype = this.$store.state.userinfo.type
// this.ruleForm.order = this.rowData.order;
},
cancel() {
// 取消
this.closeDialog();
},
handone(res) {
this.from.pay_bill_file = res.data;
},
closeDialog() {
// 弹窗关闭前的回调
setTimeout(() => {
this.codeImg = "";
this.success = false;
clearInterval(this.timer);
this.$refs["ruleForm"].resetFields();
}, 1000);
this.$emit("update:visible", false);
},
},
};
</script>
<style lang="less" scoped>
.titbig {
font-size: 30px;
font-weight: 400;
}
.felx {
display: flex;
}
.spsb {
justify-content: space-between;
}
.paymoeny {
font-weight: bold;
font-size: 16px;
color: #333333;
}
.smal {
margin-left: 10px;
font-size: 12px;
}
.htmlts {
width: 100%;
height: 100%;
}
.demo-ruleForm {
.form-content {
position: relative;
.input-tip {
position: absolute;
top: 8px;
right: 130px;
font-size: 14px;
color: #333333;
}
/deep/input {
color: #009688;
}
}
.type-item {
padding: 6px 15px;
border: 1px solid #e6e6e6;
font-size: 14px;
line-height: normal;
color: #666666;
margin-right: 8px;
overflow: hidden;
}
.type-item.active {
border: 1px solid #009688;
color: #009688;
position: relative;
}
.type-item.active::after {
content: "";
display: block;
width: 15px;
height: 15px;
background: #009688;
position: absolute;
bottom: -7px;
right: -7px;
transform: rotate(45deg);
}
.code-box {
margin-top: 20px;
height: 200px;
.code-tip {
font-size: 14px;
color: #333333;
}
.code-img {
width: 160px;
height: 160px;
border: 1px solid #e6e6e6;
box-sizing: border-box;
}
}
}
.titsa {
border: 1px solid #f0f0f0;
border-bottom: none;
font-size: 12px;
color: #333333;
font-weight: bold;
line-height: 30px;
background-color: #f7f8fa;
text-indent: 10px;
}
.titlhead {
background-color: #FFF6F6;
color: #FF1A1A;
line-height: 30px;
margin: 10px 0;
}
.waip {
width: 100%;
border: 1px solid #f0f0f0;
padding: 10px;
}
.btnbgc {
background-color: #009688;
color: #fff;
}
</style>