技术交流 > 前端技术 > Web打印控件之Lodop(综合表格打印)

Web打印控件之Lodop(综合表格打印)

在开发Web应用时,打印是必不可少的环节之一,例如客户的销售订单、采购订单之类的单据需要打印出来,

今天分享一款免费的Web打印控件Lodop,它由Lodop插件及CLodop服务组成,官网下载地址:http://www.lodop.net


=======================================================

下载完成后安装CLodop_Setup_for_Win32NT.exe和install_lodop64.exe(或install_lodop32.exe)后,就可以使用了。

官方基础指令:


  • PRINT_INIT(strPrintTaskName)打印初始化

  • SET_PRINT_PAGESIZE(intOrient,intPageWidth,intPageHeight,strPageName)设定纸张大小

  • ADD_PRINT_HTM(intTop,intLeft,intWidth,intHeight,strHtml)增加超文本项

  • ADD_PRINT_TEXT(intTop,intLeft,intWidth,intHeight,strContent)增加纯文本项

  • ADD_PRINT_TABLE(intTop,intLeft,intWidth,intHeight,strHtml)增加表格项

  • ADD_PRINT_SHAPE(intShapeType,intTop,intLeft,intWidth,intHeight,intLineStyle,intLineWidth,intColor)画图形

  • SET_PRINT_STYLE(strStyleName,varStyleValue)设置对象风格

  • PREVIEW打印预览

  • PRINT直接打印

  • PRINT_SETUP打印维护

  • PRINT_DESIGN打印设计


最基本的打印过程至少有初始化语句添内容语句打印语句三部分组成,例如:

//首先一个初始化语句

LODOP.PRINT_INIT("打印任务名"); 

//然后多个ADD语句及SET语句

LODOP.ADD_PRINT_TEXT(0,0,100,20,"文本内容一");

//最后一个打印(或预览、维护、设计)语句

LODOP.PRINT();


现在介绍综合表格打印的演示示例:

//CLodop服务默认占用8000端口和18000端口


//==判断是否需要CLodop(那些不支持插件的浏览器):==
function needCLodop() {
    try {
        var ua = navigator.userAgent;
        if (ua.match(/WindowssPhone/i))
            return true;
        if (ua.match(/iPhone|iPod|iPad/i))
            return true;
        if (ua.match(/Android/i))
            return true;
        if (ua.match(/EdgeD?d+/i))
            return true;

        var verTrident = ua.match(/TridentD?d+/i);
        var verIE = ua.match(/MSIED?d+/i);
        var verOPR = ua.match(/OPRD?d+/i);
        var verFF = ua.match(/FirefoxD?d+/i);
        var x64 = ua.match(/x64/i);
        if ((!verTrident) && (!verIE) && (x64))
            return true;
        else if (verFF) {
            verFF = verFF[0].match(/d+/);
            if ((verFF[0] >= 41) || (x64))
                return true;
        } else if (verOPR) {
            verOPR = verOPR[0].match(/d+/);
            if (verOPR[0] >= 32)
                return true;
        } else if ((!verTrident) && (!verIE)) {
            var verChrome = ua.match(/ChromeD?d+/i);
            if (verChrome) {
                verChrome = verChrome[0].match(/d+/);
                if (verChrome[0] >= 41)
                    return true;
            }
        }
        return false;
    } catch (err) {
        return true;
    }
}

