print.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* eslint-disable */
  2. // 打印类属性、方法定义
  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. try {
  10. this.dom = document.querySelector(dom)
  11. }
  12. catch {
  13. const createDom = document.createElement('div')
  14. createDom.innerHTML = dom
  15. this.dom = createDom
  16. }
  17. }
  18. else {
  19. this.isDOM(dom)
  20. this.dom = this.isDOM(dom) ? dom : dom.$el
  21. }
  22. this.init()
  23. }
  24. Print.prototype = {
  25. init() {
  26. const content = this.getStyle() + this.getHtml()
  27. this.writeIframe(content)
  28. },
  29. extend(obj, obj2) {
  30. for (const k in obj2) {
  31. obj[k] = obj2[k]
  32. }
  33. return obj
  34. },
  35. getStyle() {
  36. let str = ''
  37. const styles = document.querySelectorAll('style,link')
  38. for (let i = 0; i < styles.length; i++) {
  39. str += styles[i].outerHTML
  40. }
  41. str += `<style>${ this.options.noPrint ? this.options.noPrint : '.no-print'
  42. }{display:none;}</style>`
  43. str += '<style>html,body{background-color:#fff;}</style>'
  44. return str
  45. },
  46. getHtml() {
  47. const inputs = document.querySelectorAll('input')
  48. const textareas = document.querySelectorAll('textarea')
  49. const selects = document.querySelectorAll('select')
  50. for (let k = 0; k < inputs.length; k++) {
  51. if (inputs[k].type == 'checkbox' || inputs[k].type == 'radio') {
  52. if (inputs[k].checked == true) {
  53. inputs[k].setAttribute('checked', 'checked')
  54. }
  55. else {
  56. inputs[k].removeAttribute('checked')
  57. }
  58. }
  59. else if (inputs[k].type == 'text') {
  60. inputs[k].setAttribute('value', inputs[k].value)
  61. }
  62. else {
  63. inputs[k].setAttribute('value', inputs[k].value)
  64. }
  65. }
  66. for (let k2 = 0; k2 < textareas.length; k2++) {
  67. if (textareas[k2].type == 'textarea') {
  68. textareas[k2].innerHTML = textareas[k2].value
  69. }
  70. }
  71. for (let k3 = 0; k3 < selects.length; k3++) {
  72. if (selects[k3].type == 'select-one') {
  73. const child = selects[k3].children
  74. for (const i in child) {
  75. if (child[i].tagName == 'OPTION') {
  76. if (child[i].selected == true) {
  77. child[i].setAttribute('selected', 'selected')
  78. }
  79. else {
  80. child[i].removeAttribute('selected')
  81. }
  82. }
  83. }
  84. }
  85. }
  86. return this.dom.outerHTML
  87. },
  88. writeIframe(content) {
  89. let w; let doc; const iframe = document.createElement('iframe')
  90. const f = document.body.appendChild(iframe)
  91. iframe.id = 'myIframe'
  92. // iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
  93. iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-10px;left:-10px;')
  94. w = f.contentWindow || f.contentDocument
  95. doc = f.contentDocument || f.contentWindow.document
  96. doc.open()
  97. doc.write(content)
  98. doc.close()
  99. const _this = this
  100. iframe.onload = function() {
  101. _this.toPrint(w)
  102. setTimeout(() => {
  103. document.body.removeChild(iframe)
  104. }, 100)
  105. }
  106. },
  107. toPrint(frameWindow) {
  108. try {
  109. setTimeout(() => {
  110. frameWindow.focus()
  111. try {
  112. if (!frameWindow.document.execCommand('print', false, null)) {
  113. frameWindow.print()
  114. }
  115. }
  116. catch (e) {
  117. frameWindow.print()
  118. }
  119. frameWindow.close()
  120. }, 10)
  121. }
  122. catch (err) {
  123. console.log('err', err)
  124. }
  125. },
  126. isDOM: (typeof HTMLElement === 'object')
  127. ? function(obj) {
  128. return obj instanceof HTMLElement
  129. }
  130. : function(obj) {
  131. return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string'
  132. },
  133. }
  134. export default Print