1
duhe
4 天以前 13ccc20164939867a5f6f8964eed8dfdff11bd57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
(function ($) {
  $.fn.hiwprint = function (options) {
    var hasLoaded = false;
    var usedFrame = document.getElementById('hiwprint_iframe');
    if (usedFrame) usedFrame.parentNode.removeChild(usedFrame);
    var opt = $.extend({}, $.fn.hiwprint.defaults, options);
    var $element = this;
    var $iframe = $('<iframe id="hiwprint_iframe"  style="visibility: hidden; height: 0; width: 0; position: absolute;"></iframe>');
    var css = '';
    if (opt.importCss) {
      if (opt.styleHandler) {
        css += opt.styleHandler()
      }
      if ($("link[media=print]").length > 0) {
        $("link[media=print]").each(function () {
          if ($(this).attr("href").indexOf('print-lock.css') >= 0) {
            css += '<link rel="stylesheet" type="text/css" media="print" href="' + $(this).attr("href") + '">';
            // ↑若加上media="print",仅对浏览器打印时有效 所以查看iframe页面时样式无效
            css += '<link rel="stylesheet" type="text/css" href="' + $(this).attr("href") + '">';
          }
        });
      }
    }
    $iframe[0].srcdoc = '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8">' + css + '</head><body></body></html>';
 
    $iframe[0].onload = function () {
      if (hasLoaded) return;
      hasLoaded = true;
      var printDocument = $iframe[0].contentWindow || $iframe[0].contentDocument;
      if (printDocument.document) printDocument = printDocument.document;
      if (!$iframe.attr('srcdoc')) {
        printDocument.write('<!DOCTYPE html><html><head><title></title><meta charset="UTF-8">' + css + '</head><body></body></html>');
      }
      if (opt.printContainer) {
        printDocument.body.innerHTML = $element[0].outerHTML;
      } else {
        printDocument.body.innerHTML = $element.html();
      }
      loadAllImages(printDocument, function () {
 
        performPrint($iframe[0], opt);
      });
 
    };
 
    $iframe.appendTo("body");
 
  };
 
  $.fn.hiwprint.defaults = {
    importCss: true,
    printContainer: true,
    callback: null,
    styleHandler: null,
  };
 
  function performPrint(iframeElement, opt) {
    try {
      iframeElement.focus();
      if (isEdge() || isIE()) {
        try {
          iframeElement.contentWindow.document.execCommand('print', false, null);
        } catch (e) {
          iframeElement.contentWindow.print();
        }
      } else {
        // Other browsers
        iframeElement.contentWindow.print();
      }
      if (opt.callback) {
        opt.callback()
      }
    } catch (error) {
      console.log(error);
    }
  }
 
 
  function isIE() {
    return navigator.userAgent.indexOf('MSIE') !== -1 || !!document.documentMode;
  }
 
  // Edge 20+
  function isEdge() {
    return !isIE() && !!window.StyleMedia;
  }
 
 
  function loadAllImages(printDocument, callback, time) {
 
    if (time === undefined) {
      time = 0;
    }
    var images = printDocument.getElementsByTagName('img');
    var allLoaded = true;
    for (var i = 0; i < images.length; i++) {
      var image = images[i];
      if (image.src && image.src !== window.location.href && image.src.indexOf('base64') == -1) {
 
        if (!image || typeof image.naturalWidth === 'undefined' || image.naturalWidth === 0 || !image.complete) {
          if (!image.complete) {
            allLoaded = false;
          }
        }
      }
    }
    time++;
    if (!allLoaded && time < 10) {
 
      setTimeout(function () {
        loadAllImages(printDocument, callback, time);
      }, 500);
    } else {
      callback();
    }
  }
 
 
})(jQuery);