preview.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <el-dialog v-model="state.dialogVisible" draggable :close-on-click-modal="false" :width="Number(state.width) + Number(8) + 'mm'">
  3. <div id="preview_content" ref="previewContentRef"></div>
  4. <template #footer>
  5. <el-button :loading="state.waitShowPrinter" type="primary" icon="ele-Printer" @click.stop="print">直接打印</el-button>
  6. <el-button type="primary" icon="ele-Printer" @click.stop="toPdf">导出PDF</el-button>
  7. <el-button key="close" @click="hideDialog"> 关闭 </el-button>
  8. </template>
  9. </el-dialog>
  10. </template>
  11. <script lang="ts" setup>
  12. import { nextTick, reactive, ref } from 'vue';
  13. const state = reactive({
  14. dialogVisible: false,
  15. waitShowPrinter: false,
  16. width: 0, // 纸张宽 mm
  17. printData: {}, // 打印数据
  18. printType: 1, // 默认浏览器打印
  19. printParam: {
  20. printer: '', // 打印机名称
  21. title: '', // 打印任务名称
  22. color: false, // 是否打印颜色 默认 true
  23. copies: 1, // 打印份数 默认 1
  24. },
  25. // 打印参数
  26. hiprintTemplate: {} as any,
  27. });
  28. const previewContentRef = ref();
  29. const showDialog = (hiprintTemplate: any, printData: {}, width = 210, printType = 1, printParam: { printer: ''; title: ''; color: false; copies: 1 }) => {
  30. state.dialogVisible = true;
  31. state.width = width;
  32. state.hiprintTemplate = hiprintTemplate;
  33. state.printData = printData;
  34. state.printParam = printParam;
  35. state.printType = printType;
  36. nextTick(() => {
  37. while (previewContentRef.value?.firstChild) {
  38. previewContentRef.value.removeChild(previewContentRef.value.firstChild);
  39. }
  40. const newHtml = hiprintTemplate.getHtml(printData);
  41. previewContentRef.value.appendChild(newHtml[0]);
  42. });
  43. };
  44. const print = () => {
  45. state.waitShowPrinter = true;
  46. debugger;
  47. // 判断是否已成功连接
  48. if (state.printType == 2) {
  49. // 注意:连接是异步的
  50. // 已连接
  51. // 获取打印机列表
  52. const printerList = state.hiprintTemplate.getPrinterList();
  53. let sfcz = printerList.some((item: any) => {
  54. return item.name == state.printParam.printer;
  55. });
  56. if (!sfcz) {
  57. alert('打印机不存在');
  58. } else {
  59. // 直接打印 将使用系统设置的 默认打印机
  60. state.hiprintTemplate.print2(state.printData, state.printParam);
  61. // 发送任务到打印机成功
  62. state.hiprintTemplate.on('printSuccess', function (e: any) {
  63. state.waitShowPrinter = false;
  64. });
  65. // 发送任务到打印机失败
  66. state.hiprintTemplate.on('printError', function (e: any) {
  67. state.waitShowPrinter = false;
  68. alert('打印失败:' + e);
  69. });
  70. }
  71. } else {
  72. state.hiprintTemplate.print(
  73. state.printData,
  74. {},
  75. {
  76. callback: () => {
  77. state.waitShowPrinter = false;
  78. },
  79. }
  80. );
  81. }
  82. };
  83. const toPdf = () => {
  84. state.hiprintTemplate.toPdf(state.printData, 'PDF文件');
  85. };
  86. const hideDialog = () => {
  87. state.dialogVisible = false;
  88. };
  89. defineExpose({ showDialog });
  90. </script>
  91. <style lang="less" scoped>
  92. :deep(.ant-modal-body) {
  93. padding: 0px;
  94. }
  95. :deep(.ant-modal-content) {
  96. margin-bottom: 24px;
  97. }
  98. </style>