//==加载引用CLodop的主JS,用双端口8000和18000(以防其中一个被占):==
function loadCLodop() {
    if (CLodopJsState == "loading" || CLodopJsState == "complete") return;
    CLodopJsState = "loading";
    var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
    var JS1 = document.createElement("script");
    var JS2 = document.createElement("script");
    JS1.src = "http://localhost:8000/CLodopfuncs.js?priority=1";
    JS2.src = "http://localhost:18000/CLodopfuncs.js";
    JS1.onload = JS2.onload = function () { CLodopJsState = "complete"; }
    JS1.onerror = JS2.onerror = function (evt) { CLodopJsState = "complete"; }
    head.insertBefore(JS1, head.firstChild);
    head.insertBefore(JS2, head.firstChild);
    CLodopIsLocal = !!((JS1.src + JS2.src).match(///localho|//127.0.0./i));
}

if (needCLodop()) { loadCLodop(); }//加载

//==获取LODOP对象主过程,判断是否安装、需否升级:==
function getLodop(oOBJECT, oEMBED) {

    ... ...
    var LODOP;
    try {
        var ua = navigator.userAgent;
        var isIE = !!(ua.match(/MSIE/i)) || !!(ua.match(/Trident/i));
        if (needCLodop()) {
            try {
                LODOP = getCLodop();
            } catch (err) { }
            if (!LODOP && CLodopJsState !== "complete") {
                if (CLodopJsState == "loading") alert("网页还没下载完毕,请稍等一下再操作."); else alert("没有加载CLodop的主js,请先调用loadCLodop过程.");
                return;
            }
            if (!LODOP) {
                document.body.innerHTML = strCLodopInstall_1 + (CLodopIsLocal ? strCLodopInstall_2 : "") + strCLodopInstall_3 + document.body.innerHTML;
                return;
            } else {
                if (CLODOP.CVERSION < "4.1.2.7") {
                    document.body.innerHTML = strCLodopUpdate + document.body.innerHTML;
                }
                if (oEMBED && oEMBED.parentNode)
                    oEMBED.parentNode.removeChild(oEMBED); //清理旧版无效元素
                if (oOBJECT && oOBJECT.parentNode)
                    oOBJECT.parentNode.removeChild(oOBJECT);
            }
        } else {
            var is64IE = isIE && !!(ua.match(/x64/i));
            //==如果页面有Lodop就直接使用,否则新建:==
            if (oOBJECT || oEMBED) {
                if (isIE)
                    LODOP = oOBJECT;
                else
                    LODOP = oEMBED;
            } else if (!CreatedOKLodopObject) {
                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);
                CreatedOKLodopObject = LODOP;
            } else
                LODOP = CreatedOKLodopObject;
            //==Lodop插件未安装时提示下载地址:==
            if ((!LODOP) || (!LODOP.VERSION)) {
                if (ua.indexOf('Chrome') >= 0)
                    document.body.innerHTML = strHtmChrome + document.body.innerHTML;
                if (ua.indexOf('Firefox') >= 0)
                    document.body.innerHTML = strHtmFireFox + document.body.innerHTML;
                document.body.innerHTML = (is64IE ? strHtm64_Install : strHtmInstall) + document.body.innerHTML;
                return LODOP;
            }
        }
        if (LODOP.VERSION < "6.2.2.6") {
            if (!needCLodop())
                document.body.innerHTML = (is64IE ? strHtm64_Update : strHtmUpdate) + document.body.innerHTML;
        }
        //===如下空白位置适合调用统一功能(如注册语句、语言选择等):


        //========================================
        return LODOP;
    } catch (err) {
        alert("getLodop出错:" + err);
    }
}


//组装一个要打印的html字符串

var divTest =

"<TABLE border=0 cellSpacing=0 cellPadding=0 width="100% ">" +

"<TBODY  style="font-size: 12px">" +
"<TR>" +
"<TD width="25%" height="22">发货工厂:</TD>" +
"<TD width="25%">物流商:</TD>" +
"<TD width="25%">车牌号:</TD>" +
"<TD width="25%">实收总件数:</TD>" +
"</TR>" +

... +

"</TBODY>"
"</TABLE >";


//调用LODOP打印插件

LODOP = getLodop();  //获取LODOP对象

LODOP.SET_PRINT_STYLE("FontSize", 9);  //设置字体

LODOP.PRINT_INIT("test");  //初始化

LODOP.ADD_PRINT_HTM(30, "8%", "86%", 1050, divTest);  //打印主体

LODOP.ADD_PRINT_HTM(1100, 700, 300, 100, "<font color='#000' format='#'><span tdata='pageNO'>第##页</span>/<span tdata='pageCount'>共##页</span></font>");  //页脚
LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
LODOP.SET_PRINT_STYLEA(0, "Horient", 1);    
LODOP.PREVIEW();  //插件预览


看看预览效果:

微信图片_20210910153813.png


打印之后的pdf:

微信截图_20210910155624.png


=======================================================

由于篇幅有限,本文只介绍了表格的打印,还有其他强大的功能例如公章打印、话费缴费回执单打印等 待大家去探索。



2021-09-10 15:54:59
评论
  • 评论加载中...

评论内容: