步骤1:全局引入
npm install print-js --save
步骤2:组件内引用
<template>
<div>
<el-button
type="primary"
@click="print"
>打 印</el-button
>
<Print />
</div>
</template>
import printHtml from 'print-js'
import Print from './components/Print.vue'
步骤3:创建实例方法
components: {
Print,
},
print() {
// 更改页面打印名称
document.title = '******' '.pdf'
printHtml({
printable: 'printMe', // 文档来源:pdf或图像url,html元素ID或json数据对象
type: 'html', // 可打印的类型。可用的打印选项包括:pdf,html,图像,json和raw-html。
// css: 'Print.css', // 这使我们可以传递一个或多个CSS文件URL,这些URL应该应用于要打印的html。值可以是具有单个URL的字符串或具有多个URL的数组。
scanStyles: false, // 设置为false时,库将不处理应用于正在打印的html的样式。使用css参数时很有用。
style: '@media print{@page {size:landscape}}',
showModal: true
})
}
步骤4:创建print 页面 注意:id与print方法内 printable的参数保持一致 ,因为样式类不起作用 只能在元素style属性内写
<template>
<div style="position: fixed; top: 0; left: -9999px" id="pdfDom">
<div id="printMe">
<div
v-for="unit in printUnit"
:key="unit.code"
style="padding: 10px; background: #fff !important"
>
<div v-for="(t, i) in pagenum" :key="i">
<div style="display: flex">
<div>
<table style="border-collapse: collapse;width:100%">
<thead style="
text-align: center;
margin:0;padding:0;
">
<tr>
<th colspan="1"></th>
<th
colspan="8"
style="
text-align: center;
font-size: 18px;
font-weight: bold;
font-family: STZhongsong; margin:0;padding:0;
"
>
战备储备物资调拨通知单(<span style="
font-size: 15px;
font-weight: bold;
font-family: KaiTi_GB2312;
line-height: 24px;
">{{ printData.allocationTypeName }}</span>)
</th>
<th colspan="1"></th>
</tr>
</thead>
<tbody style="font-size: 9px;font-family: SimHei">
<tr style=" text-align: left">
<td colspan="2" style="margin:0;padding:0;">
单 号:<span style="font-family: SimSun;margin-left:1px;">{{ printData.allocationNumber }}</span>
</td>
<td
colspan="3"
rowspan="2"
style="
text-align: center;
font-size: 14px;
font-weight: bold;
font-family: KaiTi_GB2312;
line-height: 24px;
margin:0;padding:0;
"
>
({{ printData.printSpecialityName?printData.printSpecialityName:printData.reserveSpecialityName }})
</td>
<td colspan="5" style="text-align: right">
<tr style="font-size: 9px; text-align: right">
<td colspan="4" style="width:191px"></td>
<td colspan="2" style="width:140px;text-align: right">
开单日期:<span style="font-family: SimSun">{{ printData.billDate }}</span>
</td>
</tr>
</td>
</tr>
<tr style="font-size: 9px; text-align: left;margin:0;padding:0;">
<td colspan="2" rowspan="2" style="margin:0;padding:0;">
<div style="
height: 28px;
overflow: hidden;
text-overflow: ellipsis;
font-size: 9px;
display:block;
line-height:15px;
width:350px;
margin:0;padding:0;
"
v-if="printData.allocationBasis.length>26"
>
<tr style="font-size: 9px; text-align: left;margin:0;padding:0;">
<td style="width:54px;margin:0;padding:0;">依据文号:</td>
<td ><span style="font-family: SimSun;margin:0;padding:0;">{{ printData.allocationBasis }}</span></td>
</tr>
</div>
<div style="
height: 28px;
overflow: hidden;
text-overflow: ellipsis;
font-size: 9px;
display:block;
line-height:28px;
width:350px;margin:0;padding:0;
"
v-else
>
<tr style="font-size: 9px; text-align: left;margin:0;padding:0;">
<td style="width:54px;margin:0;padding:0;">依据文号:</td>
<td ><span style="font-family: SimSun;margin:0;padding:0;">{{ printData.allocationBasis }}</span></td>
</tr>
</div>
</td>
<td colspan="5" style="text-align: right">
<tr style="font-size: 9px; text-align: right">
<td colspan="5" style="width:191px"></td>
<td colspan="2" style="width:140px;text-align: right">
有效期至:<span style="font-family: SimSun">{{ printData.expDate }}</span>
</td>
</tr>
</td>
</tr>
<tr style="font-size: 9px; text-align: left">
<td colspan="4" style="text-align: right"></td>
<td colspan="4" style="text-align: right">
<tr style="font-size: 9px; text-align: right">
<td colspan="4" style="width:140px"></td>
<td style="text-align: right">
运输方式:<span style="font-family: SimSun">{{ printData.freightModeName }}</span>
</td>
</tr>
</td>
</tr>
<tr style="font-size: 9px; text-align: left;margin:0;padding:0;">
<td colspan="5" style="width:500px;margin:0;padding:0;">
<tr>
<td style="width:54px;margin:0;padding:0;" >
发物单位:
</td>
<td style="width:260px;margin:0;padding:0;">
<div style="
height: 18px;
overflow: hidden;
text-overflow: ellipsis;
font-size: 9px;
display:block;
line-height:18px;margin:0;padding:0;
">
<tr>
<td>
<span style="font-family: SimSun">{{ printData.supplierOrganization }}</span>
</td>
</tr>
</div>
</td>
<td style="width:54px;margin:0;padding:0;">
发物地址:
</td>
<td colspan="3" style="width:280px;margin:0;padding:0;">
<div style="
height: 18px;
overflow: hidden;
text-overflow: ellipsis;
font-size: 9px;
display:block;
line-height:18px;
margin:0;padding:0;
">
<tr>
<td>
<span style="font-family: SimSun">{{ printData.sendCompanyAddr?printData.sendCompanyAddr:'' }}</span>
</td>
</tr>
</div>
</td>
</tr>
</td>
<td colspan="5" style="width:200px;margin:0;padding:0;">
<tr style="margin:0;padding:0;">
<td style="width:45px;margin:0;padding:0;">联系人:</td>
<td v-if="printData.sendCompanyContacts" style="margin:0;padding:0;">
<div style=" width:150px;
height: 18px;
overflow: hidden;
text-overflow: ellipsis;
font-size: 9px;
display:block;
line-height:18px;
font-family: SimSun;
margin:0;padding:0;text-align: left;
" >{{ printData.sendCompanyContacts }}</div>
</td>
<td v-else style="width:150px;margin:0;padding:0;"> </td>
<td colspan="1" style="text-align: right">发 站:</td>
<td colspan="1" style="text-align: left"><span style="font-family: SimSun">{{ printData.sendingStation }}</span></td>
</tr>
</td>
</tr>
<tr style="font-size: 9px; text-align: left;margin:0;padding:0;">
<td colspan="5" style="width:500px;margin:0;padding:0;">
<tr>
<td style="width:54px;margin:0;padding:0;" >
收物单位:
</td>
<td style="width:260px;margin:0;padding:0;">
<div style="
height: 18px;
overflow: hidden;
text-overflow: ellipsis;
font-size: 9px;
display:block;
line-height:18px;margin:0;padding:0;
">
<tr>
<td>
<span style="font-family: SimSun" >{{ printData.recieveyOrganization }}</span>
</td>
</tr>
</div>
</td>
<td style="width:54px;margin:0;padding:0;">
收物地址:
</td>
<td colspan="3" style="width:280px;margin:0;padding:0;">
<div style="
height: 18px;
overflow: hidden;
text-overflow: ellipsis;
font-size: 9px;
display:block;
line-height:18px;margin:0;padding:0;
">
<tr>
<td>
<span style="font-family: SimSun" v-if="printData.receivedCompanyAddr">{{ printData.receivedCompanyAddr }}</span>
</td>
</tr>
</div>
</td>
</tr>
</td>
<td colspan="5" style="width:200px;margin:0;padding:0;">
<tr>
<td style="width:45px;margin:0;padding:0;">
联系人:
</td>
<td v-if="printData.receivedCompanyContacts" style="margin:0;padding:0;">
<div style=" width:150px;
height: 18px;
overflow: hidden;
text-overflow: ellipsis;
font-size: 9px;
display:block;
line-height:18px;
font-family: SimSun;
margin:0;padding:0;text-align: left;
">{{ printData.receivedCompanyContacts }}</div>
</td>
<td v-else style="width:150px;margin:0;padding:0;"> </td>
<td colspan="1" style="text-align: right">到 站:</td>
<td colspan="1" style="text-align: left"><span style="font-family: SimSun">{{ printData.arrivalStation }}</span></td>
</tr>
</td>
</tr>
<tr>
<td
style="
height: 38px;
border: 1px solid black;
vertical-align: center;
text-align: center;
width: 30px;
"
>
序号
</td>
<td
style="
border: 1px solid black;
vertical-align: center;
text-align: center;
width: 340px;
"
>
物资名称
</td>
<td
style="
border: 1px solid black;
vertical-align: center;
text-align: center;
width: 80px;
"
>
物资编码
</td>
<td
style="
border: 1px solid black;
vertical-align: center;
text-align: center;
width: 120px !important;
"
>
规格型号
</td>
<td
style="
border: 1px solid black;
vertical-align: center;
text-align: center;
width: 50px !important;
"
>
计量单位
</td>
<td
style="
border: 1px solid black;
vertical-align: center;
text-align: center;
width: 50px;
"
>
储备等级
</td>
<td
style="
border: 1px solid black;
vertical-align: center;
text-align: center;
width: 80px !important;
"
>
数量
</td>
<td
style="
border: 1px solid black;
vertical-align: center;
text-align: center;
width: 100px;
"
>
金额
(元)
</td>
<td
style="
border: 1px solid black;
text-align: center;
width: 100px;
"
>
备注
</td>
</tr>
<tr v-for="(item, index) in printData.itemList" :key="index" style="font-size: 9px;font-family: SimSun">
<template
v-if="index >= pageSize * i && index < pageSize * (i + 1)"
>
<td
style="
height: 38px;
border: 1px solid black;
vertical-align: center;
text-align: center;
"
>
{{ index + 1 }}
</td>
<td style="
height: 38px;
border: 1px solid black;
"
>
<div style="
height: 38px;
overflow: hidden;
text-overflow: ellipsis;
font-size: 9px;
line-height:15px;
display:block; width:100%;
display: flex;
align-items: center;
justify-content: left;
text-align: justify;
"
v-if="item && item.reserveItemName.length>26"
>
{{ item && item.reserveItemName.slice(0,52) }}
</div>
<div style="
height: 38px;
overflow: hidden;
text-overflow: ellipsis;
font-size: 9px;
line-height:38px;
display:block; width:100%;
"
v-else
>
{{ item && item.reserveItemName }}
</div>
</td>
<td
style="
height: 38px;
border: 1px solid black;
vertical-align: center;
text-align: center;
overflow: hidden;
white-space: nowrap;
"
>
{{ item && item.businessReserveItemCode }}
</td>
<td
style="
height: 38px;
border: 1px solid black;
vertical-align: center;
text-align: center;
white-space: nowrap;
maxWidth: 160px !important;
white-space:nowrap;
overflow:hidden;
text-overflow:ellipsis;
"
>
{{ item && item.itemSam }}
</td>
<td
style="
height: 38px;
border: 1px solid black;
vertical-align: center;
text-align: center;
overflow: hidden;
white-space: nowrap;
"
>
{{ item && item.itemUnit }}
</td>
<td
style="
height: 38px;
border: 1px solid black;
vertical-align: center;
text-align: center;
overflow: hidden;
white-space: nowrap;
"
>
{{ item && item.reserveGradeName }}
</td>
<td
style="
height: 38px;
border: 1px solid black;
vertical-align: center;
text-align: right;
overflow: hidden;
white-space: nowrap;
padding-right: 10px;
"
>
{{ item && item.itemQuantity }}
</td>
<td
style="
height: 38px;
border: 1px solid black;
vertical-align: center;
text-align: right;
overflow: hidden;
white-space: nowrap;
padding-right: 10px;
"
>
{{ item && Number(item.itemAmount).toFixed(2) }}
</td>
<td
style="
height: 38px;
border: 1px solid black;
vertical-align: center;
"
>
{{ item && item.remk }}
</td>
<div style="page-break-after: always"></div>
</template>
</tr>
<tr>
<td
colspan="2"
style="
height: 38px;
border: 1px solid black;
vertical-align: center;
text-align: center;
"
>
本调拨单合计金额(元)
</td>
<td
colspan="2"
style="
height: 38px;
border: 1px solid black;
vertical-align: center;
text-align: center;
font-family: SimSun
"
>
{{ printData.totalAmount | numFormat }}
</td>
<td
colspan="3"
style="
height: 38px;
border: 1px solid black;
vertical-align: center;
text-align: center;
"
>
本页金额小计(元)
</td>
<td
colspan="2"
style="
height: 20px;
border: 1px solid black;
vertical-align: center;
text-align: center;
font-family: SimSun
"
>
{{
printData.itemList
.slice(i * pageSize, (i + 1) * pageSize)
.map((item) => item.itemAmount)
.reduce((pre, next) => pre + next, 0) | numFormat
}}
</td>
</tr>
</tbody>
</table>
<div style="display: flex; height: 38px;font-size: 9px; padding-top: 2px">
<!-- <div style="width: 400px; line-height:10px;
overflow:hidden; margin:0;padding:0;">签发单位(公章):<span style="font-family: SimSun">{{ printData.managermentOrganizationName?printData.managermentOrganizationName.repeat(40):'' }}</span>
</div> -->
<div
style="display: flex; width: 500px;"
>
<div style="text-align: right; min-width: 85px;margin:0;padding:0;">签发单位(公章):</div>
<div style="text-align: left;min-width:330px;line-height:20px;margin:0;padding:0;
overflow:hidden;font-family: SimSun">{{ printData.managermentOrganizationName?printData.managermentOrganizationName:'' }}</div>
<div style="text-align: left;min-width: 80px;">局(处)长:</div>
</div>
<div
style="display: flex; justify-content: space-around;width: 450px;"
>
<div style="text-align: center;min-width: 60px;"></div>
<div style="text-align: center;width: 140px;">承办人:</div>
<div style="text-align: center;width: 180px;white-space:nowrap;
overflow:hidden;">电话:<span style="font-family: SimSun">{{ printData.phone?printData.phone:'' }}</span></div>
</div>
</div>
</div>
<div
style="
width: 20px;
margin-top: 40vh;
font-size: 9px;
padding-left: 10px;
white-space: pre-wrap;
"
v-html="unit.value"
></div>
</div>
<div
style="font-size: 9px;text-align: center; margin-top: 10px; font-family: SimSun"
>
第{{ i + 1 }}页/共{{ pagenum }}页
</div>
<div style="page-break-after: always"></div>
</div>
</div>
</div>
</div>
</template>
<script lang="ts">
import { printingApi } from '@/api/goodsTransfer/createAllocationSheet/detail'
export default class Print extends Vue {
created() {
if (this.$route.query.id) {
this.getData()
}
}
private pageSize = 10;
private pagenum = 0;
private printData: any = [];
private getData() {//获取页面数据
printingApi(this.$route.query.id).then((res: any) => {
this.printData = JSON.parse(JSON.stringify(res.data))
if (this.printData.itemList.length === 0) {
this.pagenum = 1
} else {
this.pagenum = Math.ceil(
this.printData.itemList.length / this.pageSize
)
}
this.printData.itemList.length =
this.printData.itemList.length +
(this.pageSize - (this.printData.itemList.length % this.pageSize))
})
}
}
</script>
<style lang="scss" scoped>
</style>
网友评论