JGPrint.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. // 打印控件
  38. // export const JGPrint = function (data, type, printerType) {
  39. // if (!data) {
  40. // return
  41. // }
  42. // // 针式打印
  43. // if (printerType == 'NEEDLE') {
  44. // const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  45. // console.log(LODOP, 'LODOP')
  46. // if (!LODOP) {
  47. // confirm({
  48. // title: '提示?',
  49. // content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  50. // okText: '立即下载',
  51. // okType: 'danger',
  52. // cancelText: '暂不打印',
  53. // onOk () {
  54. // window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  55. // }
  56. // })
  57. // return
  58. // }
  59. // LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  60. // LODOP.SET_PRINT_MODE('POS_BASEON_PAPER', true) // 可使输出以纸张边缘为基点
  61. // LODOP.ADD_PRINT_HTM('0', '0', 'RightMargin:0cm', 'BottomMargin:0cm', data)
  62. // LODOP.SET_PRINT_PAGESIZE(3, 2090, 45, '') // 这里3表示纵向打印且纸高“按内容的高度”;2140表示纸宽214.0mm;60表示页底空白6.0mm
  63. // // LODOP.SET_PRINT_PAGESIZE(1, 2090, 1000, '')
  64. // // LODOP.ADD_PRINT_HTM('0', '0', '595pt', 1000, data)
  65. // if (type == 'preview') {
  66. // LODOP.PREVIEW()
  67. // }
  68. // if (type == 'print') {
  69. // LODOP.PRINT()
  70. // }
  71. // } else {
  72. // // 喷墨打印
  73. // const url = window.URL.createObjectURL(new Blob([data], { type: 'application/pdf' }))
  74. // document.getElementById('print').innerHTML = '<iframe id="printfsqd" name="printfsqd" src="' + url + '" hidden></iframe>'
  75. // if (type == 'preview') { // 预览
  76. // window.open(url)
  77. // } else if (type == 'print') { // 打印
  78. // window.frames['printfsqd'].focus()
  79. // window.frames['printfsqd'].print()
  80. // }
  81. // }
  82. // }
  83. export const demoGetBASE64 = function (dataArray) {
  84. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  85. var strData = ''
  86. for (var i = 0, ii = dataArray.length; i < ii; i += 3) {
  87. if (isNaN(dataArray[i])) break
  88. var b1 = dataArray[i] & 0xFF; var b2 = dataArray[i + 1] & 0xFF; var b3 = dataArray[i + 2] & 0xFF
  89. var d1 = b1 >> 2; var d2 = ((b1 & 3) << 4) | (b2 >> 4)
  90. var d3 = i + 1 < ii ? ((b2 & 0xF) << 2) | (b3 >> 6) : 64
  91. var d4 = i + 2 < ii ? (b3 & 0x3F) : 64
  92. strData += digits.substring(d1, d1 + 1) + digits.substring(d2, d2 + 1) + digits.substring(d3, d3 + 1) + digits.substring(d4, d4 + 1)
  93. }
  94. return strData
  95. }
  96. // 下载pdf
  97. export const demoDownloadPDF = function (data) {
  98. // if (!(/^https?:/i.test(url))) return
  99. // if (window.XMLHttpRequest) var xhr = new XMLHttpRequest(); else var xhr = new ActiveXObject('MSXML2.XMLHTTP')
  100. // xhr.open('GET', url, false) // 同步方式
  101. // if (xhr.overrideMimeType) {
  102. // try {
  103. // xhr.responseType = 'arraybuffer'
  104. // var arrybuffer = true
  105. // } catch (err) {
  106. // xhr.overrideMimeType('text/plain; charset=x-user-defined')
  107. // }
  108. // }
  109. // xhr.send(null)
  110. // var data = xhr.response || xhr.responseBody
  111. var arrybuffer = true
  112. if (typeof Uint8Array !== 'undefined') {
  113. if (arrybuffer) var dataArray = new Uint8Array(data); else {
  114. var dataArray = new Uint8Array(data.length)
  115. for (var i = 0; i < dataArray.length; i++) { dataArray[i] = data.charCodeAt(i) }
  116. }
  117. }
  118. return demoGetBASE64(dataArray)
  119. }
  120. // 导出下载excel
  121. export const downloadExcel = function (data, fileName) {
  122. if (!data) { return }
  123. const a = moment().format('YYYYMMDDHHmmss')
  124. const fname = fileName + a
  125. const blob = new Blob([data], { type: 'application/vnd.ms-excel' })
  126. if (window.navigator && window.navigator.msSaveOrOpenBlob) {
  127. navigator.msSaveBlob(blob, fname + '.xlsx')
  128. } else {
  129. const link = document.createElement('a')
  130. link.style.display = 'none'
  131. var href = URL.createObjectURL(blob)
  132. link.href = href
  133. link.setAttribute('download', fname + '.xlsx')
  134. document.body.appendChild(link)
  135. link.click()
  136. document.body.removeChild(link)
  137. window.URL.revokeObjectURL(href) // 释放掉blob对象
  138. }
  139. }
  140. /*
  141. *printerType: 打印机类型
  142. *type: 打印预览,打印,导出
  143. *url: 数据请求接口
  144. *params: 请求参数
  145. *fileName: 导出文件名称
  146. *callback: 回调函数
  147. */
  148. // export const hdPrint = function (printerType, type, url, params, fileName, callback) {
  149. // // 打印时需要传打印机类型
  150. // if (type !== 'export') {
  151. // params.type = printerType
  152. // }
  153. // url(params).then(res => {
  154. // if (res.type == 'application/json') {
  155. // var reader = new FileReader()
  156. // reader.addEventListener('loadend', function () {
  157. // const obj = JSON.parse(reader.result)
  158. // notification.error({
  159. // message: '提示',
  160. // description: obj.message
  161. // })
  162. // })
  163. // reader.readAsText(res)
  164. // } else {
  165. // if (type == 'export') { // 导出
  166. // downloadExcel(res, fileName)
  167. // } else { // 打印
  168. // JGPrint(res, type, printerType)
  169. // }
  170. // }
  171. // callback()
  172. // })
  173. // }
  174. /*
  175. *printerType: 打印机类型
  176. *type: 打印预览,打印,导出
  177. *url: 数据请求接口
  178. *params: 请求参数
  179. *fileName: 导出文件名称
  180. *callback: 回调函数
  181. */
  182. export const hdPrint = function (printerType, type, url, params, fileName, callback) {
  183. // 打印时需要传打印机类型
  184. if (type !== 'export') {
  185. params.type = printerType
  186. }
  187. // 针式预览和喷墨预览一样
  188. if (type == 'preview') {
  189. // params.type = 'INK'
  190. // params.printType = 'INK'
  191. // printerType = 'INK'
  192. }
  193. url(params).then(res => {
  194. console.log(res, type, printerType)
  195. if (res.type == 'application/json') {
  196. var reader = new FileReader()
  197. reader.addEventListener('loadend', function () {
  198. const obj = JSON.parse(reader.result)
  199. notification.error({
  200. message: '提示',
  201. description: obj.message
  202. })
  203. })
  204. reader.readAsText(res)
  205. } else {
  206. if (type == 'export') { // 导出
  207. downloadExcel(res, fileName)
  208. } else { // 打印
  209. jGPrint(res, type, printerType)
  210. }
  211. }
  212. callback()
  213. })
  214. }
  215. // 打印控件
  216. export const jGPrint = function (data, type, printerType) {
  217. if (!data) {
  218. return
  219. }
  220. // 针式打印
  221. if (printerType == 'NEEDLE') {
  222. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  223. if (!LODOP) {
  224. confirm({
  225. title: '提示?',
  226. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  227. okText: '立即下载',
  228. okType: 'danger',
  229. cancelText: '暂不打印',
  230. onOk () {
  231. var agent = navigator.userAgent.toLowerCase();
  232. if (agent.indexOf("win32") >= 0 || agent.indexOf("wow32") >= 0) {
  233. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  234. }else if (agent.indexOf("win64") >= 0 || agent.indexOf("wow64") >= 0) {
  235. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  236. }
  237. }
  238. })
  239. return
  240. }
  241. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  242. // LODOP.SET_PRINT_MODE('POS_BASEON_PAPER', true) // 可使输出以纸张边缘为基点
  243. if (type == 'preview') { // 预览
  244. const reader = new FileReader()
  245. reader.readAsDataURL(new Blob([data], { type: 'application/pdf' }))
  246. reader.addEventListener('load', () => {
  247. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', reader.result.split('base64,')[1])
  248. LODOP.SET_PRINT_PAGESIZE(3,2090,65,"");
  249. LODOP.PREVIEW()
  250. })
  251. } else if (type == 'print') { // 打印
  252. const dlen = data.data.length
  253. for(let i=0;i<dlen;i++){
  254. LODOP.PRINT_INIT("")
  255. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', data.data[i])
  256. LODOP.SET_PRINT_PAGESIZE(3, 2090, 65, '')
  257. LODOP.PRINT()
  258. }
  259. }
  260. } else {
  261. // 喷墨打印
  262. const url = window.URL.createObjectURL(new Blob([data], { type: 'application/pdf' }))
  263. document.getElementById('print').innerHTML = '<iframe id="printfsqd" name="printfsqd" src="' + url + '" hidden></iframe>'
  264. if (type == 'preview') { // 预览
  265. window.open(url)
  266. } else if (type == 'print') { // 打印
  267. window.frames['printfsqd'].focus()
  268. window.frames['printfsqd'].print()
  269. }
  270. }
  271. }