lodop插件

首先需要下载插件,下边是插件官网:
http://www.lodop.net/index.html

第一种:ADD_PRINT_HTML特点:切行切字,不补表格线,类似截图,把内容当图截了

// 页面引入 LodopFuncs文件在下边
import { getLodop } from "@/utils/LodopFuncs";

// 使用
let newstr = document.getElementById("print").innerHTML;
let LODOP = getLodop();
LODOP.PRINT_INIT("打印任务"); //首先一个初始化语句
LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "");
// 设置内容
LODOP.ADD_PRINT_HTML(5, 5, 756, 1086, newstr);
// 设置内容比例
LODOP.SET_PRINT_MODE("PRINT_PAGE_PERCENT", "Width:100%;Height:85%");
LODOP.SET_PRINT_STYLEA(0, "HtmWaitMilSecs", 1000);
// LODOP.PRINT_DESIGN(); // 调试 - 可以调试样式
// LODOP.PREVIEW(); // 预览
LODOP.PRINT(); //打印


第二种:ADD_PRINT_TABLE 特点:不切行,只能输出一个table,嵌套复杂等表格不行

// 页面引入 LodopFuncs文件在下边
import { getLodop } from "@/utils/LodopFuncs";

var detail = this.list // 表格数据
var resHtml = '<style> table,td,th {font-family: SimSun;border: 1px solid black;border-style: solid;border-collapse: collapse;}table{width:100%;font-size:12px}</style><table border="1">';
resHtml += '<tr>';
resHtml += '<td width="5%">序号</td>';
resHtml += '<td width="30%">规格</td>';
resHtml += '<td width="25%">型号</td>';
resHtml += '<td width="20%">单位</td>';
resHtml += '<td width="20%">数量</td>';
resHtml += '</tr>';
for(var i =0, len = detail.length; i < len; i++) {
   resHtml += '<tr>';
   resHtml += '<td style="text-align: center">'+(i+1)+'</td>';
   resHtml += '<td>'+detail[i].spec+'</td>';
   resHtml += '<td>'+detail[i].model+'</td>';
   resHtml += '<td>'+detail[i].goodsUnit+'</td>';
   resHtml += '<td>'+detail[i].quantity+'</td>'
}
resHtml += '</tr>';
resHtml += '</table>';
// 标题
var rTitle = '<style> table{width:100%;font-size:14px;font-family: SimSun;}</style>';
rTitle += '<table border="0">';
rTitle += '<tr>';
rTitle += '<td colspan="6" style="text-align: center">江*******有限公司</td>'
rTitle += '</tr>';
rTitle += '</table>';
// 表格上头的数据
var form = this.form
var rHead = '<style> table{width:100%;font-size:12px;font-family: SimSun;}</style>';
rHead += '<table border="0">';
rHead += '<tr>';
rHead += '<td width="8%">机构:</td>';
rHead += '<td width="25%">'+form.organization+'</td>';
rHead += '<td width="8%">仓库:</td>';
rHead += '<td width="28%">'+form.warehouse+'</td>';
rHead += '<td width="6%">单据:</td>';
rHead += '<td width="25%">'+form.code+'</td>';
rHead += '</tr>';
rHead += '<tr>';
rHead += '<td>单据日期:</td>';
rHead += '<td>'+form.gmtCreated+'</td>';
rHead += '<td>打印日期:</td>';
rHead += '<td>'+form.gmtCreated+'</td>';
rHead += '<td></td>';
rHead += '<td></td>';
rHead += '</tr>';
rHead += '</table>';
let LODOP = getLodop();
LODOP.PRINT_INITA(0,0,1200,600,"");
LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE",1);
LODOP.ADD_PRINT_TABLE(5, 5, 745, 1086, rTitle);
LODOP.ADD_PRINT_TABLE(45, 5, 745, 1086, rHead);
LODOP.ADD_PRINT_TABLE(125 ,5, 745, 1086, resHtml); // (intTop,intLeft,intWidth,intHeight,strHtml)
// LODOP.PREVIEW(); // 预览
LODOP.PRINT(); // 打印

第二种方法,表格数据多的时候会和下边的内容重合,lodop有一个缩放的方法,但是效果不是很好
每个方法都有各自的优缺点,按照自己的需求来选择吧!

LodopFuncs.js

