print.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //支持分页
  2. // 打印类属性、方法定义
  3. /* eslint-disable */
  4. const Print = function (dom, options) {
  5. if (!(this instanceof Print)) return new Print(dom, options);
  6. this.options = this.extend({
  7. 'noPrint': '.no-print'
  8. }, options);
  9. if ((typeof dom) === "string") {
  10. this.dom = document.querySelector(dom);
  11. } else {
  12. this.isDOM(dom)
  13. this.dom = this.isDOM(dom) ? dom : dom.$el;
  14. }
  15. this.init();
  16. };
  17. Print.prototype = {
  18. init: function () {
  19. var content = this.getStyle() + this.getHtml();
  20. this.writeIframe(content);
  21. },
  22. extend: function (obj, obj2) {
  23. for (var k in obj2) {
  24. obj[k] = obj2[k];
  25. }
  26. return obj;
  27. },
  28. getStyle: function () {
  29. var str = "",
  30. styles = document.querySelectorAll('style,link');
  31. for (var i = 0; i < styles.length; i++) {
  32. str += styles[i].outerHTML;
  33. }
  34. str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
  35. return str;
  36. },
  37. getHtml: function () {
  38. var inputs = document.querySelectorAll('input');
  39. var textareas = document.querySelectorAll('textarea');
  40. var selects = document.querySelectorAll('select');
  41. for (var k = 0; k < inputs.length; k++) {
  42. if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
  43. if (inputs[k].checked == true) {
  44. inputs[k].setAttribute('checked', "checked")
  45. } else {
  46. inputs[k].removeAttribute('checked')
  47. }
  48. } else if (inputs[k].type == "text") {
  49. inputs[k].setAttribute('value', inputs[k].value)
  50. } else {
  51. inputs[k].setAttribute('value', inputs[k].value)
  52. }
  53. }
  54. for (var k2 = 0; k2 < textareas.length; k2++) {
  55. if (textareas[k2].type == 'textarea') {
  56. textareas[k2].innerHTML = textareas[k2].value
  57. }
  58. }
  59. for (var k3 = 0; k3 < selects.length; k3++) {
  60. if (selects[k3].type == 'select-one') {
  61. var child = selects[k3].children;
  62. for (var i in child) {
  63. if (child[i].tagName == 'OPTION') {
  64. if (child[i].selected == true) {
  65. child[i].setAttribute('selected', "selected")
  66. } else {
  67. child[i].removeAttribute('selected')
  68. }
  69. }
  70. }
  71. }
  72. }
  73. return this.dom.outerHTML;
  74. },
  75. writeIframe: function (content) {
  76. var w, doc, iframe = document.createElement('iframe'),
  77. f = document.body.appendChild(iframe);
  78. iframe.id = "myIframe";
  79. //iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
  80. iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-10px;left:-10px;');
  81. w = f.contentWindow || f.contentDocument;
  82. doc = f.contentDocument || f.contentWindow.document;
  83. doc.open();
  84. doc.write(content);
  85. doc.close();
  86. var _this = this
  87. iframe.onload = function(){
  88. _this.toPrint(w);
  89. setTimeout(function () {
  90. document.body.removeChild(iframe)
  91. }, 100)
  92. }
  93. },
  94. toPrint: function (frameWindow) {
  95. try {
  96. setTimeout(function () {
  97. frameWindow.focus();
  98. try {
  99. if (!frameWindow.document.execCommand('print', false, null)) {
  100. frameWindow.print();
  101. }
  102. } catch (e) {
  103. frameWindow.print();
  104. }
  105. frameWindow.close();
  106. }, 10);
  107. } catch (err) {
  108. console.log('err', err);
  109. }
  110. },
  111. isDOM: (typeof HTMLElement === 'object') ?
  112. function (obj) {
  113. return obj instanceof HTMLElement;
  114. } :
  115. function (obj) {
  116. return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
  117. }
  118. };
  119. const MyPlugin = {}
  120. MyPlugin.install = function (Vue, options) {
  121. // 4. 添加实例方法
  122. Vue.prototype.$printE = Print
  123. }
  124. export default MyPlugin
  125. //不支持分页
  126. /* eslint-disable */
  127. // const Print = function (dom, options) {
  128. // if (!(this instanceof Print)) return new Print(dom, options);
  129. //
  130. // this.options = this.extend({
  131. // 'noPrint': '.no-print'
  132. // }, options);
  133. //
  134. // if ((typeof dom) === "string") {
  135. // this.dom = document.querySelector(dom);
  136. // } else {
  137. // this.isDOM(dom)
  138. // this.dom = this.isDOM(dom) ? dom : dom.$el;
  139. // }
  140. //
  141. // this.init();
  142. // };
  143. // Print.prototype = {
  144. // init: function () {
  145. // var content = this.getStyle() + this.getHtml();
  146. // this.writeIframe(content);
  147. // },
  148. // extend: function (obj, obj2) {
  149. // for (var k in obj2) {
  150. // obj[k] = obj2[k];
  151. // }
  152. // return obj;
  153. // },
  154. //
  155. // getStyle: function () {
  156. // var str = "",
  157. // styles = document.querySelectorAll('style,link');
  158. // for (var i = 0; i < styles.length; i++) {
  159. // str += styles[i].outerHTML;
  160. // }
  161. // str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
  162. //
  163. // return str;
  164. // },
  165. //
  166. // getHtml: function () {
  167. // var inputs = document.querySelectorAll('input');
  168. // var textareas = document.querySelectorAll('textarea');
  169. // var selects = document.querySelectorAll('select');
  170. //
  171. // for (var k = 0; k < inputs.length; k++) {
  172. // if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
  173. // if (inputs[k].checked == true) {
  174. // inputs[k].setAttribute('checked', "checked")
  175. // } else {
  176. // inputs[k].removeAttribute('checked')
  177. // }
  178. // } else if (inputs[k].type == "text") {
  179. // inputs[k].setAttribute('value', inputs[k].value)
  180. // } else {
  181. // inputs[k].setAttribute('value', inputs[k].value)
  182. // }
  183. // }
  184. //
  185. // for (var k2 = 0; k2 < textareas.length; k2++) {
  186. // if (textareas[k2].type == 'textarea') {
  187. // textareas[k2].innerHTML = textareas[k2].value
  188. // }
  189. // }
  190. //
  191. // for (var k3 = 0; k3 < selects.length; k3++) {
  192. // if (selects[k3].type == 'select-one') {
  193. // var child = selects[k3].children;
  194. // for (var i in child) {
  195. // if (child[i].tagName == 'OPTION') {
  196. // if (child[i].selected == true) {
  197. // child[i].setAttribute('selected', "selected")
  198. // } else {
  199. // child[i].removeAttribute('selected')
  200. // }
  201. // }
  202. // }
  203. // }
  204. // }
  205. // // 包裹要打印的元素
  206. // // fix: https://github.com/xyl66/vuePlugs_printjs/issues/36
  207. // let outerHTML = this.wrapperRefDom(this.dom).outerHTML
  208. // return outerHTML;
  209. // },
  210. // // 向父级元素循环,包裹当前需要打印的元素
  211. // // 防止根级别开头的 css 选择器不生效
  212. // wrapperRefDom: function (refDom) {
  213. // let prevDom = null
  214. // let currDom = refDom
  215. // // 判断当前元素是否在 body 中,不在文档中则直接返回该节点
  216. // if (!this.isInBody(currDom)) return currDom
  217. //
  218. // while (currDom) {
  219. // if (prevDom) {
  220. // let element = currDom.cloneNode(false)
  221. // element.appendChild(prevDom)
  222. // prevDom = element
  223. // } else {
  224. // prevDom = currDom.cloneNode(true)
  225. // }
  226. //
  227. // currDom = currDom.parentElement
  228. // }
  229. //
  230. // return prevDom
  231. // },
  232. //
  233. // writeIframe: function (content) {
  234. // var w, doc, iframe = document.createElement('iframe'),
  235. // f = document.body.appendChild(iframe);
  236. // iframe.id = "myIframe";
  237. // //iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
  238. // iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-10px;left:-10px;');
  239. // w = f.contentWindow || f.contentDocument;
  240. // doc = f.contentDocument || f.contentWindow.document;
  241. // doc.open();
  242. // doc.write(content);
  243. // doc.close();
  244. // var _this = this
  245. // iframe.onload = function(){
  246. // _this.toPrint(w);
  247. // setTimeout(function () {
  248. // document.body.removeChild(iframe)
  249. // }, 100)
  250. // }
  251. // },
  252. //
  253. // toPrint: function (frameWindow) {
  254. // try {
  255. // setTimeout(function () {
  256. // frameWindow.focus();
  257. // try {
  258. // if (!frameWindow.document.execCommand('print', false, null)) {
  259. // frameWindow.print();
  260. // }
  261. // } catch (e) {
  262. // frameWindow.print();
  263. // }
  264. // frameWindow.close();
  265. // }, 10);
  266. // } catch (err) {
  267. // console.log('err', err);
  268. // }
  269. // },
  270. // // 检查一个元素是否是 body 元素的后代元素且非 body 元素本身
  271. // isInBody: function (node) {
  272. // return (node === document.body) ? false : document.body.contains(node);
  273. // },
  274. // isDOM: (typeof HTMLElement === 'object') ?
  275. // function (obj) {
  276. // return obj instanceof HTMLElement;
  277. // } :
  278. // function (obj) {
  279. // return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
  280. // }
  281. // };
  282. // const MyPlugin = {}
  283. // MyPlugin.install = function (Vue, options) {
  284. // // 4. 添加实例方法
  285. // Vue.prototype.$print = Print
  286. // }
  287. // export default MyPlugin