print.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.notPrint ? this.options.notPrint : '.no-print') + "{display:none;}</style>";
  34. str += "<style>.results{width:100%!important;} .result .title{width:100%;}</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. var canvass = document.querySelectorAll('canvas');
  42. for (var k = 0; k < inputs.length; k++) {
  43. if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
  44. if (inputs[k].checked == true) {
  45. inputs[k].setAttribute('checked', "checked")
  46. } else {
  47. inputs[k].removeAttribute('checked')
  48. }
  49. } else if (inputs[k].type == "text") {
  50. inputs[k].setAttribute('value', inputs[k].value)
  51. } else {
  52. inputs[k].setAttribute('value', inputs[k].value)
  53. }
  54. }
  55. for (var k2 = 0; k2 < textareas.length; k2++) {
  56. if (textareas[k2].type == 'textarea') {
  57. textareas[k2].innerHTML = textareas[k2].value
  58. }
  59. }
  60. for (var k3 = 0; k3 < selects.length; k3++) {
  61. if (selects[k3].type == 'select-one') {
  62. var child = selects[k3].children;
  63. for (var i in child) {
  64. if (child[i].tagName == 'OPTION') {
  65. if (child[i].selected == true) {
  66. child[i].setAttribute('selected', "selected")
  67. } else {
  68. child[i].removeAttribute('selected')
  69. }
  70. }
  71. }
  72. }
  73. }
  74. //canvass echars图表转为图片
  75. for (var k4 = 0; k4 < canvass.length; k4++) {
  76. var imageURL = canvass[k4].toDataURL("image/png");
  77. var img = document.createElement("img");
  78. img.src = imageURL;
  79. img.setAttribute('style', 'max-width: 100%;');
  80. img.className = 'isNeedRemove'
  81. // canvass[k4].style.display = 'none'
  82. // canvass[k4].parentNode.style.width = '100%'
  83. // canvass[k4].parentNode.style.textAlign = 'center'
  84. canvass[k4].parentNode.insertBefore(img, canvass[k4].nextElementSibling);
  85. }
  86. //做分页
  87. //style="page-break-after: always"
  88. var pages = document.querySelectorAll('.result');
  89. for (var k5 = 0; k5 < pages.length; k5++) {
  90. pages[k5].setAttribute('style', 'page-break-after: always');
  91. }
  92. return this.dom.outerHTML;
  93. },
  94. writeIframe: function (content) {
  95. var w, doc, iframe = document.createElement('iframe'),
  96. f = document.body.appendChild(iframe);
  97. iframe.id = "myIframe";
  98. //iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
  99. iframe.setAttribute('style', 'position:absolute;width:' + document.querySelector('.results').clientWidth + 'px;height:0;top:-10px;left:-10px;');
  100. w = f.contentWindow || f.contentDocument;
  101. doc = f.contentDocument || f.contentWindow.document;
  102. doc.open();
  103. doc.write(content);
  104. doc.close();
  105. var removes = document.querySelectorAll('.isNeedRemove');
  106. for (var k = 0; k < removes.length; k++) {
  107. removes[k].parentNode.removeChild(removes[k]);
  108. }
  109. var _this = this
  110. iframe.onload = function () {
  111. _this.toPrint(w);
  112. setTimeout(function () {
  113. document.body.removeChild(iframe)
  114. }, 100)
  115. }
  116. },
  117. toPrint: function (frameWindow) {
  118. try {
  119. setTimeout(function () {
  120. frameWindow.focus();
  121. try {
  122. if (!frameWindow.document.execCommand('print', false, null)) {
  123. frameWindow.print();
  124. }
  125. } catch (e) {
  126. frameWindow.print();
  127. }
  128. frameWindow.close();
  129. }, 10);
  130. } catch (err) {
  131. console.log('err', err);
  132. }
  133. },
  134. isDOM: (typeof HTMLElement === 'object') ?
  135. function (obj) {
  136. return obj instanceof HTMLElement;
  137. } :
  138. function (obj) {
  139. return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
  140. }
  141. };
  142. // const MyPlugin = {}
  143. // MyPlugin.install = function (Vue, options) {
  144. // // 4. 添加实例方法
  145. // Vue.prototype.$print = Print
  146. // }
  147. // export default MyPlugin