|
@@ -0,0 +1,180 @@
|
|
1
|
+/***** iframe打印
|
|
2
|
+ * @param dom 打印区域的class, id
|
|
3
|
+ * @param options.margin 控制页眉页脚, 默认 4mm
|
|
4
|
+ * @param options.padding 打印边距, 默认 '0 0'
|
|
5
|
+ * @method options.afterprint 打印结束后回调
|
|
6
|
+ * @method options.cancel 取消打印后回调
|
|
7
|
+ *
|
|
8
|
+ * **/
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+ const Print = function(dom, options) {
|
|
12
|
+ if (!(this instanceof Print)) return new Print(dom, options);
|
|
13
|
+
|
|
14
|
+ this.options = this.extend(
|
|
15
|
+ {
|
|
16
|
+ noPrint: ".no-print",
|
|
17
|
+ },
|
|
18
|
+ options
|
|
19
|
+ );
|
|
20
|
+
|
|
21
|
+ if (typeof dom === "string") {
|
|
22
|
+ this.dom = document.querySelector(dom);
|
|
23
|
+ } else {
|
|
24
|
+ this.isDOM(dom);
|
|
25
|
+ this.dom = this.isDOM(dom) ? dom : dom.$el;
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ this.init();
|
|
29
|
+ };
|
|
30
|
+ Print.prototype = {
|
|
31
|
+ init: function() {
|
|
32
|
+ var content = this.getStyle() + this.getHtml();
|
|
33
|
+ this.writeIframe(content);
|
|
34
|
+ },
|
|
35
|
+ extend: function(obj, obj2) {
|
|
36
|
+ for (var k in obj2) {
|
|
37
|
+ obj[k] = obj2[k];
|
|
38
|
+ }
|
|
39
|
+ return obj;
|
|
40
|
+ },
|
|
41
|
+
|
|
42
|
+ getStyle: function() {
|
|
43
|
+ var str = "",
|
|
44
|
+ styles = document.querySelectorAll("style,link");
|
|
45
|
+ for (var i = 0; i < styles.length; i++) {
|
|
46
|
+ str += styles[i].outerHTML;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+ str +=
|
|
52
|
+ "<style>" +
|
|
53
|
+ (this.options.noPrint ? this.options.noPrint : ".no-print") +
|
|
54
|
+ "{display:none;}</style>";
|
|
55
|
+
|
|
56
|
+ str += `<style>
|
|
57
|
+ @media print {
|
|
58
|
+ @page { margin: ${this.options.margin || '4mm'}; padding: ${this.options.padding || '0 0'}; width:210mm;height:290mm}
|
|
59
|
+ body {
|
|
60
|
+ -webkit-print-color-adjust:exact;-moz-print-color-adjust:exact;-ms-print-color-adjust:exact;print-color-adjust:exact;
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+ </style>`;
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+ return str;
|
|
67
|
+ },
|
|
68
|
+
|
|
69
|
+ getHtml: function() {
|
|
70
|
+ var inputs = document.querySelectorAll("input");
|
|
71
|
+ var textareas = document.querySelectorAll("textarea");
|
|
72
|
+ var selects = document.querySelectorAll("select");
|
|
73
|
+
|
|
74
|
+ for (var k = 0; k < inputs.length; k++) {
|
|
75
|
+ if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
|
|
76
|
+ if (inputs[k].checked == true) {
|
|
77
|
+ inputs[k].setAttribute("checked", "checked");
|
|
78
|
+ } else {
|
|
79
|
+ inputs[k].removeAttribute("checked");
|
|
80
|
+ }
|
|
81
|
+ } else if (inputs[k].type == "text") {
|
|
82
|
+ inputs[k].setAttribute("value", inputs[k].value);
|
|
83
|
+ } else {
|
|
84
|
+ inputs[k].setAttribute("value", inputs[k].value);
|
|
85
|
+ }
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ for (var k2 = 0; k2 < textareas.length; k2++) {
|
|
89
|
+ if (textareas[k2].type == "textarea") {
|
|
90
|
+ textareas[k2].innerHTML = textareas[k2].value;
|
|
91
|
+ }
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ for (var k3 = 0; k3 < selects.length; k3++) {
|
|
95
|
+ if (selects[k3].type == "select-one") {
|
|
96
|
+ var child = selects[k3].children;
|
|
97
|
+ for (var i in child) {
|
|
98
|
+ if (child[i].tagName == "OPTION") {
|
|
99
|
+ if (child[i].selected == true) {
|
|
100
|
+ child[i].setAttribute("selected", "selected");
|
|
101
|
+ } else {
|
|
102
|
+ child[i].removeAttribute("selected");
|
|
103
|
+ }
|
|
104
|
+ }
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ return this.dom.outerHTML;
|
|
110
|
+ },
|
|
111
|
+
|
|
112
|
+ writeIframe: function(content) {
|
|
113
|
+ var w,
|
|
114
|
+ doc,
|
|
115
|
+ iframe = document.createElement("iframe"),
|
|
116
|
+ f = document.body.appendChild(iframe);
|
|
117
|
+ iframe.id = "myIframe";
|
|
118
|
+ iframe.setAttribute(
|
|
119
|
+ "style",
|
|
120
|
+ "position:absolute;width:0;height:0;top:-10px;left:-10px;"
|
|
121
|
+ );
|
|
122
|
+
|
|
123
|
+ w = f.contentWindow || f.contentDocument;
|
|
124
|
+ doc = f.contentDocument || f.contentWindow.document;
|
|
125
|
+ doc.open();
|
|
126
|
+ doc.write(content);
|
|
127
|
+ doc.close();
|
|
128
|
+ var _this = this;
|
|
129
|
+
|
|
130
|
+ iframe.onload = function() {
|
|
131
|
+ w.onbeforeprint = _this.options.beforeprint;
|
|
132
|
+ w.onafterprint = _this.options.afterprint;
|
|
133
|
+ // iframe.contentWindow.print();
|
|
134
|
+ _this.toPrint(w);
|
|
135
|
+ setTimeout(function() {
|
|
136
|
+ document.body.removeChild(iframe);
|
|
137
|
+ }, 100);
|
|
138
|
+ };
|
|
139
|
+ },
|
|
140
|
+
|
|
141
|
+ toPrint: function(frameWindow) {
|
|
142
|
+ var _t = this;
|
|
143
|
+ try {
|
|
144
|
+ setTimeout(function() {
|
|
145
|
+ frameWindow.focus();
|
|
146
|
+ try {
|
|
147
|
+ if (!frameWindow.document.execCommand("print", false, null)) {
|
|
148
|
+
|
|
149
|
+ frameWindow.print();
|
|
150
|
+ }
|
|
151
|
+ } catch (e) {
|
|
152
|
+
|
|
153
|
+ frameWindow.print();
|
|
154
|
+ }
|
|
155
|
+ frameWindow.close();
|
|
156
|
+ typeof _t.options.cancel === "function" && _t.options.cancel();
|
|
157
|
+ frameWindow.onbeforeprint = null;
|
|
158
|
+ frameWindow.onafterprint = null;
|
|
159
|
+ }, 10);
|
|
160
|
+ } catch (err) {
|
|
161
|
+ console.log("err", err);
|
|
162
|
+ }
|
|
163
|
+ },
|
|
164
|
+
|
|
165
|
+ isDOM:
|
|
166
|
+ typeof HTMLElement === "object"
|
|
167
|
+ ? function(obj) {
|
|
168
|
+ return obj instanceof HTMLElement;
|
|
169
|
+ }
|
|
170
|
+ : function(obj) {
|
|
171
|
+ return (
|
|
172
|
+ obj &&
|
|
173
|
+ typeof obj === "object" &&
|
|
174
|
+ obj.nodeType === 1 &&
|
|
175
|
+ typeof obj.nodeName === "string"
|
|
176
|
+ );
|
|
177
|
+ },
|
|
178
|
+ };
|
|
179
|
+// export default Print
|
|
180
|
+
|