[原创]Lodop打印, 以及Lodop引用css文件控制打印样式的问题.

最近在做Lodop打印功能:

思路是:  用MasterPage搭个打印页面的框架, 然后在具体的页面中填入数据, 打印的样式由母版页和CSS来控制.

困扰了一天的问题是:  在打印的JS文件中, 引用外部css文件时, 需要拼出实际的含有IP地址的CSS文件地址, 并且需要用双斜杠(//)来代替单斜杠.

代码如下:

//母版页, 注意:  url的拼接形式

<input id="hidCssRef" name="hidCssRef" type="hidden" value =‘<link href="http://<%= this.Request.Url.Authority %>//App_Themes//<%=Application["Theme"].ToString()%>//Css//PrintStyleSheet.css" rel="stylesheet" type="text/css" />‘ />

//PrintHelper.Master

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="PrintHelper.master.cs" Inherits="JN.UI.Commons.PrintHelper.PrintHelper1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="/App_Themes/<%= Application["Theme"].ToString() %>/Css/PrintStyleSheet.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.json.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.commons.js" type="text/javascript"></script>
    <asp:ContentPlaceHolder ID="headPlaceHolder" runat="server">
    </asp:ContentPlaceHolder>
    <style>
        @media print
        {
            .noprint
            {
               display:none;
            }

            .printOnly
            {
                display: block;
            }
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="page">
            <!--<input type="button" value="打印" onclick=‘<%= "printPreview(\‘"+ this.ContentPlaceHolder1.ClientID +"\‘);"%>‘ />-->
            <input type="button" value="打印" onclick=‘<%= "printPreview(\"printContent\");" %>‘ />
            <input id="hidCssRef" name="hidCssRef" type="hidden" value =‘<link href="http://<%= this.Request.Url.Authority %>//App_Themes//<%=Application["Theme"].ToString()%>//Css//PrintStyleSheet.css" rel="stylesheet" type="text/css" />‘ />

            <!--PrintStart-->
            <div id="printContent">
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                </asp:ContentPlaceHolder>
            </div>
            <!--PrintEnd-->
        </div>
    </form>
</body>
</html>

//PrintHelper.Master.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace JN.UI.Commons.PrintHelper
{
    public partial class PrintHelper1 : System.Web.UI.MasterPage
    {
        #region 全局变量
        private string _printType = HttpContext.Current.Request.QueryString.AllKeys.Contains("pType") ? HttpContext.Current.Request.QueryString["pType"] : string.Empty;
        #endregion

        #region 控件事件
        protected void Page_Init(object sender, EventArgs e)
        {
            //加载lodop的object
            HtmlGenericControl objControl = null;

            //string jsPathPatt = "~/Commons/PrintHelper/Resources/Scripts/{0}.js";
            string jsPathPatt = "/Commons/PrintHelper/Resources/Scripts/{0}.js";
            string jsPath = string.Empty;

            switch (_printType)
            {
                case "winPter":
                    goto default;
                case "webPter":
                    //jsPath = this.Page.ResolveUrl(string.Format(jsPathPatt, "WebrowserPrinter"));
                    jsPath = string.Format(jsPathPatt, "WebrowserPrinter");
                    break;
                case "lodPter":
                    //加载lodop的原始JS文件
                    string jsSrcPath = string.Format("/App_Libs/Lodop/6.203/{0}.js", "LodopFuncs");
                    HtmlGenericControl JsSrcControl = new HtmlGenericControl("script");
                    JsSrcControl.Attributes.Add("type", "text/javascript");
                    JsSrcControl.Attributes.Add("src", jsSrcPath);
                    this.headPlaceHolder.Controls.Add(JsSrcControl);
                    //lodop打印功能的JS
                    jsPath = string.Format(jsPathPatt, "LodopPrinter");
                    //lodp的object
                    objControl = new HtmlGenericControl("object");
                    objControl.Attributes.Add("id", "LODOP_OB");
                    objControl.Attributes.Add("classid", "clsid:2105C259-1E0C-4534-8141-A753534CB4CA");
                    objControl.Attributes.Add("id", "LODOP_OB");
                    objControl.Attributes.Add("width", "0");
                    objControl.Attributes.Add("height", "0");
                    //加载FF支持
                    HtmlGenericControl ffLodopSup = new HtmlGenericControl("embed");
                    ffLodopSup.Attributes.Add("id", "LODOP_EM");
                    ffLodopSup.Attributes.Add("type", "application/x-print-lodop");
                    ffLodopSup.Attributes.Add("width", "0");
                    ffLodopSup.Attributes.Add("height", "0");
                    objControl.Controls.Add(ffLodopSup);
                    break;
                default:
                    jsPath = string.Format(jsPathPatt, "WindowPrinter");
                    break;
            }
            //加载JS
            HtmlGenericControl JsControl = new HtmlGenericControl("script");
            JsControl.Attributes.Add("type", "text/javascript");
            JsControl.Attributes.Add("src",jsPath);

            this.headPlaceHolder.Controls.Add(JsControl);
            if(objControl != null)
                this.headPlaceHolder.Controls.Add(objControl);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        #endregion

        #region 初始化及绑定方法
        #endregion
    }
}

//具体的打印页面

//GodownEntryPrint.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Commons/PrintHelper/PrintHelper.Master" AutoEventWireup="true" CodeBehind="GodownEntryPrint.aspx.cs" Inherits="JN.UI.InventoryAdmin.GodownEntryPrint" %>
<%@ Register src="UserControls/UC_PrintTop.ascx" tagname="UC_PrintTop" tagprefix="uc1" %>
<%@ Register src="UserControls/UC_GodownEntryPrint.ascx" tagname="UC_GodownEntryPrint" tagprefix="uc2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="headPlaceHolder" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
   <uc1:UC_PrintTop ID="UC_PrintTop1" runat="server" />
   <uc2:UC_GodownEntryPrint ID="UC_GodownEntryPrint1" runat="server" />
</asp:Content>

//GodownEntryPrint.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JN.UI.InventoryAdmin
{
    public partial class GodownEntryPrint : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            this.UC_PrintTop1.PrintTitle = "物资入库单";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

//具体的实现代码放在用户控件中

//GodownEntryPrint.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UC_GodownEntryPrint.ascx.cs" Inherits="JN.UI.InventoryAdmin.UserControls.UC_GodownEntryPrint" %>

<div class="prnTable">
    <div class="baseInfo">
        <table>
            <tr>
                <td class="baseTitle"><span>单据编号:</span></td>
                <td class="baseContent">
                    <span><asp:Label ID="lblGENum" runat="server" Text=""></asp:Label></span></td>
                <td class="baseTitle"><span>发票号:</span></td>
                <td class="baseContent">
                    <span><asp:Label ID="lblInvoNum" runat="server" Text=""></asp:Label></span>
                </td>
                <td class="baseTitle"><span>发票金额:</span></td>
                <td class="baseContent">
                    <span><asp:Label ID="lblInvoSum" runat="server" Text=""></asp:Label></span>
                </td>
            </tr>
            <tr>
                <td class="baseTitle"><span>供应商:</span></td>
                <td class="baseContent">
                    <span><asp:Label ID="lblSuppName" runat="server" Text=""></asp:Label></span>
                </td>
                <td class="baseTitle"><span>入库日期:</span></td>
                <td class="baseContent">
                    <span><asp:Label ID="lblCreateTime" runat="server" Text=""></asp:Label></span>
                </td>
                <td class="baseTitle"><span>库存状态:</span></td>
                <td class="baseContent">
                    <span><asp:Label ID="lblInveStatus" runat="server" Text=""></asp:Label></span>
                </td>
            </tr>
        </table>
    </div>
    <div class="detailInfo">
        <asp:Repeater ID="rptGEPrn" runat="server">
            <HeaderTemplate>
                <table>
                    <thead>
                        <tr>
                            <td class="detailTitle"><span>物资编码</span></td>
                            <td class="detailTitle"><span>物资名称</span></td>
                            <td class="detailTitle"><span>规格型号</span></td>
                            <td class="detailTitle"><span>单位</span></td>
                            <td class="detailTitle"><span>账务类型</span></td>
                            <td class="detailTitle"><span>入库数量</span></td>
                            <td class="detailTitle"><span>入库单价</span></td>
                            <td class="detailTitle"><span>入库金额</span></td>
                        </tr>
                    </thead>
                    <tbody>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td class="detailContent"><span><%# Eval("T_MaterialInfo.MaterialNO")  %></span></td>
                    <td class="detailContent"><span><%# Eval("T_MaterialInfo.MaterialName")  %></span></td>
                    <td class="detailContent"><span><%# Eval("T_MaterialInfo.MaterialSpec")  %></span></td>
                    <td class="detailContent"><span><%# Eval("T_Unit.UnitName")  %></span></td>
                    <td class="detailContent"><span><%# Eval("T_WareHouse.WHName")  %></span></td>
                    <td class="detailContent"><span><%# Eval("T_ZGEdownEntry.ZGEAmount")  %></span></td>
                    <td class="detailContent"><span><%# Eval("T_ZGEdownEntry.ZGEPrice")  %></span></td>
                    <td class="detailContent"><span>
                        <asp:Label ID="lblSumPrice" runat="server" Text=‘<%# Eval("T_ZGEdownEntry.ZGESumPrice")  %>‘></asp:Label></span>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="6"></td>
                        <td class="detailTitle"><span>总合计:</span></td>
                        <td class="detailContent"><span><asp:Label ID="lblTotalSum" runat="server" Text=""></asp:Label></span></td>
                    </tr>
                </tfoot>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </div>
    <div class="footInfo">
        <table>
            <tr>
                <td class="footTitle"><span>部门主管:</span></td>
                <td class="footContent">
                    <span><asp:Label ID="lblDepPrin" runat="server" Text=""></asp:Label></span></td>
                <td class="footTitle"><span>制单:</span></td>
                <td class="footContent">
                    <span><asp:Label ID="lblDocOpter" runat="server" Text=""></asp:Label></span></td>
                <td class="footTitle"><span>入库记账:</span></td>
                <td class="footContent">
                    <span><asp:Label ID="lblAccoClk" runat="server" Text=""></asp:Label></span></td>
            </tr>
        </table>
    </div>
</div>

//GodownEntryPrint.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using JN.BLL;
using JN.Model.Entity;
using JN.GTL;
using JN.Model.Enums;
using JN.BLL.Models;

namespace JN.UI.InventoryAdmin.UserControls
{
    public partial class UC_GodownEntryPrint : System.Web.UI.UserControl
    {
        #region 全局变量
        private BLLServices _baoSrv;
        private int _geNo = HttpContext.Current.Request.QueryString.AllKeys.Contains("geno") ? int.Parse(HttpContext.Current.Request.QueryString["geno"]) : -1;
        #endregion

        #region 控件事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                InitData();
                //BindData();
            }
        }
        #endregion

        #region 绑定及初始化方法
        private void InitData()
        {
            if (this._geNo <= 0)
                throw new ArgumentException("无效的入库单编号!", "_geNo");

            this._baoSrv = new BLLServices();

            //读取入库单信息
            E_GodownEntry geInfo = this._baoSrv.GodownEntryBAO.GetGodownEntry(this._geNo);
            if(geInfo != null)
            {
                //处理入库单表头
                this.lblGENum.Text = geInfo.T_GodownEntry.GENumber;
                this.lblInvoNum.Text = geInfo.T_Invoice.InvoicenNumber;
                this.lblInvoSum.Text = geInfo.T_Invoice.InvoiceAmount.ToString();
                this.lblSuppName.Text = geInfo.T_Supplier.SupplierName;
                this.lblCreateTime.Text = GTLServices.DateTimeHelper.FormatDataStr(geInfo.T_GodownEntry.GECreateTime);
                //this.lblInveStatus.Text = Enum.GetName(typeof(GodownEntryStatus),geInfo.T_GodownEntry.GEStatus);
                this.lblInveStatus.Text = getEnumStatus((GodownEntryStatus)Enum.Parse(typeof(GodownEntryStatus),geInfo.T_GodownEntry.GEStatus.ToString()));

                //处理入库单表尾
                this.lblDocOpter.Text = (this.Session["userInfo"] as SessionUser).User.T_User.UserName;
                this.lblAccoClk.Text = geInfo.T_User.UserName;

                //处理入库单详情
                List<E_ZGEGodownEntry> detailLst = this._baoSrv.GodownEntryBAO.GetZGodownEntryList(geInfo.T_GodownEntry,null);
                this.rptGEPrn.DataSource = detailLst.Where(zg => zg.T_ZGEdownEntry.ZGENo == geInfo.T_GodownEntry.GENo).ToList();
                this.rptGEPrn.DataBind();

                //处理合计
                double total = 0.0;
                foreach(RepeaterItem ri in this.rptGEPrn.Controls)
                {
                    if(ri.ItemType == ListItemType.AlternatingItem || ri.ItemType == ListItemType.Item)
                    {
                        total += double.Parse((ri.FindControl("lblSumPrice") as Label).Text.Trim());
                    }
                    else if (ri.ItemType == ListItemType.Footer)
                    {
                        (ri.FindControl("lblTotalSum") as Label).Text = total.ToString("F2");
                    }
                }
            }
        }
        #endregion

        #region 私有方法
        private string getEnumStatus(GodownEntryStatus status)
        {
            string result = string.Empty;
            switch (status)
            {
                case GodownEntryStatus.Normal:
                    result = "正常";
                    break;
                case GodownEntryStatus.Delete:
                    result = "删除";
                    break;
                case GodownEntryStatus.CancellingStocks:
                    result = "退库";
                    break;
                default:
                    result = string.Empty;
                    break;
            }
            return result;
        }
        #endregion
    }
}

//公共的表头

//PrintTop.asxs

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UC_PrintTop.ascx.cs" Inherits="JN.UI.InventoryAdmin.UserControls.UC_PrintTop" %>

<div class="prnTitle">
    <div>
        <h1>ZXXXX公司</h1>
    </div>
    <div>
        <h3><%= PrintTitle %></h3>
    </div>
</div>

//PrintTop.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JN.UI.InventoryAdmin.UserControls
{
    public partial class UC_PrintTop : System.Web.UI.UserControl
    {
        #region 全局变量
        private const string VIEWTITLE = "_subTitle";
        #endregion

        #region 控件事件
        protected void Page_Init(object sender, EventArgs e)
        {
            if (ViewState[VIEWTITLE] != null)
                this.PrintTitle = ViewState[VIEWTITLE].ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        #endregion

        #region 属性
        /// <summary>
        /// 打印文件头副标题
        /// </summary>
        public string PrintTitle
        {
            get
            {
                if (ViewState[VIEWTITLE] != null)
                    return ViewState[VIEWTITLE].ToString();
                else
                    return null;
            }
            set
            {
                if (value != null && value.Length > 0)
                {
                    ViewState[VIEWTITLE] = value;
                }
                else
                    ViewState[VIEWTITLE] = null;
            }
        }
        #endregion
    }
}

//print.js

//以下是自己封装的JS打印、预览方法
//var LODOP = getLodop(document.getElementById(‘LODOP_OB‘), document.getElementById(‘LODOP_EM‘));
//var LODOP = getLodop();
var LODOP;  //声明为全局变量

//打印预览
function printPreview(prnContentId) {
    LODOP.PRINT_INIT("Lodop功能_预览");
    getPrintContent(prnContentId);
    LODOP.PREVIEW();
}

//打印
function print(prnContentId) {
    LODOP.PRINT_INIT("Lodop功能_打印");
    getPrintContent(prnContentId);
    LODOP.PRINT();
}

//获取打印内容
function getPrintContent(prnContentId) {
    LODOP = getLodop(document.getElementById(‘LODOP_OB‘), document.getElementById(‘LODOP_EM‘));
    //var cssRef = decodeURIComponent(document.getElementById(‘hidCssRef‘).value).replace(new RegExp(/\+/g)," ");
    var cssRef = document.getElementById(‘hidCssRef‘).value;

    //LODOP = getLodop();
    //初始化打印任务名
    //LODOP.PRINT_INIT("Lodop打印控件_打印表单");
    //打印预览设置
    var intDispMode = 2;             //预览比例: 0-适高, 1-正常大小, 2-适宽;
    var intToolMode = 0;             //工具条和按钮: 0-显示上方工具栏, 1-显示下方按钮兰, 3-两个都显示, 4-两个都不显示.
    var blDirectPrint = 0;           //打印按钮是否"直接打印": 1-是, 0-否(选取打印机)
    var intWidth = 1024;             //窗口的宽(单位:px)
    var intHeight = 680;             //窗口的高(单位:px)
    var strTitleButtonCaption = ‘‘;    //预览窗口和打印按钮的名称组合, 字符型, 用"点"分隔
    LODOP.SET_PREVIEW_WINDOW(intDispMode, intToolMode, blDirectPrint, intWidth, intHeight, strTitleButtonCaption);

    //设置打印纸张大小.
    var intOrient = 2;         //设置打印方向: 1-纵向(固定纸张), 2-横向(固定纸张), 3-纵向(宽度固定, 高度自适应), 0-方向不定(操作者自行设置).
    var intPageWidth = 0;   //纸张宽度(单位0.1mm), 如:45表示4.5mm
    var intPageHeight = 0;    //纸张高度(单位0.1mm): 固定纸张时,表示纸张高; 自适应高度时, 表示纸张底边的空白高.  1480
    var strPageName = ‘A5‘;    //纸张类型: intPageWidth为0时才有效. 可使用"CreateCustomPage"在系统内建立名为"LKodopCustomPage"的自定义纸张类型.
    LODOP.SET_PRINT_PAGESIZE(intOrient, intPageWidth, intPageHeight, strPageName);
    //设置打印风格
    //LODOP.SET_PRINT_STYLE("FontSize", 11);
    //这里设置打印内容(Top,Left,Width,Height)

    LODOP.ADD_PRINT_HTM("5%", "5%", "100%", "100%", cssRef + ‘<body>‘ + document.getElementById(prnContentId).innerHTML + ‘</body>‘);
}

//项目添加了Lodop饮用后, 把自身的JS文件的修改下, 添加上未安装控件时引用自身目录下的控件文件

//LodopFuncs.js

var CreatedOKLodop7766=null;

//====判断是否需要安装CLodop云打印服务器:====
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;
    if (ua.match(/QQBrowser/i) != null) return false;
    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 ) 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 ="/CLodopfuncs.js";
    head.insertBefore( oscript,head.firstChild );
    //让本机浏览器打印(更优先):
    oscript = document.createElement("script");
    oscript.src ="http://localhost:8000/CLodopfuncs.js?priority=1";
    head.insertBefore( oscript,head.firstChild );
    //本机浏览器的后补端口8001:
    oscript = document.createElement("script");
    oscript.src ="http://localhost:8001/CLodopfuncs.js?priority=2";
    head.insertBefore( oscript,head.firstChild );
};

//====获取LODOP对象的主过程:====
function getLodop(oOBJECT,oEMBED){
    var strHtmInstall="<br><font color=‘#FF00FF‘>打印控件未安装!点击这里<a href=‘/App_Libs/Lodop/6.203/install_lodop32.exe‘ target=‘_self‘>执行安装</a>,安装后请刷新页面或重新进入。</font>";
    var strHtmUpdate = "<br><font color=‘#FF00FF‘>打印控件需要升级!点击这里<a href=‘/App_Libs/Lodop/6.203/install_lodop32.exe‘ target=‘_self‘>执行升级</a>,升级后请重新进入。</font>";
    var strHtm64_Install = "<br><font color=‘#FF00FF‘>打印控件未安装!点击这里<a href=‘/App_Libs/Lodop/6.203/install_lodop64.exe‘ target=‘_self‘>执行安装</a>,安装后请刷新页面或重新进入。</font>";
    var strHtm64_Update = "<br><font color=‘#FF00FF‘>打印控件需要升级!点击这里<a href=‘/App_Libs/Lodop/6.203/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=‘/App_Libs/Lodop/6.203/CLodopPrint_Setup_for_Win32NT.exe‘ target=‘_self‘>执行安装</a>,安装后请刷新页面。</font>";
    var strCLodopUpdate = "<br><font color=‘#FF00FF‘>CLodop云打印服务需升级!点击这里<a href=‘/App_Libs/Lodop/6.203/CLodopPrint_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<"2.0.4.7") {
            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.2.0.3") {
            if (needCLodop())
            document.documentElement.innerHTML=strCLodopUpdate+document.documentElement.innerHTML; else
            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) {alert("getLodop出错:"+err);};
};

