作者 xudada

进港原始,理货优化回执,添加导出EXECL,PDF功能

1 -import html2canvas from 'html2canvas'  
2 -/*import jsPDF from 'jspdf'*/  
3 -export default{  
4 - install (Vue, options) {  
5 - Vue.prototype.getPdf = function (htmlTitle,currentTime) {  
6 - var element = document.getElementById("file");  
7 - html2canvas(element, {  
8 - logging:false  
9 - }).then(function(canvas) {  
10 - var pdf = new jsPDF('p', 'mm', 'a4'); //A4纸,纵向  
11 - var ctx = canvas.getContext('2d'),  
12 - a4w = 190, a4h = 277, //A4大小,210mm x 297mm,四边各保留10mm的边距,显示区域190x277  
13 - imgHeight = Math.floor(a4h * canvas.width / a4w), //按A4显示比例换算一页图像的像素高度  
14 - renderedHeight = 0;  
15 -  
16 - while(renderedHeight < canvas.height) {  
17 - var page = document.createElement("canvas");  
18 - page.width = canvas.width;  
19 - page.height = Math.min(imgHeight, canvas.height - renderedHeight);//可能内容不足一页  
20 -  
21 - //用getImageData剪裁指定区域,并画到前面创建的canvas对象中  
22 - page.getContext('2d').putImageData(ctx.getImageData(0, renderedHeight, canvas.width, Math.min(imgHeight, canvas.height - renderedHeight)), 0, 0);  
23 - pdf.addImage(page.toDataURL('image/jpeg', 1.0), 'JPEG', 10, 10, a4w, Math.min(a4h, a4w * page.height / page.width)); //添加图像到页面,保留10mm边距  
24 -  
25 - renderedHeight += imgHeight;  
26 - if(renderedHeight < canvas.height)  
27 - pdf.addPage();//如果后面还有内容,添加一个空页 1 +//不使用JQuery版的
  2 +
  3 +import html2canvas from 'html2canvas';
  4 +import JsPDF from 'jspdf';
  5 +
  6 +/**
  7 + * @param ele 要生成 pdf 的DOM元素(容器)
  8 + * @param padfName PDF文件生成后的文件名字
  9 + * */
  10 +
  11 +function downloadPDF(ele, pdfName){
  12 +
  13 + let eleW = ele.offsetWidth;// 获得该容器的宽
  14 + let eleH = ele.offsetHeight;// 获得该容器的高
  15 +
  16 +
  17 + let eleOffsetTop = ele.offsetTop; // 获得该容器到文档顶部的距离
  18 + let eleOffsetLeft = ele.offsetLeft; // 获得该容器到文档最左的距离
  19 +
  20 + var canvas = document.createElement("canvas");
  21 + var abs = 0;
  22 +
  23 + let win_in = document.documentElement.clientWidth || document.body.clientWidth; // 获得当前可视窗口的宽度(不包含滚动条)
  24 + let win_out = window.innerWidth; // 获得当前窗口的宽度(包含滚动条)
  25 +
  26 + if(win_out>win_in){
  27 + // abs = (win_o - win_i)/2; // 获得滚动条长度的一半
  28 + abs = (win_out - win_in)/2; // 获得滚动条宽度的一半
  29 + // console.log(a, '新abs');
28 } 30 }
29 31
30 - pdf.save(htmlTitle);  
31 - }); 32 + canvas.width = eleW * 2; // 将画布宽&&高放大两倍
  33 + canvas.height = eleH * 2;
  34 +
  35 +
  36 +
  37 +
  38 + var context = canvas.getContext("2d");
  39 +
  40 + context.scale(2, 2);
  41 +
  42 + context.translate(-eleOffsetLeft -abs, -eleOffsetTop);
  43 + // 这里默认横向没有滚动条的情况,因为offset.left(),有无滚动条的时候存在差值,因此
  44 + // translate的时候,要把这个差值去掉
  45 +
  46 + // html2canvas(element).then( (canvas)=>{ //报错
  47 + // html2canvas(element[0]).then( (canvas)=>{
  48 + html2canvas( ele, {
  49 + dpi: 300,
  50 + // allowTaint: true, //允许 canvas 污染, allowTaint参数要去掉,否则是无法通过toDataURL导出canvas数据的
  51 + useCORS:true //允许canvas画布内 可以跨域请求外部链接图片, 允许跨域请求。
  52 + } ).then( (canvas)=>{
  53 +
  54 + var contentWidth = canvas.width;
  55 + var contentHeight = canvas.height;
  56 + //一页pdf显示html页面生成的canvas高度;
  57 + var pageHeight = contentWidth / 592.28 * 841.89;
  58 + //未生成pdf的html页面高度
  59 + var leftHeight = contentHeight;
  60 + //页面偏移
  61 + var position = 0;
  62 + //a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高
  63 + var imgWidth = 595.28;
  64 + var imgHeight = 595.28/contentWidth * contentHeight;
  65 +
  66 + var pageData = canvas.toDataURL('image/jpeg', 1.0);
  67 +
  68 +
  69 +
  70 + var pdf = new JsPDF('', 'pt', 'a4');
  71 +
  72 + //有两个高度需要区分,一个是html页面的实际高度,和生成pdf的页面高度(841.89)
  73 + //当内容未超过pdf一页显示的范围,无需分页
  74 + if (leftHeight < pageHeight) {
  75 + //在pdf.addImage(pageData, 'JPEG', 左,上,宽度,高度)设置在pdf中显示;
  76 + pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);
  77 + // pdf.addImage(pageData, 'JPEG', 20, 40, imgWidth, imgHeight);
  78 + } else { // 分页
  79 + while(leftHeight > 0) {
  80 + pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight);
  81 + leftHeight -= pageHeight;
  82 + position -= 841.89;
  83 + //避免添加空白页
  84 + if(leftHeight > 0) {
  85 + pdf.addPage();
32 } 86 }
33 } 87 }
  88 + }
  89 +
  90 + //可动态生成
  91 + pdf.save(pdfName);
  92 + })
  93 +
  94 +
  95 +}
  96 +
  97 +
  98 +export default {
  99 + downloadPDF
34 } 100 }
@@ -102,7 +102,7 @@ @@ -102,7 +102,7 @@
102 flightno: '', 102 flightno: '',
103 flightdate:'', 103 flightdate:'',
104 originstation:'', 104 originstation:'',
105 - destinationstation:'' 105 + destinationstation:'',
106 }, 106 },
107 options: [], 107 options: [],
108 airportid:'', 108 airportid:'',
@@ -708,7 +708,7 @@ @@ -708,7 +708,7 @@
708 import{addResponse,selectResponseList} from "../../api/InResponse"; 708 import{addResponse,selectResponseList} from "../../api/InResponse";
709 import FileSaver from "file-saver"; 709 import FileSaver from "file-saver";
710 import XLSX from "xlsx"; 710 import XLSX from "xlsx";
711 - /*import htmlToPdf from "../../api/htmlToPdf"*/ 711 + import htmlToPdf from "../../api/htmlToPdf"
712 export default { 712 export default {
713 data() { 713 data() {
714 /*初始数据*/ 714 /*初始数据*/
@@ -877,11 +877,12 @@ @@ -877,11 +877,12 @@
877 methods: { 877 methods: {
878 //导出PDF,EXCEL文件 878 //导出PDF,EXCEL文件
879 handleCommand(command) { 879 handleCommand(command) {
880 - if(command='EXCEL'){ 880 + if(command=='EXCEL'){
881 this.$message('正在为您下载' + command+'文件'); 881 this.$message('正在为您下载' + command+'文件');
882 this.downLoad(); 882 this.downLoad();
883 - }else if(command='PDF'){ 883 + }else{
884 this.htmlTitle=this.defaultQuery.flightno+this.defaultQuery.flightdate; 884 this.htmlTitle=this.defaultQuery.flightno+this.defaultQuery.flightdate;
  885 + htmlToPdf.downloadPDF(document.querySelector('#pdfDom'), this.htmlTitle)
885 this.$message('正在为您下载' + command+'文件'); 886 this.$message('正在为您下载' + command+'文件');
886 //this.getPdf(); 887 //this.getPdf();
887 } 888 }
@@ -436,7 +436,7 @@ @@ -436,7 +436,7 @@
436 flightdate:undefined, 436 flightdate:undefined,
437 originstation:undefined, 437 originstation:undefined,
438 destinationstation:undefined, 438 destinationstation:undefined,
439 - awba:undefined 439 + awba:undefined,
440 }, 440 },
441 tableData: [], 441 tableData: [],
442 tableData2: [], 442 tableData2: [],
@@ -690,6 +690,7 @@ @@ -690,6 +690,7 @@
690 this.dialogStatus='update'; 690 this.dialogStatus='update';
691 this.FenStatus='ediAwbh'; 691 this.FenStatus='ediAwbh';
692 this.ruleForm=row; 692 this.ruleForm=row;
  693 + this.ruleForm.flightno=row.carrier+row.flightno;
693 }, 694 },
694 //编辑分单 695 //编辑分单
695 handleFen(index,row){ 696 handleFen(index,row){
@@ -716,6 +717,7 @@ @@ -716,6 +717,7 @@
716 //获取进港理货列表 717 //获取进港理货列表
717 getMt5201List(){ 718 getMt5201List(){
718 this.tableLoading=true; 719 this.tableLoading=true;
  720 + console.log(this.defaultQuery)
719 getMt5201List(this.defaultQuery).then(res =>{ 721 getMt5201List(this.defaultQuery).then(res =>{
720 this.sumNmmsCount=0; 722 this.sumNmmsCount=0;
721 this.sumNmmsPrice=0; 723 this.sumNmmsPrice=0;
@@ -67,9 +67,9 @@ @@ -67,9 +67,9 @@
67 <el-col :span="8" style="margin-right: 0px"> 67 <el-col :span="8" style="margin-right: 0px">
68 <el-button type="primary" v-on:click="QueryData">查询</el-button> 68 <el-button type="primary" v-on:click="QueryData">查询</el-button>
69 </el-col> 69 </el-col>
70 - <el-col :span="16" style="margin-right: 0px"> 70 + <!--<el-col :span="16" style="margin-right: 0px">
71 <el-button type="primary">批量修改状态</el-button> 71 <el-button type="primary">批量修改状态</el-button>
72 - </el-col> 72 + </el-col>-->
73 </el-col> 73 </el-col>
74 </el-row> 74 </el-row>
75 <!--查询结果样式--> 75 <!--查询结果样式-->