md
This commit is contained in:
parent
984953abe1
commit
c9eb54dcec
@ -242,7 +242,7 @@ export default {
|
||||
height: 100%;
|
||||
width:100%;
|
||||
overflow: auto;
|
||||
max-height: 500px;
|
||||
// max-height: 1000px;
|
||||
}
|
||||
|
||||
.pdfview {
|
||||
|
||||
@ -3,6 +3,7 @@ import hasPermi from './permission/hasPermi'
|
||||
import dialogDrag from './dialog/drag'
|
||||
import dialogDragWidth from './dialog/dragWidth'
|
||||
import dialogDragHeight from './dialog/dragHeight'
|
||||
import dialogFullScreen from './dialog/fullScreen'
|
||||
import clipboard from './module/clipboard'
|
||||
import elSelectLoadmore from './ext/elSelectLoadmore'
|
||||
|
||||
@ -13,6 +14,8 @@ const install = function(Vue) {
|
||||
Vue.directive('dialogDrag', dialogDrag)
|
||||
Vue.directive('dialogDragWidth', dialogDragWidth)
|
||||
Vue.directive('dialogDragHeight', dialogDragHeight)
|
||||
Vue.directive('dialogFullScreen', dialogFullScreen)
|
||||
|
||||
Vue.directive('el-select-loadmore', elSelectLoadmore);
|
||||
}
|
||||
|
||||
|
||||
@ -59,6 +59,7 @@ class TanexPrinterSdk {
|
||||
recievePdfFileList = []
|
||||
sendPdfFileListCount = 0
|
||||
pdfFileReceiveMap = new Map();
|
||||
recievePdfFileTimer = null;
|
||||
|
||||
// 默认打印机
|
||||
defaultPrinter = null;
|
||||
@ -107,7 +108,10 @@ class TanexPrinterSdk {
|
||||
this.onGetPrintTplCallback.push(callback);
|
||||
}
|
||||
|
||||
// 只支持一个事件响应
|
||||
setOnBatchGetPDF(callback){
|
||||
// this.onBatchGetPDFCallback.push(callback);
|
||||
this.onBatchGetPDFCallback=[];
|
||||
this.onBatchGetPDFCallback.push(callback);
|
||||
}
|
||||
|
||||
@ -143,9 +147,26 @@ class TanexPrinterSdk {
|
||||
|
||||
|
||||
// 批量获取PDF文件
|
||||
batchGetPDF(tplName,printerName,orderList){
|
||||
batchGetPDF(tplName,printerName,orderList,callback){
|
||||
if(tplName&&printerName&&orderList&&orderList.length>0){
|
||||
this.setOnBatchGetPDF(callback);
|
||||
// 超时计算
|
||||
|
||||
this.sendBatchGetPDFCMD(tplName,printerName,orderList);
|
||||
this.recievePdfFileTimer = setTimeout(function(){
|
||||
if(this.onBatchGetPDFCallback){
|
||||
if(this.onBatchGetPDFCallback.length>0){
|
||||
this.onBatchGetPDFCallback.forEach(callback=>{
|
||||
if(callback!=null){
|
||||
callback([],false,"打印超时");
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
clearTimeout(this.recievePdfFileTimer);
|
||||
}, 60000)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -386,8 +407,23 @@ class TanexPrinterSdk {
|
||||
|
||||
innerBatchGetPDF(msg){
|
||||
console.log("innerBatchGetPDF")
|
||||
|
||||
console.log(msg)
|
||||
// 有出错,停止接收数据
|
||||
if(msg.Status!='2'){
|
||||
clearTimeout(this.recievePdfFileTimer);
|
||||
|
||||
if(this.onBatchGetPDFCallback.length>0){
|
||||
this.onBatchGetPDFCallback.forEach(callback=>{
|
||||
if(callback!=null){
|
||||
callback([],false,msg.Data);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if(this.recievePdfFileList.length<this.sendPdfFileListCount){
|
||||
if(msg){
|
||||
let ydSn=msg.Data.ydSn;
|
||||
@ -397,7 +433,7 @@ class TanexPrinterSdk {
|
||||
return;
|
||||
}
|
||||
|
||||
this.recievePdfFileList.push(msg.Data);
|
||||
this.recievePdfFileList.push(msg.Data.file);
|
||||
this.pdfFileReceiveMap[ydSn]=1;
|
||||
}
|
||||
}
|
||||
@ -408,8 +444,9 @@ class TanexPrinterSdk {
|
||||
if(this.onBatchGetPDFCallback.length>0){
|
||||
this.onBatchGetPDFCallback.forEach(callback=>{
|
||||
if(callback!=null){
|
||||
clearTimeout(this.recievePdfFileTimer);
|
||||
// 把PDF文件传给终端处理
|
||||
callback(this.recievePdfFileList);
|
||||
callback(this.recievePdfFileList,true,"获取成功");
|
||||
|
||||
// 发送完成后数据清空
|
||||
this.recievePdfFileList=[];
|
||||
|
||||
@ -168,6 +168,152 @@ export default class PdfUtils {
|
||||
// console.log(+new Date() - startTime)
|
||||
}
|
||||
|
||||
async mergePdf3(pdfFileList, fileName,isNeedDown=false) {
|
||||
let startTime = +new Date()
|
||||
let pdfBuffers = [];
|
||||
pdfFileList.map((item) => {
|
||||
let dateArray=this.base64ToBuffer(item);
|
||||
pdfBuffers.push(Buffer.from(dateArray));
|
||||
});
|
||||
const newPdf = await PDFDocument.create();
|
||||
for (let buffer of pdfBuffers) {
|
||||
const pdfDocument = await PDFDocument.load(buffer);
|
||||
const contentPages = await newPdf.copyPages(
|
||||
pdfDocument,
|
||||
pdfDocument.getPageIndices()
|
||||
);
|
||||
for (let page of contentPages) {
|
||||
newPdf.addPage(page);
|
||||
}
|
||||
}
|
||||
const uint8Array = await newPdf.save();
|
||||
let mergeBuffer = Buffer.from(uint8Array);
|
||||
|
||||
var blob=new Blob([mergeBuffer]);
|
||||
|
||||
if(isNeedDown){
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.setAttribute("download", fileName);
|
||||
if (typeof link.download === 'undefined') {
|
||||
link.setAttribute('target', '_blank')
|
||||
}
|
||||
// document.body.appendChild(link);
|
||||
// link.click();
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
}else{
|
||||
const mergedPdfBlob = new Blob([mergeBuffer], {
|
||||
type: "application/pdf",
|
||||
});
|
||||
return URL.createObjectURL(mergedPdfBlob);;
|
||||
}
|
||||
console.log(+new Date() - startTime)
|
||||
}
|
||||
downloadBase64(base64Str,fileName) {
|
||||
let result = base64Str.replace(/[\r\n]/g, "");
|
||||
let pdfBase64 = `data:application/pdf;base64,${result}`;
|
||||
let url = URL.createObjectURL(this.base64ToBlob(pdfBase64));
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.setAttribute("download", fileName);
|
||||
if (typeof link.download === 'undefined') {
|
||||
link.setAttribute('target', '_blank')
|
||||
}
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
|
||||
}
|
||||
|
||||
fileTobase64(file) {
|
||||
let reader = new FileReader();
|
||||
reader.readAsDataURL(file)
|
||||
console.log(reader)
|
||||
}
|
||||
|
||||
|
||||
base64ToFile(base64Data, filename) { // 64转file
|
||||
if (typeof base64Data != 'string') {
|
||||
// this.$toast("urlData不是字符串")
|
||||
return;
|
||||
}
|
||||
var arr = base64Data.split(',')
|
||||
var type = arr[0].match(/:(.*?);/)[1]
|
||||
var fileExt = type.split('/')[1]
|
||||
var bstr = atob(arr[1])
|
||||
var n = bstr.length
|
||||
var u8arr = new Uint8Array(n)
|
||||
while (n--) {
|
||||
u8arr[n] = bstr.charCodeAt(n);
|
||||
}
|
||||
return new File([u8arr], filename +'.' + fileExt, {
|
||||
type: type
|
||||
});
|
||||
}
|
||||
|
||||
base64ToBlob(base64Data) {
|
||||
let arr = base64Data.split(','),
|
||||
fileType = arr[0].match(/:(.*?);/)[1],
|
||||
bstr = atob(arr[1]),
|
||||
l = bstr.length,
|
||||
u8Arr = new Uint8Array(l);
|
||||
while (l--) {
|
||||
u8Arr[l] = bstr.charCodeAt(l);
|
||||
}
|
||||
return new Blob([u8Arr], {
|
||||
type: fileType
|
||||
});
|
||||
}
|
||||
|
||||
base64ToBuffer(base64Str){
|
||||
const padding = '='.repeat((4 - base64Str.length % 4) % 4);
|
||||
const base64 = (base64Str + padding).replace(/\-/g, '+').replace(/_/g, '/');
|
||||
const rawData = window.atob(base64);
|
||||
const outputArray = new Uint8Array(rawData.length);
|
||||
for (let i = 0; i < rawData.length; ++i) {
|
||||
outputArray[i] = rawData.charCodeAt(i);
|
||||
}
|
||||
return outputArray;
|
||||
}
|
||||
|
||||
|
||||
// base64ToArrayBuffer(base64Str) {
|
||||
// let arr = base64Data.split(','),
|
||||
// fileType = arr[0].match(/:(.*?);/)[1],
|
||||
// bstr = atob(result),
|
||||
// l = bstr.length,
|
||||
// u8Arr = new Uint8Array(l);
|
||||
// while (l--) {
|
||||
// u8Arr[l] = bstr.charCodeAt(l);
|
||||
// }
|
||||
// let blob = new Blob([u8Arr], {
|
||||
// type: fileType
|
||||
// });
|
||||
// let reader = new FileReader();
|
||||
// reader.onload = function(result) {
|
||||
// console.log(result);
|
||||
// }
|
||||
|
||||
// reader.readAsArrayBuffer(blob);
|
||||
// }
|
||||
|
||||
base64File(base64Str,fileName){
|
||||
var fileExt=".pdf";
|
||||
|
||||
var bstr = atob(base64Str)
|
||||
var n = bstr.length
|
||||
var u8arr = new Uint8Array(n)
|
||||
while (n--) {
|
||||
u8arr[n] = bstr.charCodeAt(n);
|
||||
}
|
||||
return new File([u8arr], fileName +'.' + fileExt, {
|
||||
type: type
|
||||
});
|
||||
}
|
||||
|
||||
getObjectURL(file) {
|
||||
let url = null;
|
||||
if (window.createObjectURL != undefined) { // basic
|
||||
|
||||
@ -444,8 +444,10 @@
|
||||
</el-dialog>
|
||||
-->
|
||||
|
||||
<el-dialog class="previe_dialog" title="打印预览" :visible.sync="openPdfView" width="80%" :close-on-click-modal="false" v-dialogDrag append-to-body>
|
||||
<PdfViewer :pdfUrl="pdfUrl" width="100%" @onPrint="handleBatchPrint" ></PdfViewer>
|
||||
<el-dialog class="previe_dialog" title="打印预览" :visible.sync="openPdfView" width="70%" :close-on-click-modal="false" v-dialogFullScreen v-dialogDrag append-to-body>
|
||||
<div style="height: 100%;width:100%">
|
||||
<PdfViewer :pdfUrl="pdfUrl" style="width:100%" @onPrint="handleBatchPrint" ></PdfViewer>
|
||||
</div>
|
||||
</el-dialog>
|
||||
|
||||
</div>
|
||||
@ -716,11 +718,32 @@ export default {
|
||||
this.tanexPrinter.setOnError(this.onPrinterError);
|
||||
this.tanexPrinter.setOnGetPrinter(this.onGetPrinter);
|
||||
this.tanexPrinter.setOnGetPrintTpl(this.onGetPrintTpl);
|
||||
this.tanexPrinter.setOnBatchGetPDF(this.onBatchGetPDF);
|
||||
// this.tanexPrinter.setOnBatchGetPDF(this.onBatchGetPDF);
|
||||
|
||||
},
|
||||
onBatchGetPDF(list){
|
||||
console.log(list);
|
||||
onBatchGetPDF(pdfList){
|
||||
console.log(pdfList);
|
||||
const pdfDataList=[...pdfList];
|
||||
console.log(pdfDataList);
|
||||
let pdfUtils=new PdfUtils({});
|
||||
// const loadingInstance = this.$loading({
|
||||
// text: '正在生成预览...'
|
||||
// })
|
||||
pdfUtils.mergePdf3(pdfDataList, (new Date()).getTime()+".pdf",false )
|
||||
.then(url=>{
|
||||
// loadingInstance.close();
|
||||
// this.pdfUrl='/static/pdfjs/dist/web/printPreview.html?file=' +encodeURIComponent(url);
|
||||
this.pdfUrl=url;
|
||||
console.log(url)
|
||||
this.openPdfView = true;
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error)
|
||||
});
|
||||
|
||||
// pdfDataList.forEach(item => {
|
||||
// pdfUtils.downloadBase64(item,(new Date()).getTime()+".pdf")
|
||||
// });
|
||||
},
|
||||
onPrinterOpen(evt){
|
||||
console.log("onPrinterOpen")
|
||||
@ -768,11 +791,37 @@ export default {
|
||||
|
||||
},
|
||||
async handlePrintPreview(){
|
||||
let that =this;
|
||||
let orderList=[];
|
||||
this.currentSelection.forEach(item=>{
|
||||
orderList.push(item.billCode);
|
||||
});
|
||||
this.tanexPrinter.batchGetPDF(this.selTplName,this.selPrinterName,orderList)
|
||||
const loadingInstance = this.$loading({
|
||||
text: '正在生成预览...'
|
||||
})
|
||||
this.tanexPrinter.batchGetPDF(this.selTplName,this.selPrinterName,orderList,function(pdfList,success,erroMsg){
|
||||
console.log(success);
|
||||
console.log(erroMsg);
|
||||
if(success){
|
||||
const pdfDataList=[...pdfList];
|
||||
console.log(pdfDataList);
|
||||
let pdfUtils=new PdfUtils({});
|
||||
pdfUtils.mergePdf3(pdfDataList, (new Date()).getTime()+".pdf",false )
|
||||
.then(url=>{
|
||||
loadingInstance.close();
|
||||
// this.pdfUrl='/static/pdfjs/dist/web/printPreview.html?file=' +encodeURIComponent(url);
|
||||
that.pdfUrl=url;
|
||||
console.log(url)
|
||||
that.openPdfView = true;
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error)
|
||||
});
|
||||
}else{
|
||||
console.log(erroMsg);
|
||||
// this.$modal.msgError("打印出错:"+erroMsg);
|
||||
}
|
||||
});
|
||||
},
|
||||
async handlePrintPreview_old(){
|
||||
let pdfUrlList=[];
|
||||
@ -796,11 +845,17 @@ export default {
|
||||
|
||||
},
|
||||
handleDownPDF(){
|
||||
// let pdfUrlList=[];
|
||||
// this.currentSelection.forEach(item=>{
|
||||
// let url=this.tanexPrinter.getPdfUrl(item.billCode,this.selTplName,this.selPrinterName);
|
||||
// pdfUrlList.push(url)
|
||||
// });
|
||||
let pdfUrlList=[];
|
||||
this.currentSelection.forEach(item=>{
|
||||
let url=this.tanexPrinter.getPdfUrl(item.billCode,this.selTplName,this.selPrinterName);
|
||||
pdfUrlList.push(url)
|
||||
});
|
||||
|
||||
let pdfUtils=new PdfUtils({});
|
||||
pdfUtils.mergePdf2(pdfUrlList, (new Date()).getTime()+".pdf" ,true);
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user