print.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // 打印类属性、方法定义
  2. /* eslint-disable */
  3. const Print = function (dom, options) {
  4. if (!(this instanceof Print)) return new Print(dom, options);
  5. this.options = this.extend({
  6. 'noPrint': '.no-print'
  7. }, options);
  8. if ((typeof dom) === "string") {
  9. this.dom = document.querySelector(dom);
  10. } else {
  11. this.isDOM(dom)
  12. this.dom = this.isDOM(dom) ? dom : dom.$el;
  13. }
  14. this.init();
  15. };
  16. Print.prototype = {
  17. init: function () {
  18. var content = this.getStyle() + this.getHtml();
  19. this.writeIframe(content);
  20. },
  21. extend: function (obj, obj2) {
  22. for (var k in obj2) {
  23. obj[k] = obj2[k];
  24. }
  25. return obj;
  26. },
  27. getStyle: function () {
  28. var str = "",
  29. styles = document.querySelectorAll('style,link');
  30. for (var i = 0; i < styles.length; i++) {
  31. str += styles[i].outerHTML;
  32. }
  33. str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
  34. return str;
  35. },
  36. getHtml: function () {
  37. var inputs = document.querySelectorAll('input');
  38. var textareas = document.querySelectorAll('textarea');
  39. var selects = document.querySelectorAll('select');
  40. for (var k = 0; k < inputs.length; k++) {
  41. if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
  42. if (inputs[k].checked == true) {
  43. inputs[k].setAttribute('checked', "checked")
  44. } else {
  45. inputs[k].removeAttribute('checked')
  46. }
  47. } else if (inputs[k].type == "text") {
  48. inputs[k].setAttribute('value', inputs[k].value)
  49. } else {
  50. inputs[k].setAttribute('value', inputs[k].value)
  51. }
  52. }
  53. for (var k2 = 0; k2 < textareas.length; k2++) {
  54. if (textareas[k2].type == 'textarea') {
  55. textareas[k2].innerHTML = textareas[k2].value
  56. }
  57. }
  58. for (var k3 = 0; k3 < selects.length; k3++) {
  59. if (selects[k3].type == 'select-one') {
  60. var child = selects[k3].children;
  61. for (var i in child) {
  62. if (child[i].tagName == 'OPTION') {
  63. if (child[i].selected == true) {
  64. child[i].setAttribute('selected', "selected")
  65. } else {
  66. child[i].removeAttribute('selected')
  67. }
  68. }
  69. }
  70. }
  71. }
  72. // 包裹要打印的元素
  73. // fix: https://github.com/xyl66/vuePlugs_printjs/issues/36
  74. let outerHTML = this.wrapperRefDom(this.dom).outerHTML
  75. return outerHTML;
  76. },
  77. // 向父级元素循环,包裹当前需要打印的元素
  78. // 防止根级别开头的 css 选择器不生效
  79. wrapperRefDom: function (refDom) {
  80. let prevDom = null
  81. let currDom = refDom
  82. // 判断当前元素是否在 body 中,不在文档中则直接返回该节点
  83. if (!this.isInBody(currDom)) return currDom
  84. while (currDom) {
  85. if (prevDom) {
  86. let element = currDom.cloneNode(false)
  87. element.appendChild(prevDom)
  88. prevDom = element
  89. } else {
  90. prevDom = currDom.cloneNode(true)
  91. }
  92. currDom = currDom.parentElement
  93. }
  94. return prevDom
  95. },
  96. writeIframe: function (content) {
  97. var w, doc, iframe = document.createElement('iframe'),
  98. f = document.body.appendChild(iframe);
  99. iframe.id = "myIframe";
  100. //iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
  101. iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-10px;left:-10px;');
  102. w = f.contentWindow || f.contentDocument;
  103. doc = f.contentDocument || f.contentWindow.document;
  104. doc.open();
  105. doc.write(content);
  106. doc.close();
  107. var _this = this
  108. iframe.onload = function(){
  109. _this.toPrint(w);
  110. setTimeout(function () {
  111. document.body.removeChild(iframe)
  112. }, 100)
  113. }
  114. },
  115. toPrint: function (frameWindow) {
  116. try {
  117. setTimeout(function () {
  118. frameWindow.focus();
  119. try {
  120. if (!frameWindow.document.execCommand('print', false, null)) {
  121. frameWindow.print();
  122. }
  123. } catch (e) {
  124. frameWindow.print();
  125. }
  126. frameWindow.close();
  127. }, 10);
  128. } catch (err) {
  129. console.log('err', err);
  130. }
  131. },
  132. // 检查一个元素是否是 body 元素的后代元素且非 body 元素本身
  133. isInBody: function (node) {
  134. return (node === document.body) ? false : document.body.contains(node);
  135. },
  136. isDOM: (typeof HTMLElement === 'object') ?
  137. function (obj) {
  138. return obj instanceof HTMLElement;
  139. } :
  140. function (obj) {
  141. return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
  142. }
  143. };
  144. const MyPlugin = {}
  145. MyPlugin.install = function (Vue, options) {
  146. // 4. 添加实例方法
  147. Vue.prototype.$print = Print
  148. }
  149. export default MyPlugin