Print.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* @Print.js
  2. * DH (http://denghao.me)
  3. * 2017-7-14
  4. */
  5. export function Print(dom, options) {
  6. if (!(this instanceof Print)) return new Print(dom, options);
  7. this.options = this.extend(
  8. {
  9. noPrint: ".no-print"
  10. },
  11. options
  12. );
  13. if (typeof dom === "string") {
  14. this.dom = document.querySelector(dom);
  15. } else {
  16. this.dom = dom;
  17. }
  18. this.init();
  19. }
  20. Print.prototype = {
  21. init: function() {
  22. var content = this.getStyle() + this.getHtml();
  23. this.writeIframe(content);
  24. },
  25. extend: function(obj, obj2) {
  26. for (var k in obj2) {
  27. obj[k] = obj2[k];
  28. }
  29. return obj;
  30. },
  31. getStyle: function() {
  32. var str = "",
  33. styles = document.querySelectorAll("style,link");
  34. for (var i = 0; i < styles.length; i++) {
  35. str += styles[i].outerHTML;
  36. }
  37. str +=
  38. "<style>" +
  39. (this.options.noPrint ? this.options.noPrint : ".no-print") +
  40. "{display:none;}</style>";
  41. return str;
  42. },
  43. getHtml: function() {
  44. var inputs = document.querySelectorAll("input");
  45. var textareas = document.querySelectorAll("textarea");
  46. var selects = document.querySelectorAll("select");
  47. for (var k in inputs) {
  48. if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
  49. if (inputs[k].checked == true) {
  50. inputs[k].setAttribute("checked", "checked");
  51. } else {
  52. inputs[k].removeAttribute("checked");
  53. }
  54. } else if (inputs[k].type == "text") {
  55. inputs[k].setAttribute("value", inputs[k].value);
  56. }
  57. }
  58. for (var k2 in textareas) {
  59. if (textareas[k2].type == "textarea") {
  60. textareas[k2].innerHTML = textareas[k2].value;
  61. }
  62. }
  63. for (var k3 in selects) {
  64. if (selects[k3].type == "select-one") {
  65. var child = selects[k3].children;
  66. for (var i in child) {
  67. if (child[i].tagName == "OPTION") {
  68. if (child[i].selected == true) {
  69. child[i].setAttribute("selected", "selected");
  70. } else {
  71. child[i].removeAttribute("selected");
  72. }
  73. }
  74. }
  75. }
  76. }
  77. return this.dom.outerHTML;
  78. },
  79. writeIframe: function(content) {
  80. var w,
  81. doc,
  82. iframe = document.createElement("iframe"),
  83. f = document.body.appendChild(iframe);
  84. iframe.id = "myIframe";
  85. iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
  86. w = f.contentWindow || f.contentDocument;
  87. doc = f.contentDocument || f.contentWindow.document;
  88. doc.open();
  89. doc.write(content);
  90. doc.close();
  91. this.toPrint(w);
  92. // setTimeout(function() {
  93. // document.body.removeChild(iframe)
  94. // }, 100)
  95. },
  96. toPrint: function(frameWindow) {
  97. try {
  98. setTimeout(function() {
  99. frameWindow.focus();
  100. try {
  101. if (!frameWindow.document.execCommand("print", false, null)) {
  102. frameWindow.print();
  103. }
  104. } catch (e) {
  105. frameWindow.print();
  106. }
  107. frameWindow.close();
  108. }, 1000);
  109. } catch (err) {
  110. console.log("err", err);
  111. }
  112. }
  113. };