/* eslint-disable */
var CreatedOKLodop7766=null;
//====判断是否需要安装CLodop云打印服务器:====
export function needCLodop(){
  try{
    var ua=navigator.userAgent;
    if (ua.match(/Windows\sPhone/i) !=null) return true;
    if (ua.match(/iPhone|iPod/i) != null) return true;
    if (ua.match(/Android/i) != null) return true;
    if (ua.match(/Edge\D?\d+/i) != null) return true;

    var verTrident=ua.match(/Trident\D?\d+/i);
    var verIE=ua.match(/MSIE\D?\d+/i);
    var verOPR=ua.match(/OPR\D?\d+/i);
    var verFF=ua.match(/Firefox\D?\d+/i);
    var x64=ua.match(/x64/i);
    if ((verTrident==null)&&(verIE==null)&&(x64!==null))
      return true; else
    if ( verFF !== null) {
      verFF = verFF[0].match(/\d+/);
      if ((verFF[0]>= 42)||(x64!==null)) return true;
    } else
    if ( verOPR !== null) {
      verOPR = verOPR[0].match(/\d+/);
      if ( verOPR[0] >= 32 ) return true;
    } else
    if ((verTrident==null)&&(verIE==null)) {
      var verChrome=ua.match(/Chrome\D?\d+/i);
      if ( verChrome !== null ) {
        verChrome = verChrome[0].match(/\d+/);
        if (verChrome[0]>=42) return true;
      };
    };
    return false;
  } catch(err) {return true;};
};

//====页面引用CLodop云打印必须的JS文件:====
if (needCLodop()) {
  var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
  var oscript = document.createElement("script");
  oscript.src ="http://localhost:8000/CLodopfuncs.js?priority=1";
  head.insertBefore( oscript,head.firstChild );

  //引用双端口(8000和18000)避免其中某个被占用:
  oscript = document.createElement("script");
  oscript.src ="http://localhost:18000/CLodopfuncs.js?priority=0";
  head.insertBefore( oscript,head.firstChild );
};

//====获取LODOP对象的主过程:====
export function getLodop(oOBJECT,oEMBED){
  var strHtmInstall="<br><font color='#FF00FF'>打印控件未安装!点击这里<a href='install_lodop32.exe' target='_self'>执行安装</a>,安装后请刷新页面或重新进入。</font>";
  var strHtmUpdate="<br><font color='#FF00FF'>打印控件需要升级!点击这里<a href='install_lodop32.exe' target='_self'>执行升级</a>,升级后请重新进入。</font>";
  var strHtm64_Install="<br><font color='#FF00FF'>打印控件未安装!点击这里<a href='install_lodop64.exe' target='_self'>执行安装</a>,安装后请刷新页面或重新进入。</font>";
  var strHtm64_Update="<br><font color='#FF00FF'>打印控件需要升级!点击这里<a href='install_lodop64.exe' target='_self'>执行升级</a>,升级后请重新进入。</font>";
  var strHtmFireFox="<br><br><font color='#FF00FF'>(注意:如曾安装过Lodop旧版附件npActiveXPLugin,请在【工具】->【附加组件】->【扩展】中先卸它)</font>";
  var strHtmChrome="<br><br><font color='#FF00FF'>(如果此前正常,仅因浏览器升级或重安装而出问题,需重新执行以上安装)</font>";
  var strCLodopInstall="<br><font color='#FF00FF'>CLodop云打印服务(localhost本地)未安装启动!点击这里<a href='http://www.c-lodop.com/download/CLodop_Setup_for_Win32NT_https_3.008Extend.zip' target='_self'>执行安装</a>,安装后请刷新页面。</font>";
  var strCLodopUpdate="<br><font color='#FF00FF'>CLodop云打印服务需升级!点击这里<a href='CLodop_Setup_for_Win32NT.exe' target='_self'>执行升级</a>,升级后请刷新页面。</font>";
  var LODOP;
  try{
    var isIE = (navigator.userAgent.indexOf('MSIE')>=0) || (navigator.userAgent.indexOf('Trident')>=0);
    if (needCLodop()) {
      try{ LODOP=getCLodop();} catch(err) {};
      if (!LODOP && document.readyState!=="complete") {alert("C-Lodop没准备好,请稍后再试!"); return;};
      if (!LODOP) {
        // if (isIE) document.write(strCLodopInstall); else
          // document.documentElement.innerHTML=strCLodopInstall+document.documentElement.innerHTML;
        // return;
      } else {

        if (CLODOP.CVERSION<"3.0.0.2") {
          if (isIE) document.write(strCLodopUpdate); else
            document.documentElement.innerHTML=strCLodopUpdate+document.documentElement.innerHTML;
        };
        if (oEMBED && oEMBED.parentNode) oEMBED.parentNode.removeChild(oEMBED);
        if (oOBJECT && oOBJECT.parentNode) oOBJECT.parentNode.removeChild(oOBJECT);
      };
    } else {
      var is64IE  = isIE && (navigator.userAgent.indexOf('x64')>=0);
      //=====如果页面有Lodop就直接使用,没有则新建:==========
      if (oOBJECT!=undefined || oEMBED!=undefined) {
        if (isIE) LODOP=oOBJECT; else  LODOP=oEMBED;
      } else if (CreatedOKLodop7766==null){
        LODOP=document.createElement("object");
        LODOP.setAttribute("width",0);
        LODOP.setAttribute("height",0);
        LODOP.setAttribute("style","position:absolute;left:0px;top:-100px;width:0px;height:0px;");
        if (isIE) LODOP.setAttribute("classid","clsid:2105C259-1E0C-4534-8141-A753534CB4CA");
        else LODOP.setAttribute("type","application/x-print-lodop");
        document.documentElement.appendChild(LODOP);
        CreatedOKLodop7766=LODOP;
      } else LODOP=CreatedOKLodop7766;
      //=====Lodop插件未安装时提示下载地址:==========
      if ((LODOP==null)||(typeof(LODOP.VERSION)=="undefined")) {
        if (navigator.userAgent.indexOf('Chrome')>=0)
          document.documentElement.innerHTML=strHtmChrome+document.documentElement.innerHTML;
        if (navigator.userAgent.indexOf('Firefox')>=0)
          document.documentElement.innerHTML=strHtmFireFox+document.documentElement.innerHTML;
        if (is64IE) document.write(strHtm64_Install); else
        if (isIE)   document.write(strHtmInstall);    else
          document.documentElement.innerHTML=strHtmInstall+document.documentElement.innerHTML;
        return LODOP;
      };
    };
    if (LODOP.VERSION<"6.0") {
      if (!needCLodop()){
        if (is64IE) document.write(strHtm64_Update); else
        if (isIE) document.write(strHtmUpdate); else
          document.documentElement.innerHTML=strHtmUpdate+document.documentElement.innerHTML;
      };
      return LODOP;
    };
    //===如下空白位置适合调用统一功能(如注册语句、语言选择等):===
    //===========================================================
    return LODOP;
  } catch(err) {
    console.log("getLodop出错:"+err)
  };
};
export default getLodop;

