print.js 4.0 KB

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