最近在做Lodop打印功能:
思路是: 用MasterPage搭个打印页面的框架, 然后在具体的页面中填入数据, 打印的样式由母版页和CSS来控制.
困扰了一天的问题是: 在打印的JS文件中, 引用外部css文件时, 需要拼出实际的含有IP地址的CSS文件地址, 并且需要用双斜杠(//)来代替单斜杠.
=====后来遇到的问题: css文件里的样式, 在页面上显示没有问题, 但是打印预览却没有样式....
又挣扎了一天, 原来问题是: lodop中的加入css文件后, 不能识别css类(.baseInfo)这样的样式, 得写到具体的元素中才可以, 如: (.baseInfo Table)下的css才会生效.
对于Repeater中的Table标签, 貌似也加载不了样式, 最后网repeater的Table标签设置了一个ClassName, 才生效.
=====为了能让页眉、页脚、以及内容页, 全部都应用CSS中的样式, 着实费了一番功夫. 不知道是不是<link....> + HTML的方式不好使还是我没弄清楚, CSS样式表无法全部生效(只有用ADD_PRINT_HTM的部分生效, 其他不生效). 最终, 在JS中用ajax获取CSS文件内容, 然后采用<style...>+HTML的方式解决.
代码如下:
//母版页, 注意: 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" />' />
<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" />' />--%> <input id="hidCssRef" name="hidCssRef" type="hidden" value ='http://<%= this.Request.Url.Authority %>/App_Themes/<%=Application["Theme"].ToString()%>/Css/PrintStyleSheet.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) { } } }
//具体的实现代码放在用户控件中
//UC_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 class="detailTabInfo"> <thead> <tr> <td class="detailTitle" style="width:10%;"><span>物资编码</span></td> <td class="detailTitle" style="width:20%;"><span>物资名称</span></td> <td class="detailTitle" style="width:15%;"><span>规格型号</span></td> <td class="detailTitle" style="width:5%;"><span>单位</span></td> <td class="detailTitle" style="width:10%;"><span>账务名称</span></td> <td class="detailTitle" style="width:5%;"><span>入库数量</span></td> <td class="detailTitle" style="width:15%;"><span>入库单价</span></td> <td class="detailTitle" style="width:20%;"><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 id="pageSum" style="display:none;"> <td colspan="6"></td> <td class="detailTitle"><span>当页合计:</span></td> <td class="detailContent" tdata="subSum" format="#,##0.00"><span><font>###</font></span></td> </tr>--%> <tr> <td colspan="6" class="detailTitle"></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>
//UC_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.Summation.ToString("F6"); 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 = geInfo.T_User.UserName; this.lblAccoClk.Text = geInfo.Accoer.UserName; //处理入库单详情 List<E_ZGEGodownEntry> detailLst = this._baoSrv.ZGEdownEntryBAO.GetZGEdownEntryList(null,geInfo.T_GodownEntry,null,null,null,-1,-1); this.rptGEPrn.DataSource = detailLst; 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 } }
//打印页的样式表
/* PrintPages ----------------------------------------------------------*/ body { font-size: .80em; font-family: "Helvetica Neue" , "Lucida Grande" , "Segoe UI" , Arial, Helvetica, Verdana, sans-serif; margin: 0px; padding: 0px; } a:link, a:visited { color: #034af3; } a:hover { color: #1d60ff; text-decoration: none; } a:active { color: #034af3; } p { margin-bottom: 10px; line-height: 1.6em; } /* HEADINGS ----------------------------------------------------------*/ h1, h2, h3, h4, h5, h6 { font-size: 1.5em; color: #666666; font-variant: small-caps; text-transform: none; font-weight: 200; margin-bottom: 0px; } h1 { font-size: 1.6em; padding-bottom: 0px; margin-bottom: 0px; } h2 { font-size: 1.5em; font-weight: 600; } h3 { font-size: 1.2em; } h4 { font-size: 1.1em; } h5, h6 { font-size: 1em; } /* this rule styles <h1> and <h2> tags that are the first child of the left and right table columns */ .rightColumn > h1, .rightColumn > h2, .leftColumn > h1, .leftColumn > h2 { margin-top: 0px; } /* PRIMARY LAYOUT ELEMENTS ----------------------------------------------------------*/ .page { /*min-height:100%; height: 0px; position:absolute; _position:absolute; left: 0;*/ margin: 0 auto; width: 90%; } .prnTitle { width: 100%; /*min-height:20px;*/ height:70px; margin:0 auto; font-family:SimHei; text-align:center; letter-spacing:5px; } .prnTitle h1 { font-size:24px; } .prnTitle h3 { margin-top:5px; font-size:20px; } .prnTable { font-family:SimHei; text-align:center; } .baseInfo { width:100%; height:52px; } .baseInfo table { border: none; width:100%; } .baseTitle { width: 12%; font-size:14px; text-align:right; } .baseContent { width: 22%; font-size:14px; text-align:left; } .detailInfo { width:100%; font-family:SimHei; } .detailTabInfo { width:100%; font-size:14px; border-collapse:collapse; text-align:center; } .detailTitle { border:1px solid #666666; text-align:center; } .detailContent { border:1px solid #666666; text-align:center; } .footInfo { width:100%; margin-top:10px; } .footInfo table { border:none; width:100%; height:30px; } .footTitle { width:12%; text-align:right; font-size:14px; } .footContent { width:21%; text-align:left; font-size:14px; }
//公共的表头
//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')); //显示分页总计 //document.getElementById('pageSum').style.display = ""; //var cssRef = decodeURIComponent(document.getElementById('hidCssRef').value).replace(new RegExp(/\+/g)," "); //样式表文件 var cssRef = document.getElementById('hidCssRef').value; var cssInfo = '<style>' + $.ajax({ type: "GET", url: cssRef, async: false }).responseText + '</style>'; //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 = 768; //窗口的高(单位:px) var strTitleButtonCaption = ''; //预览窗口和打印按钮的名称组合, 字符型, 用"点"分隔 LODOP.SET_PREVIEW_WINDOW(intDispMode, intToolMode, blDirectPrint, intWidth, intHeight, strTitleButtonCaption); //设置打印纸张大小. var intOrient = 1; //设置打印方向: 1-纵向(固定纸张), 2-横向(固定纸张), 3-纵向(宽度固定, 高度自适应), 0-方向不定(操作者自行设置). var intPageWidth = 2100; //纸张宽度(单位0.1mm), 如:45表示4.5mm var intPageHeight = 1480; //纸张高度(单位0.1mm): 固定纸张时,表示纸张高; 自适应高度时, 表示纸张底边的空白高. 1480 var strPageName = 'CreateCustomPage'; //纸张类型: intPageWidth和intPageHeight都为0时才有效. 可使用"CreateCustomPage"在系统内建立名为"LodopCustomPage"的自定义纸张类型. LODOP.SET_PRINT_PAGESIZE(intOrient, intPageWidth, intPageHeight, strPageName); //设置打印风格 //LODOP.SET_PRINT_STYLE("FontSize", 11); //LODOP.SET_PRINT_MODE("PRINT_PAGE_PERCENT", "Full-Width"); //设置打印缩放: 第二个参数可选值(Full-Width, Full-Height, Full-Page, Auto-Width, Auto-Height, Width:200%, Width:200%;Height:200%,55%) LODOP.SET_PRINT_MODE("POS_BASEON_PAPER", true); //这里设置打印内容(Top,Left,Width,Height) //var tabStyle = "<style>table,th,td {border:1px solid #666666;border-collapse:collapse; text-align: center; font-size:small;} table {width:92%;margin:0 auto;}</style>"; //LODOP.ADD_PRINT_TABLE(180, "5%", "100%", 325, tabStyle + document.getElementsByClassName('detailInfo')[0].innerHTML); //LODOP.ADD_PRINT_HTM("5%", "5%", "100%", 140, cssRef + '<body>' + document.getElementById(prnContentId).innerHTML + '</body>'); LODOP.ADD_PRINT_TABLE(155, "10%", "90%", 325, cssInfo + document.getElementsByClassName('detailInfo')[0].innerHTML); //LODOP.SET_PRINT_STYLEA(0, "Vorient", 3); //第一个参数:(1:页眉页脚;2:页号项;3:页数项;4:多页打印;0:普通项); 第二个参数(属性);第三个参数(值); LODOP.SET_PRINT_STYLEA(0, "ItemType", 4); LODOP.SET_PRINT_STYLEA(0, "Horient", 3); LODOP.SET_PRINT_STYLEA(0, "Vorient", 3); LODOP.ADD_PRINT_HTM("5%", "10%", "90%", 125, cssInfo + document.getElementById(prnContentId).innerHTML); LODOP.SET_PRINT_STYLEA(0, "ItemType", 1); LODOP.SET_PRINT_STYLEA(0, "LinkedItem", 1); LODOP.ADD_PRINT_HTM(485, "10%", "90%", 100, cssInfo + document.getElementsByClassName('footInfo')[0].innerHTML); LODOP.SET_PRINT_STYLEA(0, "ItemType", 1); LODOP.SET_PRINT_STYLEA(0, "LinkedItem", 1); LODOP.ADD_PRINT_HTM(485, 665, 80, 20, "<font size='1'><span tdata='pageNO'>第##页</span>/<span tdata='pageCount'>共##页</span></font>"); LODOP.SET_PRINT_STYLEA(0, "ItemType", 1); LODOP.SET_PRINT_STYLEA(0, "LinkedItem", 1); //显示分页总计 //document.getElementById('pageSum').style.display = "none"; }
//项目添加了Lodop饮用后, 把自身的JS文件的修改下, 添加上未安装控件时引用自身目录下的控件文件
//LodopFuncs.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')); //显示分页总计 document.getElementById('pageSum').style.display = ""; //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和intPageHeight都为0时才有效. 可使用"CreateCustomPage"在系统内建立名为"LKodopCustomPage"的自定义纸张类型. LODOP.SET_PRINT_PAGESIZE(intOrient, intPageWidth, intPageHeight, strPageName); //设置打印风格 //LODOP.SET_PRINT_STYLE("FontSize", 11); //这里设置打印内容(Top,Left,Width,Height) //alert(document.getElementsByClassName('detailInfo')[0].innerHTML); var tabStyle = "<style>table,th,td {border:1px solid #666666;border-collapse:collapse;} table {width:92%;margin:0 auto;}</style>"; LODOP.ADD_PRINT_TABLE(180, "5%", "100%", 325, tabStyle + document.getElementsByClassName('detailInfo')[0].innerHTML); LODOP.SET_PRINT_STYLEA(0, "Vorient", 3); LODOP.ADD_PRINT_HTM("5%", "5%", "100%", 145, cssRef + '<body>' + document.getElementById(prnContentId).innerHTML + '</body>'); LODOP.SET_PRINT_STYLEA(0, "ItemType", 1); LODOP.SET_PRINT_STYLEA(0, "LinkedItem", 1); //LODOP.ADD_PRINT_HTM("5", "5%", "100%", 500, cssRef + '<body>' + document.getElementById(prnContentId).innerHTML + '</body>'); //LODOP.ADD_PRINT_TABLE(160, "5%", "100%", 480, document.getElementsByClassName('detailInfo')[0].innerHTML); LODOP.ADD_PRINT_HTM(508, "5%", "100%", 100, cssRef + '<body>' + document.getElementsByClassName('footInfo')[0].innerHTML + '</body>'); LODOP.SET_PRINT_STYLEA(0, "ItemType", 1); LODOP.SET_PRINT_STYLEA(0, "LinkedItem", 1); LODOP.ADD_PRINT_HTM(508, 700, 80, 20, "<font size='1'><span tdata='pageNO'>第##页</span>/<span tdata='pageCount'>共##页</span></font>"); LODOP.SET_PRINT_STYLEA(0, "ItemType", 1); LODOP.SET_PRINT_STYLEA(0, "LinkedItem", 1); //显示分页总计 document.getElementById('pageSum').style.display = "none"; }