From ff00d1d4854d435cb083abb327d58bd82a7af243 Mon Sep 17 00:00:00 2001 From: linfso Date: Wed, 5 Jun 2024 07:16:31 +0800 Subject: [PATCH] md --- src/components/PdfViewer/viewer.vue | 5 +- src/utils/pdfUtils.js | 80 ++++++------------- .../emis/EmisBaseTools/TanexPrinterPanel.vue | 73 +++++++++++++++-- .../emisCustomerAddress/recipientAddress.vue | 10 +-- .../emisCustomerAddress/senderAddress.vue | 8 +- .../emis/emisWaybill/waybillIsPrinted.vue | 2 +- src/views/emis/emisWaybill/waybillRecord.vue | 6 +- 7 files changed, 108 insertions(+), 76 deletions(-) diff --git a/src/components/PdfViewer/viewer.vue b/src/components/PdfViewer/viewer.vue index 650150b3..822d7215 100644 --- a/src/components/PdfViewer/viewer.vue +++ b/src/components/PdfViewer/viewer.vue @@ -62,6 +62,7 @@ * 但他会下载最新的2.16版本,这个版本和原来的2.5结构目录不一样, * 一定要使用4.2的话就手动装一下pdfjs-dist的2.5.207 ==> npm i pdfjs-dist@2.5.207 */ +import printJS from 'print-js' import pdf from 'vue-pdf' import PdfUtils from '@/utils/pdfUtils.js'; export default { @@ -376,7 +377,9 @@ export default { // } // }); // } - this.$refs.pdf.print(); + + printJS(this.loadPdfUrl); + // this.$refs.pdf.print(); // window.print(); }else{ this.$emit("onPrint"); diff --git a/src/utils/pdfUtils.js b/src/utils/pdfUtils.js index fdbcaccc..7830d8d1 100644 --- a/src/utils/pdfUtils.js +++ b/src/utils/pdfUtils.js @@ -18,64 +18,9 @@ import * as pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry'; */ export default class PdfUtils { constructor() { - // { blob, docTitle, isAddWatermark = false, watermark: { type = 'text', text = 'WFT', image } } const _self = this - // if(!blob) { - // return console.error('[PDF Blob Is a required parameter]') - // } - // if(!isAddWatermark) { // 不添加水印 - // _self.preView(blob, docTitle) - // } else { - // let bytes,imageType - // if(type == 'image') { - // if(!image) { - // return console.error('["image" Is a required parameter]') - // } - // bytes = image.bytes - // imageType = image.imageType - // } - // const map = { - // 'text': _self.addTextWatermark.bind(_self), - // 'image': _self.addImageWatermark.bind(_self), - // 'canvas': _self.addCanvasWatermark.bind(_self) - // } - // blob.arrayBuffer().then(async buffer => { - // const existingPdfBytes = buffer - // const pdfDoc = await PDFDocument.load(existingPdfBytes) - // let params - // if(type == 'text') params = { pdfDoc, text, docTitle } - // if(type == 'image') params = { pdfDoc, bytes, imageType, docTitle } - // if(type == 'canvas') params = { pdfDoc, text, docTitle } - // map[type](params) - // }).catch(e => console.error('[Preview Pdf Error]:', e)) - // } } - /** - * 通过模版生成A4纸面单 - * @param {*} templatePath - * @param {*} outputPath - * @param {*} data - */ -// const { PDFDocument, StandardFonts } = require('pdf-lib'); -// const fs = require('fs'); -// const fontkit = require('@pdf-lib/fontkit') -// let pdfDoc -// let font -// 使用示例 -// const templatePath = 'template.pdf'; -// const outputPath = 'output.pdf'; -// const data = { -// 'key': 'value支持中文参数', -// }; - -// fillPdfForm(templatePath, outputPath, data) -// .then(() => { -// console.log('PDF生成成功'); -// }) -// .catch((error) => { -// console.log('PDF生成失败:', error); -// }); async fillPdfForm(templatePath, outputPath, data) { const templateBytes = await fs.promises.readFile(templatePath); @@ -98,6 +43,31 @@ export default class PdfUtils { await fs.promises.writeFile(outputPath, modifiedPdfBytes); } + async getPdfByTpl(tplBase64Data, data) { + const templateBytes = await fs.promises.readFile(templatePath); + pdfDoc = await PDFDocument.load(templateBytes); + pdfDoc.registerFontkit(fontkit); + font = await pdfDoc.embedFont(fontBytes); + const form = pdfDoc.getForm(); + + for (const fieldKey in data) { + const field = form.getTextField(fieldKey); + if (field) { + field.setText(data[fieldKey]); + field.updateAppearances(font) + } + } + + const modifiedPdfBytes = await pdfDoc.save(); + + await fs.promises.writeFile(outputPath, modifiedPdfBytes); + } + + + + + + // 添加 Text 水印 async addTextWatermark({ pdfDoc, text, docTitle }) { // console.log(StandardFonts, 'StandardFonts-->>') // 字体 diff --git a/src/views/emis/EmisBaseTools/TanexPrinterPanel.vue b/src/views/emis/EmisBaseTools/TanexPrinterPanel.vue index 09518d43..7cf282ac 100644 --- a/src/views/emis/EmisBaseTools/TanexPrinterPanel.vue +++ b/src/views/emis/EmisBaseTools/TanexPrinterPanel.vue @@ -138,6 +138,9 @@ selPrinterName:null, currentSelection:[], + + pdfUtils:new PdfUtils({}) + }; }, watch: { @@ -358,8 +361,9 @@ } else if(code == 200){ console.log("===获取到数据====>"); - const pdfData=deepClone(data); - loadingInstance.close(); + loadingInstance.close(); + + const pdfData=deepClone(data); let pdfInfo={ "billCode":pdfData.ydSn, "pdfUrl":null, @@ -368,7 +372,6 @@ that.pdfList.push(pdfInfo); - console.log(that.pdfList); if(!that.openPdfView && that.showCounter==0){ that.openPdfView = true; @@ -432,20 +435,76 @@ } else if(code == 200){ + if(!that.isInitA4){ const pdfData=deepClone(data); loadingInstance.close(); - let pdfUtils=new PdfUtils({}); - pdfUtils.getBlobUrl(pdfData.file, pdfData.ydSn+".pdf",true ) + that.pdfUtils.getBlobUrl(pdfData.file, pdfData.ydSn+".pdf",true ) + }else{ + + const pdfData=deepClone(data); + let pdfInfo={ + "billCode":pdfData.ydSn, + "pdfUrl":null, + "pdfBase64":pdfData.file + } + that.pdfList.push(pdfInfo); + + } } else if(code == 800){ - - // 全部获取完成 // console.log("===全部获取完成====>"); + // A4下载 + if(that.isInitA4){ + that.downA4PDF(); + } + } }); }, + downA4PDF(){ + let cacheA4FileList=[]; + const newList = Object.assign([], this.pdfList); + if(newList&&newList.length>4){ + let spliteArr = (array = [], subGroupLength = 0) => { + let index = 0; + const newArray = []; + while (index < array.length) { + newArray.push(array.slice(index, index += subGroupLength)); + } + return newArray; + } + const subPdfList=spliteArr(newList, 4); + for (let i=0; i < subPdfList.length;i++){ + let fileItem={ + "billCode":"page-"+(i+1), + "pdfUrl":null, + "childreen":subPdfList[i] + } + cacheA4FileList.push(fileItem); + } + }else{ + let fileItem={ + "billCode":"page-"+1, + "pdfUrl":null, + "childreen":newList + } + cacheA4FileList.push(fileItem); + } + + // 下载 + for(let i=0;i - + @@ -118,7 +118,7 @@ - + @@ -128,12 +128,12 @@ - - + + - + diff --git a/src/views/emis/emisCustomerAddress/senderAddress.vue b/src/views/emis/emisCustomerAddress/senderAddress.vue index b198ad07..29fd778e 100644 --- a/src/views/emis/emisCustomerAddress/senderAddress.vue +++ b/src/views/emis/emisCustomerAddress/senderAddress.vue @@ -109,7 +109,7 @@ --> - + @@ -120,7 +120,7 @@ - + @@ -130,12 +130,12 @@ - + - + diff --git a/src/views/emis/emisWaybill/waybillIsPrinted.vue b/src/views/emis/emisWaybill/waybillIsPrinted.vue index 41383195..b685170c 100644 --- a/src/views/emis/emisWaybill/waybillIsPrinted.vue +++ b/src/views/emis/emisWaybill/waybillIsPrinted.vue @@ -257,7 +257,7 @@ - + diff --git a/src/views/emis/emisWaybill/waybillRecord.vue b/src/views/emis/emisWaybill/waybillRecord.vue index 7e8c34bb..a4ad320e 100644 --- a/src/views/emis/emisWaybill/waybillRecord.vue +++ b/src/views/emis/emisWaybill/waybillRecord.vue @@ -171,7 +171,7 @@
- +
@@ -1534,7 +1534,7 @@ methods: { this.form.sender.country=value.country; this.form.sender.province=value.province; this.form.sender.city=value.city; - this.form.sender.county=value.district; + this.form.sender.county=value.county; // this.form.receiveTown=value.town; this.form.sender.address=value.address; this.form.sender.company=value.company; @@ -1545,7 +1545,7 @@ methods: { this.form.reciever.country=value.country; this.form.reciever.province=value.province; this.form.reciever.city=value.city; - this.form.reciever.county=value.district; + this.form.reciever.county=value.county; // this.form.receiveTown=value.town; this.form.reciever.address=value.address; this.form.reciever.company=value.company;