我一开始用的打印是window.print();但是他会有一个预览;需求是不要预览,所以我找到了lodop插件;
他是用代码控制的预览和打印;只要我不加预览,就可以点击打印直接打印想要的内容 \ ( ^ o ^) /


window.print()

举例:

<span @click="handlePrint">打印</span>

<div id="print-container">
	// 打印内容
</div>

// 引入 (js文件在下边!)
import printHtml from "@/utils/print.js";

handlePrint() {
   	// js 打印带预览
    let newstr = document.getElementById("print-container").innerHTML;
    let printData = newstr
    printHtml(printData)
},

print.js

export default function printHtml(html) {
    let style = getStyle();
    let container = getContainer(html);
   
    document.body.appendChild(style);
    document.body.appendChild(container);
   
    getLoadPromise(container).then(() => {
      window.print();
      document.body.removeChild(style);
      document.body.removeChild(container);
    });
  }
   
  // 设置打印样式
  function getStyle() {
  let styleContent = `#print-container {
      display: none;
  }
  @media print {
      body > :not(.print-container) {
          display: none;
      }
      html,
      body {
          display: block !important;
          font-size: 12px;
      }
      #print-container {
          display: block;
          font-family: '宋体';
      }
  }`;
    let style = document.createElement("style");
    style.innerHTML = styleContent;
    return style;
  }
   
//   // 清空打印内容
//   function cleanPrint() {
//     let div = document.getElementById('print-container')
//     if (!!div) {
//       document.querySelector('body').removeChild(div)
//     }
//   }
   
  // 新建DOM,将需要打印的内容填充到DOM
  function getContainer(html) {
    // cleanPrint()
    let container = document.createElement("div");
    container.setAttribute("id", "print-container");
    container.innerHTML = html;
    return container;
  }
   
  // 图片完全加载后再调用打印方法
  function getLoadPromise(dom) {
    let imgs = dom.querySelectorAll("img");
    imgs = [].slice.call(imgs);
   
    if (imgs.length === 0) {
      return Promise.resolve();
    }
   
    let finishedCount = 0;
    return new Promise(resolve => {
      function check() {
        finishedCount++;
        if (finishedCount === imgs.length) {
          resolve();
        }
      }
      imgs.forEach(img => {
        img.addEventListener("load", check);
        img.addEventListener("error", check);
      })
    });
  }

vue-easy-print

// 安装
npm install vue-easy-print --save 
// 页面引入
import vueEasyPrint from "vue-easy-print";
// 使用
<vue-easy-print ref="printRef"> </vue-easy-print>
// 打印
this.$refs.printRef.print()
Logo

前往低代码交流专区

更多推荐