print.js 4.6 KB

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