JGPrint.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 JGPrintTag = function (html, width, height, data) {
  7. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  8. if (!LODOP) {
  9. confirm({
  10. title: '提示?',
  11. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  12. okText: '立即下载',
  13. okType: 'danger',
  14. cancelText: '暂不打印',
  15. onOk () {
  16. var agent = navigator.userAgent.toLowerCase();
  17. if (agent.indexOf("win32") >= 0 || agent.indexOf("wow32") >= 0) {
  18. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  19. }else if (agent.indexOf("win64") >= 0 || agent.indexOf("wow64") >= 0) {
  20. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  21. }
  22. }
  23. })
  24. return
  25. }
  26. LODOP.PRINT_INIT("")
  27. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  28. LODOP.SET_PRINT_MODE('POS_BASEON_PAPER', true) // 可使输出以纸张边缘为基点
  29. LODOP.ADD_PRINT_HTM(0, 0, '100%', '100%', html)
  30. LODOP.ADD_PRINT_BARCODE('36%','65%',90,90,"QRCode",data.qrCodeContent)
  31. LODOP.SET_PRINT_STYLEA(0,"QRCodeVersion",5)
  32. // LODOP.SET_PRINT_STYLEA(0,"QRCodeErrorLevel",'H')
  33. LODOP.SET_PRINT_COPIES(data.printQty)// 指定份数
  34. LODOP.SET_PRINT_PAGESIZE(1, width, height)
  35. LODOP.PRINT()
  36. }
  37. export const demoGetBASE64 = function (dataArray) {
  38. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  39. var strData = ''
  40. for (var i = 0, ii = dataArray.length; i < ii; i += 3) {
  41. if (isNaN(dataArray[i])) break
  42. var b1 = dataArray[i] & 0xFF; var b2 = dataArray[i + 1] & 0xFF; var b3 = dataArray[i + 2] & 0xFF
  43. var d1 = b1 >> 2; var d2 = ((b1 & 3) << 4) | (b2 >> 4)
  44. var d3 = i + 1 < ii ? ((b2 & 0xF) << 2) | (b3 >> 6) : 64
  45. var d4 = i + 2 < ii ? (b3 & 0x3F) : 64
  46. strData += digits.substring(d1, d1 + 1) + digits.substring(d2, d2 + 1) + digits.substring(d3, d3 + 1) + digits.substring(d4, d4 + 1)
  47. }
  48. return strData
  49. }
  50. // 导出下载excel
  51. export const downloadExcel = function (data, fileName) {
  52. if (!data) { return }
  53. const a = moment().format('YYYYMMDDHHmmss')
  54. const fname = fileName + a
  55. const blob = new Blob([data], { type: 'application/vnd.ms-excel' })
  56. if (window.navigator && window.navigator.msSaveOrOpenBlob) {
  57. navigator.msSaveBlob(blob, fname + '.xlsx')
  58. } else {
  59. const link = document.createElement('a')
  60. link.style.display = 'none'
  61. var href = URL.createObjectURL(blob)
  62. link.href = href
  63. link.setAttribute('download', fname + '.xlsx')
  64. document.body.appendChild(link)
  65. link.click()
  66. document.body.removeChild(link)
  67. window.URL.revokeObjectURL(href) // 释放掉blob对象
  68. }
  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. // 针式预览和喷墨预览一样
  84. if (type == 'preview') {
  85. // params.type = 'INK'
  86. // params.printType = 'INK'
  87. // printerType = 'INK'
  88. }
  89. url(params).then(res => {
  90. console.log(res, type, printerType)
  91. if (res.type == 'application/json') {
  92. var reader = new FileReader()
  93. reader.addEventListener('loadend', function () {
  94. const obj = JSON.parse(reader.result)
  95. notification.error({
  96. message: '提示',
  97. description: obj.message
  98. })
  99. })
  100. reader.readAsText(res)
  101. } else {
  102. if (type == 'export') { // 导出
  103. downloadExcel(res, fileName)
  104. } else { // 打印
  105. jGPrint(res, type, printerType)
  106. }
  107. }
  108. callback()
  109. })
  110. }
  111. // pdf blob 转 base64
  112. export const blobToBaseByPdf = function(data,callback){
  113. const reader = new FileReader()
  114. reader.readAsDataURL(new Blob([data], { type: 'application/pdf' }))
  115. reader.addEventListener('load', () => {
  116. callback(reader.result)
  117. })
  118. }
  119. // 打印控件
  120. export const jGPrint = function (data, type, printerType) {
  121. if (!data) {
  122. return
  123. }
  124. // 针式打印
  125. if (printerType == 'NEEDLE') {
  126. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  127. if (!LODOP) {
  128. confirm({
  129. title: '提示?',
  130. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  131. okText: '立即下载',
  132. okType: 'danger',
  133. cancelText: '暂不打印',
  134. onOk () {
  135. var agent = navigator.userAgent.toLowerCase();
  136. if (agent.indexOf("win32") >= 0 || agent.indexOf("wow32") >= 0) {
  137. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  138. }else if (agent.indexOf("win64") >= 0 || agent.indexOf("wow64") >= 0) {
  139. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  140. }
  141. }
  142. })
  143. return
  144. }
  145. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  146. // LODOP.SET_PRINT_MODE('POS_BASEON_PAPER', true) // 可使输出以纸张边缘为基点
  147. if (type == 'preview') { // 预览
  148. // blobToBaseByPdf(data,function(result){
  149. // LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', result.split('base64,')[1])
  150. // LODOP.SET_PRINT_PAGESIZE(3,2090,65,"");
  151. // LODOP.PREVIEW()
  152. // })
  153. if(data.data.length > 1){
  154. LODOP.SET_SHOW_MODE("HIDE_PBUTTIN_PREVIEW",1);//隐藏打印按钮
  155. }
  156. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', data.data[0])
  157. LODOP.SET_PRINT_PAGESIZE(3,2090,40,"");
  158. LODOP.PREVIEW()
  159. } else if (type == 'print') { // 打印
  160. const dlen = data.data.length
  161. for(let i=0;i<dlen;i++){
  162. LODOP.PRINT_INIT("")
  163. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', data.data[i])
  164. LODOP.SET_PRINT_PAGESIZE(3, 2090, 40, '')
  165. LODOP.PRINT()
  166. }
  167. }
  168. } else {
  169. // 喷墨打印
  170. const url = window.URL.createObjectURL(new Blob([data], { type: 'application/pdf' }))
  171. document.getElementById('print').innerHTML = '<iframe id="printfsqd" name="printfsqd" src="' + url + '" hidden></iframe>'
  172. if (type == 'preview') { // 预览
  173. window.open(url)
  174. } else if (type == 'print') { // 打印
  175. window.frames['printfsqd'].focus()
  176. window.frames['printfsqd'].print()
  177. }
  178. }
  179. }