JGPrint.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import confirm from 'ant-design-vue/es/modal/confirm'
  2. import notification from 'ant-design-vue/es/notification'
  3. import { getLodop } from '@/libs/LodopFuncs'
  4. import moment from 'moment'
  5. // 打印控件
  6. export const JGPrint = function(data, type, printerType){
  7. if (!data) {
  8. return
  9. }
  10. // 针式打印
  11. if (printerType == 'NEEDLE') {
  12. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  13. console.log(LODOP)
  14. if (!LODOP) {
  15. confirm({
  16. title: '提示?',
  17. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  18. okText: '立即下载',
  19. okType: 'danger',
  20. cancelText: '暂不打印',
  21. onOk () {
  22. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  23. }
  24. })
  25. return
  26. }
  27. const rx = /<body[^>]*>([\s\S]+?)<\/body>/i///
  28. let m = rx.exec(data)
  29. if (m) m = m[1]
  30. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  31. LODOP.SET_PRINT_PAGESIZE(3, '2140', '60', '') // 这里3表示纵向打印且纸高“按内容的高度”;2140表示纸宽214.0mm;45表示页底空白4.5mm
  32. LODOP.ADD_PRINT_HTM("0","0","RightMargin:0.5cm","BottomMargin:0.5cm",m)
  33. // LODOP.ADD_PRINT_TABLE(0, 0, '100%', '100%', m)
  34. if (type == 'preview') {
  35. LODOP.PREVIEW()
  36. }
  37. if (type == 'print') {
  38. LODOP.PRINT()
  39. }
  40. } else {
  41. // 喷墨打印
  42. const url = window.URL.createObjectURL(new Blob([data], { type: 'application/pdf' }))
  43. document.getElementById('print').innerHTML = '<iframe id="printfsqd" name="printfsqd" src="' + url + '" hidden></iframe>'
  44. if (type == 'preview') { // 预览
  45. // window.open(url)
  46. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  47. LODOP.ADD_PRINT_PDF(0,0,"100%","100%",'https://jianguan-images.oss-cn-beijing.aliyuncs.com/helpvideo/1.pdf');
  48. LODOP.SET_PRINT_STYLEA(0,"PDFScalMode",1);
  49. LODOP.SET_PRINT_PAGESIZE(3,0,0,"");
  50. LODOP.PREVIEW()
  51. } else if (type == 'print') { // 打印
  52. window.frames['printfsqd'].focus()
  53. window.frames['printfsqd'].print()
  54. }
  55. }
  56. }
  57. // 导出下载excel
  58. export const downloadExcel = function (data,fileName){
  59. if (!data) { return }
  60. const url = window.URL.createObjectURL(new Blob([data]))
  61. const link = document.createElement('a')
  62. link.style.display = 'none'
  63. link.href = url
  64. const a = moment().format('YYYYMMDDHHmmss')
  65. const fname = fileName + a
  66. link.setAttribute('download', fname + '.xlsx')
  67. document.body.appendChild(link)
  68. link.click()
  69. }
  70. /*
  71. *printerType: 打印机类型
  72. *type: 打印预览,打印,导出
  73. *url: 数据请求接口
  74. *params: 请求参数
  75. *fileName: 导出文件名称
  76. *callback: 回调函数
  77. */
  78. export const hdPrint = function (printerType,type,url,params,fileName,callback){
  79. // 打印时需要传打印机类型
  80. if(type !== 'export'){
  81. params.type = printerType
  82. }
  83. url(params).then(res => {
  84. if (res.type == 'application/json') {
  85. var reader = new FileReader()
  86. reader.addEventListener('loadend', function () {
  87. const obj = JSON.parse(reader.result)
  88. notification.error({
  89. message: '提示',
  90. description: obj.message
  91. })
  92. })
  93. reader.readAsText(res)
  94. } else {
  95. if (type == 'export') { // 导出
  96. downloadExcel(res, fileName)
  97. } else { // 打印
  98. JGPrint(res, type, printerType)
  99. }
  100. }
  101. callback()
  102. })
  103. }