时间: 2024-10-11 11:19:58

[原创]Lodop打印, 以及Lodop引用css文件控制打印样式的问题.的相关文章

Lodop打印控件传入css样式、看是否传入正确样式

Lodop中可以传入页面存在的css样式,也可以是拼接后的新样式,例如本博客的其他博文:Lodop打印如何隐藏table某一列 需要打印的页面,样式不一定都是行内样式,style样式单独写在页面上,或引用的是.css文件等情况,使用Lodop打印控件进行打印超文本的时候,要注意把样式也传进去.可参考LODOP官网的样例10(或下载中心综合版里自带的样例10),如图,就是该样例的几种引用css样式的方式: 引入之后,可以通过打印设计(PRINT_DESIGN) 选中超文本打印项-右键-把查看到的超

打印函数 lodop

Lodop属性和方法详解 例子:LODOP.PRINT_INIT("打印任务名");LODOP.SET_PRINT_COPIES(2);bdhtml=window.document.body.innerHTML;var hei = $('#div1').outerHeight();string = prnhtml;LODOP.SET_PRINT_PAGESIZE (3,'6cm','2cm',2);LODOP.ADD_PRINT_HTM(0,0,227,hei,string);LODO

利用lodop打印控件轻松实现批量打印

最近在做一个打印程序,要实现批量打印功能,在网上找了很多天,也在tp官网咨询大牛们,对大家的的提议我一一进行了研究,总结如下:要实现批量打印可以有两个办法: 一是利用专业的报表程序,能实现十分复杂的需求,但学习报表也是很痛苦的一件事,我专心研究了一天都没啥进展,也许我实在太菜了.这种方法推荐锐浪报表,免费还好用. 二是利用js插件实现,也在网上找了很多,最后感谢hunduncn,他建议我使用lodop(详见:http://www.thinkphp.cn/topic/12938.html),由于对

打印之Lodop

前序 前面遇到一个问题:在线打印合同.通过各方查找资料和请教他人,终于完美的解决了这个问题.其中的解决方案,可以查看:http://www.cnblogs.com/zcy-xy/p/4290436.html.其中只详细简单介绍了一些方法,希望能给你点思路.其实在后面实现打印时,遇到了很多问题,现在就一一列举. 一.浏览器自带打印 浏览器自带打印确实既方便又简单.其实对于打印我们都很熟悉,最终打印时都是一张图片.那么在调用浏览器自带打印时,它会自动针对当前页面生成一张图片来实现打印.但是会存在一个

CSS基础概念 第一节 引用CSS样式表

引用CSS样式有四种方法,它们包含了:文档内部样式的引用.文档外部样式表的引用. 1.LINK 元素 LINK元素在HTML和XHTML中创建,用来引用外部样式表到当前文档中 通用的语法 <link rel="..." type="text/css" href="..." media="..."> 属性 rel="..." 此属性描述当前文档与链接文件的关系.对于外部样式表,有两种可能的值:st

引用CSS文件到html网页里方法

引用CSS文件到Html方法-css引入,css引用 使用不同的方法来引用css样式表,最终到达的效果相同,但是使用不同方法应用的css文件将影响到SEO及网页打开速度效率. html引用css方法如下1.直接在div中使用css样式制作div+css网页2.html中使用style自带式3.使用@import引用外部CSS文件4.使用link引用外部CSS文件 推荐此方法 扩展阅读:link与import区别 接下来我们将逐个讲解html引用css方法的例子 1.直接在html标签元素内嵌入c

如何为要被打印的内容设置CSS样式属性

如何为要被打印的内容设置CSS样式属性:有时候我们要打印的内容也是需要美观度的,并不希望太原汁原味了,所以要对其进行一下样式设置,本章节就简单介绍一下,如何利用CSS设置要被打印内容的样式,方式有多种,下面一一做一下简单介绍.一.使用link引入外部样式表: <link rel="stylesheet" href="css/style.css" media="screen" /> 以上代码的CSS样式用于屏幕显示效果,对于打印无效.关

网页中如何用 CSS 设置打印分页符

Word 中按 Ctrl + Enter 创建一个分页符,方便打印,其实网页中也可以,用 CSS 的 page-break-after:always;. <p>第 1 页</p><div style="page-break-after:always;"></div><p>第 2 页</p><div style="page-break-after:always;"></div&g

jquery 利用CSS 控制打印样式

一.添加打印样式 1. 为屏幕显示和打印分别准备一个css文件,如下所示:  用于屏幕显示的css: <link rel="stylesheet" href="css/mainstylesheet.css" media="screen" />  用于打印的css:<link rel="stylesheet" href="css/printstylesheet.css" media=&quo