web 后台打印

本文介绍了一种利用JavaScript和jQuery实现的网页打印方法,该方法通过调用特定函数在后台完成打印任务,避免了预览页面的加载,提高了打印效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    //提交打印
    function sbumitPrint() { 
        printHidden("AppsDSPrintDoub.aspx?type=print");
    }

  

function printFrame(frame, onfinish) {
 
    if (!frame)
        frame = window;

    function execOnFinish() {
        switch (typeof (onfinish)) {
            case "string":
                execScript(onfinish);
                break;
            case "function":
                onfinish();
                break;
        }
        if (focused && !focused.disabled)
            focused.focus();
    }
   
    if (frame.document.readyState !== "complete") {
        execOnFinish();
        return;
    }

//    var eventScope = printGetEventScope(frame);
//    var focused = document.activeElement;
    window.printHelper = function() {
        //        printWB.ExecWB(6, 2);
        //        printFireEvent(frame, eventScope, "onafterprint");
        //        printWB.outerHTML = "";
        //        execOnFinish();
        //frame.jqprint();
        var prints = $(frame.document.getElementById('printTable')); //打印table的ID
        prints.jqprint();
        window.printHelper = null;
    }
    //document.body.insertAdjacentHTML("beforeEnd", "<object id=\"printWB\" width=0 height=0 \ classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\">");
    //printFireEvent(frame, eventScope, "onbeforeprint");
    frame.focus();
    window.printHelper();
}

function printIsNativeSupport() {
    var agent = window.navigator.userAgent;
    var i = agent.indexOf("MSIE ") + 5;
    return parseInt(agent.substr(i)) >= 5 && agent.indexOf("5.0b1") < 0;
}

function printFireEvent(frame, obj, name) {
    var handler = obj[name];
    switch (typeof (handler)) {
        case "string":
            frame.execScript(handler);
            break;
        case "function":
            handler();
            break;
    }
}

function printGetEventScope(frame) {
    var frameset = frame.document.all.tags("FRAMESET");
    if (frameset.length)
        return frameset[0];
    return frame.document.body;
}

function printHidden(url) {
  
    document.body.insertAdjacentHTML("beforeEnd", "<iframe name='printHiddenFrame' width='0' height='0'></iframe>");
    var doc = printHiddenFrame.document;
    doc.open();
    doc.write("<body onload=\"parent.onprintHiddenFrame()\">");
    doc.write("<iframe name='printMe' width='0' height='0' src=\"" + url + "\"></iframe>");
    doc.write("</body>");
    doc.close();
}

function onprintHiddenFrame() {
    function onfinish() {
        printHiddenFrame.outerHTML = "";
        if (window.onprintcomplete)
            window.onprintcomplete();
    }
    printFrame(printHiddenFrame.printMe, onfinish);
}

这里还要用到一个jqprint-0.3.js 

// -----------------------------------------------------------------------
// Eros Fratini - eros@recoding.it
// jqprint 0.3
//------------------------------------------------------------------------

(function($) {
    var opt;

    $.fn.jqprint = function (options) {
        opt = $.extend({}, $.fn.jqprint.defaults, options);

        var $element = (this instanceof jQuery) ? this : $(this);
        
        if (opt.operaSupport && $.browser.opera) 
        { 
            var tab = window.open("","jqPrint-preview");
            tab.document.open();

            var doc = tab.document;
        }
        else 
        {
            var $iframe = $("<iframe  />");
        
            if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px" }); }

            $iframe.appendTo("body");
            var doc = $iframe[0].contentWindow.document;
        }
        
        if (opt.importCSS)
        {
            if ($("link[media=print]").length > 0) 
            {
                $("link[media=print]").each( function() {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
                });
            }
            else 
            {
                $("link").each( function() {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
                });
            }
        }
        
        if (opt.printContainer) { doc.write($element.outer()); }
        else { $element.each( function() { doc.write($(this).html()); }); }
        
        doc.close();
        
        (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus();
        setTimeout( function() { (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).print(); if (tab) { tab.close(); } }, 1000);
    }
    
    $.fn.jqprint.defaults = {
		debug: false,
		importCSS: true, 
		printContainer: true,
		operaSupport: true
	};

    // Thanks to 9__, found at http://users.livejournal.com/9__/380664.html
    jQuery.fn.outer = function() {
      return $($('<div></div>').html(this.clone())).html();
    } 
})(jQuery);

可以直接调用jqprint-0.3.js,这样做是为了在提交数据的时候直接打印,等于后台打印,就不要重新打开一个预览页面

 

转载于:https://www.cnblogs.com/dragon-L/p/4235300.html

Web工程中,如果后台打印乱码通常是由于字符编码设置不一致导致的。当从用户端(如浏览器)接收数据时,如果没有正确的字符集解析,就可能出现乱码现象。以下是常见的几个原因及解决步骤: 1. **请求头编码**:检查HTTP请求头的`Content-Type`字段,它应指定发送的数据编码,例如`application/x-www-form-urlencoded` 或 `multipart/form-data`,确保其与后端处理程序所期望的编码匹配。 2. **POST数据编码**:如果是通过POST请求传递的数据,需要确认前端是否正确设置了`charset`属性。比如,在JavaScript中,`new FormData()`默认使用`utf-8`,但如果服务器期待其他编码,可能需要手动设置。 3. **后端解码**:后端语言(如PHP、Java等)中接收字符串时,需要正确地将其转换为正确的字符集,例如`iconv`, `mb_convert_encoding`(PHP),`StringDecoder`(Java)等函数。 4. **文件读写**:如果涉及文件操作,需要确保文件的读取和写入都使用了相同的字符集,避免因编码冲突造成乱码。 5. **数据库连接**:如果数据存储在数据库中,检查连接和查询的字符集设置,确保一致。 6. **响应头设置**:后端返回给前端的HTML或JSON数据,也需要有正确的字符集声明,例如在响应头添加`Content-Type: text/html; charset=UTF-8`。 为了调试,你可以尝试将接收到的文本先转码再打印,或者使用一些工具(如Fiddler、Chrome开发者工具)查看原始请求和响应的实际内容,以便确定确切的问题所在。同时,检查日志文件也可以提供有价值的线索。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值