print.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. str += "<style>html,body,div{height: auto!important;font-size:14px}</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. for (var k = 0; k < inputs.length; k++) {
  42. if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
  43. if (inputs[k].checked == true) {
  44. inputs[k].setAttribute('checked', "checked")
  45. } else {
  46. inputs[k].removeAttribute('checked')
  47. }
  48. } else if (inputs[k].type == "text") {
  49. inputs[k].setAttribute('value', inputs[k].value)
  50. } else {
  51. inputs[k].setAttribute('value', inputs[k].value)
  52. }
  53. }
  54. for (var k2 = 0; k2 < textareas.length; k2++) {
  55. if (textareas[k2].type == 'textarea') {
  56. textareas[k2].innerHTML = textareas[k2].value
  57. }
  58. }
  59. for (var k3 = 0; k3 < selects.length; k3++) {
  60. if (selects[k3].type == 'select-one') {
  61. var child = selects[k3].children;
  62. for (var i in child) {
  63. if (child[i].tagName == 'OPTION') {
  64. if (child[i].selected == true) {
  65. child[i].setAttribute('selected', "selected")
  66. } else {
  67. child[i].removeAttribute('selected')
  68. }
  69. }
  70. }
  71. }
  72. }
  73. return this.dom.outerHTML;
  74. },
  75. writeIframe: function (content) {
  76. var w, doc, iframe = document.createElement('iframe'),
  77. f = document.body.appendChild(iframe);
  78. iframe.id = "myIframe";
  79. //iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
  80. iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-10px;left:-10px;');
  81. w = f.contentWindow || f.contentDocument;
  82. doc = f.contentDocument || f.contentWindow.document;
  83. doc.open();
  84. doc.write(content);
  85. doc.close();
  86. var _this = this
  87. iframe.onload = function(){
  88. _this.toPrint(w);
  89. setTimeout(function () {
  90. document.body.removeChild(iframe)
  91. }, 100)
  92. }
  93. },
  94. toPrint: function (frameWindow) {
  95. try {
  96. setTimeout(function () {
  97. frameWindow.focus();
  98. try {
  99. if (!frameWindow.document.execCommand('print', false, null)) {
  100. frameWindow.print();
  101. }
  102. } catch (e) {
  103. frameWindow.print();
  104. }
  105. frameWindow.close();
  106. }, 10);
  107. } catch (err) {
  108. console.log('err', err);
  109. }
  110. },
  111. isDOM: (typeof HTMLElement === 'object') ?
  112. function (obj) {
  113. return obj instanceof HTMLElement;
  114. } :
  115. function (obj) {
  116. return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
  117. }
  118. };
  119. const MyPlugin = {}
  120. MyPlugin.install = function (Vue, options) {
  121. // 4. 添加实例方法
  122. Vue.prototype.$print = Print
  123. }
  124. export default MyPlugin