print.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <ele-modal
  3. title="二维码"
  4. :visible.sync="QRvisible"
  5. v-if="QRvisible"
  6. width="800px"
  7. :modal="false"
  8. >
  9. <div id="printSection">
  10. <div
  11. v-for="(item, index) in codeList"
  12. :key="index"
  13. style="
  14. display: flex;
  15. padding-left: 20px;
  16. align-items: center;
  17. justify-content: center;
  18. margin: auto;
  19. "
  20. >
  21. <div style="width: 250px; height: 240px">
  22. <img
  23. :src="item.qrcode"
  24. alt="QR Code"
  25. style="width: 240px; height: 240px"
  26. />
  27. </div>
  28. <div
  29. style="
  30. width: 440px;
  31. height: 240px;
  32. display: flex;
  33. flex-direction: column;
  34. flex-wrap: wrap;
  35. justify-content: center;
  36. "
  37. >
  38. <div style="text-align: left; font-size: 20px">
  39. <span style="display: inline-block; width: 100px">编码:</span>
  40. <span style="color: #000">{{ item.code }}</span>
  41. </div>
  42. <div style="text-align: left; font-size: 20px">
  43. <span style="display: inline-block; width: 100px">名称:</span>
  44. <span style="color: #000">{{ item.name }}</span>
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. <div slot="footer">
  50. <el-button @click="print">打印预览</el-button>
  51. <el-button @click="close">关闭</el-button>
  52. </div>
  53. </ele-modal>
  54. </template>
  55. <script>
  56. import QRCode from 'qrcode';
  57. export default {
  58. name: 'print',
  59. data() {
  60. return {
  61. QRvisible: false,
  62. codeList: []
  63. };
  64. },
  65. methods: {
  66. open(data) {
  67. this.codeList = data;
  68. this.QRvisible = true;
  69. this.$nextTick(() => {
  70. this.generateQRCodes();
  71. });
  72. // queryPrint({ ids }).then(res => {
  73. // this.codeList = res;
  74. // this.QRvisible = true
  75. // this.$nextTick(() => {
  76. // this.generateQRCodes()
  77. // })
  78. // })
  79. },
  80. generateQRCodes() {
  81. this.codeList.forEach((item) => {
  82. QRCode.toDataURL(item.code)
  83. .then((url) => {
  84. item.qrcode = url;
  85. this.$forceUpdate();
  86. })
  87. .catch((err) => {
  88. console.error(err);
  89. });
  90. });
  91. },
  92. close() {
  93. this.QRvisible = false;
  94. },
  95. print() {
  96. const printSection = document.getElementById('printSection');
  97. // 创建打印任务
  98. const printWindow = window.open('', '_blank');
  99. printWindow.document.open();
  100. printWindow.document.write('<html><head><title>打印预览</title>');
  101. printWindow.document.write(
  102. '<link rel="stylesheet" href="your-stylesheet-url.css" type="text/css" />'
  103. );
  104. printWindow.document.write('</head><body>');
  105. printWindow.document.write(printSection.innerHTML);
  106. printWindow.document.write('</body></html>');
  107. printWindow.document.close();
  108. printWindow.onload = function () {
  109. printWindow.print();
  110. };
  111. }
  112. }
  113. };
  114. </script>
  115. <style lang="scss" scoped>
  116. @media print {
  117. #printSection {
  118. font-size: 34px;
  119. span {
  120. font-size: 34px;
  121. }
  122. }
  123. }
  124. </style>