print.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. return this.wrapperRefDom(this.dom).outerHTML;
  75. },
  76. // 向父级元素循环,包裹当前需要打印的元素
  77. // 防止根级别开头的 css 选择器不生效
  78. wrapperRefDom: function (refDom) {
  79. let prevDom = null
  80. let currDom = refDom
  81. while (currDom && currDom.tagName.toLowerCase() !== 'body') {
  82. if (prevDom) {
  83. let element = currDom.cloneNode(false)
  84. element.appendChild(prevDom)
  85. prevDom = element
  86. } else {
  87. prevDom = currDom.cloneNode(true)
  88. }
  89. currDom = currDom.parentElement
  90. }
  91. return currDom.tagName.toLowerCase() === 'body' ? currDom : prevDom
  92. },
  93. writeIframe: function (content) {
  94. var w, doc, iframe = document.createElement('iframe'),
  95. f = document.body.appendChild(iframe);
  96. iframe.id = "myIframe";
  97. //iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
  98. iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-10px;left:-10px;');
  99. w = f.contentWindow || f.contentDocument;
  100. doc = f.contentDocument || f.contentWindow.document;
  101. doc.open();
  102. doc.write(content);
  103. doc.close();
  104. var _this = this
  105. iframe.onload = function(){
  106. _this.toPrint(w);
  107. setTimeout(function () {
  108. document.body.removeChild(iframe)
  109. }, 100)
  110. }
  111. },
  112. toPrint: function (frameWindow) {
  113. try {
  114. setTimeout(function () {
  115. frameWindow.focus();
  116. try {
  117. if (!frameWindow.document.execCommand('print', false, null)) {
  118. frameWindow.print();
  119. }
  120. } catch (e) {
  121. frameWindow.print();
  122. }
  123. frameWindow.close();
  124. }, 10);
  125. } catch (err) {
  126. console.log('err', err);
  127. }
  128. },
  129. isDOM: (typeof HTMLElement === 'object') ?
  130. function (obj) {
  131. return obj instanceof HTMLElement;
  132. } :
  133. function (obj) {
  134. return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
  135. }
  136. };
  137. const MyPlugin = {}
  138. MyPlugin.install = function (Vue, options) {
  139. // 4. 添加实例方法
  140. Vue.prototype.$print = Print
  141. }
  142. export default MyPlugin