print.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /***** iframe打印
  2. * @param dom 打印区域的class, id
  3. * @param options.margin 控制页眉页脚, 默认 4mm
  4. * @param options.padding 打印边距, 默认 '0 0'
  5. * @method options.afterprint 打印结束后回调
  6. * @method options.cancel 取消打印后回调
  7. *
  8. * **/
  9. const Print = function(dom, options) {
  10. if (!(this instanceof Print)) return new Print(dom, options);
  11. this.options = this.extend(
  12. {
  13. noPrint: ".no-print",
  14. },
  15. options
  16. );
  17. if (typeof dom === "string") {
  18. this.dom = document.querySelector(dom);
  19. } else {
  20. this.isDOM(dom);
  21. this.dom = this.isDOM(dom) ? dom : dom.$el;
  22. }
  23. this.init();
  24. };
  25. Print.prototype = {
  26. init: function() {
  27. var content = this.getStyle() + this.getHtml();
  28. this.writeIframe(content);
  29. },
  30. extend: function(obj, obj2) {
  31. for (var k in obj2) {
  32. obj[k] = obj2[k];
  33. }
  34. return obj;
  35. },
  36. getStyle: function() {
  37. var str = "",
  38. styles = document.querySelectorAll("style,link");
  39. for (var i = 0; i < styles.length; i++) {
  40. str += styles[i].outerHTML;
  41. }
  42. str +=
  43. "<style>" +
  44. (this.options.noPrint ? this.options.noPrint : ".no-print") +
  45. "{display:none;}</style>";
  46. str += `<style>
  47. @media print {
  48. @page { margin: ${this.options.margin || '4mm'}; padding: ${this.options.padding || '0 0'}; width:210mm;height:290mm}
  49. body {
  50. -webkit-print-color-adjust:exact;-moz-print-color-adjust:exact;-ms-print-color-adjust:exact;print-color-adjust:exact;
  51. }
  52. }
  53. </style>`;
  54. return str;
  55. },
  56. getHtml: function() {
  57. var inputs = document.querySelectorAll("input");
  58. var textareas = document.querySelectorAll("textarea");
  59. var selects = document.querySelectorAll("select");
  60. for (var k = 0; k < inputs.length; k++) {
  61. if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
  62. if (inputs[k].checked == true) {
  63. inputs[k].setAttribute("checked", "checked");
  64. } else {
  65. inputs[k].removeAttribute("checked");
  66. }
  67. } else if (inputs[k].type == "text") {
  68. inputs[k].setAttribute("value", inputs[k].value);
  69. } else {
  70. inputs[k].setAttribute("value", inputs[k].value);
  71. }
  72. }
  73. for (var k2 = 0; k2 < textareas.length; k2++) {
  74. if (textareas[k2].type == "textarea") {
  75. textareas[k2].innerHTML = textareas[k2].value;
  76. }
  77. }
  78. for (var k3 = 0; k3 < selects.length; k3++) {
  79. if (selects[k3].type == "select-one") {
  80. var child = selects[k3].children;
  81. for (var i in child) {
  82. if (child[i].tagName == "OPTION") {
  83. if (child[i].selected == true) {
  84. child[i].setAttribute("selected", "selected");
  85. } else {
  86. child[i].removeAttribute("selected");
  87. }
  88. }
  89. }
  90. }
  91. }
  92. return this.dom.outerHTML;
  93. },
  94. writeIframe: function(content) {
  95. var w,
  96. doc,
  97. iframe = document.createElement("iframe"),
  98. f = document.body.appendChild(iframe);
  99. iframe.id = "myIframe";
  100. iframe.setAttribute(
  101. "style",
  102. "position:absolute;width:0;height:0;top:-10px;left:-10px;"
  103. );
  104. w = f.contentWindow || f.contentDocument;
  105. doc = f.contentDocument || f.contentWindow.document;
  106. doc.open();
  107. doc.write(content);
  108. doc.close();
  109. var _this = this;
  110. iframe.onload = function() {
  111. w.onbeforeprint = _this.options.beforeprint;
  112. w.onafterprint = _this.options.afterprint;
  113. // iframe.contentWindow.print();
  114. _this.toPrint(w);
  115. setTimeout(function() {
  116. document.body.removeChild(iframe);
  117. }, 100);
  118. };
  119. },
  120. toPrint: function(frameWindow) {
  121. var _t = this;
  122. try {
  123. setTimeout(function() {
  124. frameWindow.focus();
  125. try {
  126. if (!frameWindow.document.execCommand("print", false, null)) {
  127. frameWindow.print();
  128. }
  129. } catch (e) {
  130. frameWindow.print();
  131. }
  132. frameWindow.close();
  133. typeof _t.options.cancel === "function" && _t.options.cancel();
  134. frameWindow.onbeforeprint = null;
  135. frameWindow.onafterprint = null;
  136. }, 10);
  137. } catch (err) {
  138. console.log("err", err);
  139. }
  140. },
  141. isDOM:
  142. typeof HTMLElement === "object"
  143. ? function(obj) {
  144. return obj instanceof HTMLElement;
  145. }
  146. : function(obj) {
  147. return (
  148. obj &&
  149. typeof obj === "object" &&
  150. obj.nodeType === 1 &&
  151. typeof obj.nodeName === "string"
  152. );
  153. },
  154. };
  155. // export default Print