JGPrint.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 { printLogSave, printLogSaveBatch } from '@/api/data'
  5. import moment from 'moment'
  6. // 打印页签,支持批量打印
  7. export const JGPrintTag = function (html, width, height, data) {
  8. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  9. if (!LODOP) {
  10. confirm({
  11. title: '提示?',
  12. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  13. okText: '立即下载',
  14. okType: 'danger',
  15. cancelText: '暂不打印',
  16. onOk () {
  17. var agent = navigator.userAgent.toLowerCase()
  18. if (agent.indexOf('win32') >= 0 || agent.indexOf('wow32') >= 0) {
  19. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  20. } else if (agent.indexOf('win64') >= 0 || agent.indexOf('wow64') >= 0) {
  21. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  22. }
  23. }
  24. })
  25. return
  26. }
  27. LODOP.PRINT_INIT('')
  28. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  29. LODOP.SET_PRINT_MODE('POS_BASEON_PAPER', true) // 可使输出以纸张边缘为基点
  30. LODOP.ADD_PRINT_HTML(0, 0, '100%', '100%', html)
  31. LODOP.ADD_PRINT_BARCODE('36%', '65%', 90, 90, 'QRCode', data.qrCodeContent)
  32. LODOP.SET_PRINT_STYLEA(0, 'QRCodeVersion', 5)
  33. // LODOP.SET_PRINT_STYLEA(0,"QRCodeErrorLevel",'H')
  34. LODOP.SET_PRINT_COPIES(data.printQty)// 指定份数
  35. LODOP.SET_PRINT_PAGESIZE(1, width, height)
  36. LODOP.PRINT()
  37. }
  38. // 导出文件
  39. export const exportExcel = function (url, params, fileName, callback, noShowTime) {
  40. url(params).then(res => {
  41. if (res.type == 'application/json') {
  42. var reader = new FileReader()
  43. reader.addEventListener('loadend', function () {
  44. const obj = JSON.parse(reader.result)
  45. notification.error({
  46. message: '提示',
  47. description: obj.message
  48. })
  49. })
  50. reader.readAsText(res)
  51. } else {
  52. downloadExcel(res, fileName, noShowTime)
  53. }
  54. callback()
  55. })
  56. }
  57. // 下载excel
  58. export const downloadExcel = function (data, fileName, noShowTime) {
  59. if (!data) { return }
  60. const a = moment().format('YYYYMMDDHHmmss')
  61. const fname = noShowTime ? fileName : (fileName + a)
  62. const blob = new Blob([data], { type: 'application/vnd.ms-excel' })
  63. if (window.navigator && window.navigator.msSaveOrOpenBlob) {
  64. navigator.msSaveBlob(blob, fname + '.xlsx')
  65. } else {
  66. const link = document.createElement('a')
  67. link.style.display = 'none'
  68. var href = URL.createObjectURL(blob)
  69. link.href = href
  70. link.setAttribute('download', fname + '.xlsx')
  71. document.body.appendChild(link)
  72. link.click()
  73. document.body.removeChild(link)
  74. window.URL.revokeObjectURL(href) // 释放掉blob对象
  75. }
  76. }
  77. // pdf blob 转 base64
  78. export const blobToBaseByPdf = function (data, callback) {
  79. const reader = new FileReader()
  80. reader.readAsDataURL(new Blob([data], { type: 'application/pdf' }))
  81. reader.addEventListener('load', () => {
  82. callback(reader.result)
  83. })
  84. }
  85. // 打印html
  86. export const jGPrint = function (data, type, callback, printLogParams, printPageSize) {
  87. if (!data) {
  88. return
  89. }
  90. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  91. if (!LODOP) {
  92. confirm({
  93. title: '提示?',
  94. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  95. okText: '立即下载',
  96. okType: 'danger',
  97. cancelText: '暂不打印',
  98. onOk () {
  99. var agent = navigator.userAgent.toLowerCase()
  100. if (agent.indexOf('win32') >= 0 || agent.indexOf('wow32') >= 0) {
  101. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  102. } else if (agent.indexOf('win64') >= 0 || agent.indexOf('wow64') >= 0) {
  103. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  104. }
  105. }
  106. })
  107. return
  108. }
  109. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  110. // 执行该语句之后,PRINT指令不再返回那个所谓“打印成功”,才能获取到打印状态
  111. LODOP.SET_PRINT_MODE('CATCH_PRINT_STATUS', true)
  112. // TaskID:任务id,Value:job代码
  113. LODOP.On_Return = function (TaskID, Value) {
  114. console.log(TaskID, Value)
  115. // 已打印
  116. if (Value && printLogParams) {
  117. console.log('已打印,统计打印次数')
  118. printLog(printLogParams, callback)
  119. } else {
  120. callback()
  121. }
  122. }
  123. if (printPageSize) {
  124. LODOP.ADD_PRINT_HTM(0, 0, '100%', '99%', data)
  125. LODOP.SET_PRINT_PAGESIZE(3, '210mm', 100, '')
  126. } else {
  127. LODOP.ADD_PRINT_HTM('1cm', 0, '100%', '99%', data)
  128. LODOP.SET_SHOW_MODE('BKIMG_PRINT', 0)
  129. }
  130. if (type == 'preview') { // 预览
  131. LODOP.PREVIEW()
  132. } else if (type == 'print') { // 打印
  133. LODOP.PRINTA()
  134. }
  135. }
  136. // 浏览器打印pdf功能
  137. export const winPrintPdf = function (data, type, callback) {
  138. if (!data) {
  139. return
  140. }
  141. const url = window.URL.createObjectURL(new Blob([data], { type: 'application/pdf' }))
  142. document.getElementById('print').innerHTML = '<iframe id="printfbod" name="printfbod" src="' + url + '" hidden></iframe>'
  143. window.frames['printfbod'].onload = function () {
  144. callback()
  145. }
  146. if (type == 'orginPrint') {
  147. window.frames['printfbod'].focus()
  148. window.frames['printfbod'].print()
  149. } else {
  150. window.open(url)
  151. }
  152. }
  153. // 打印
  154. export const printFun = function (url, params, type, taskName, callback, printLogParams, hidePrint) {
  155. url(params).then(res => {
  156. if (res.type == 'application/json') {
  157. var reader = new FileReader()
  158. reader.addEventListener('loadend', function () {
  159. const obj = JSON.parse(reader.result)
  160. notification.error({
  161. message: '提示',
  162. description: obj.message
  163. })
  164. })
  165. reader.readAsText(res)
  166. } else {
  167. console.log(res, 'printFun')
  168. // 使用浏览器自带打印功能
  169. if (type == 'orginPrint' || type == 'orginPreview') {
  170. winPrintPdf(res, type, callback)
  171. } else {
  172. jGPrintPdf(res, type, taskName, printLogParams, callback, hidePrint)
  173. }
  174. }
  175. })
  176. }
  177. // 获取系统信息
  178. export const getSystemInfo = function (strINFOType, callback) {
  179. LODOP = getLodop()
  180. if (LODOP.CVERSION) CLODOP.On_Return = function (TaskID, Value) { callback(Value) }
  181. var strResult = LODOP.GET_SYSTEM_INFO(strINFOType)
  182. if (!LODOP.CVERSION) return strResult; else return ''
  183. }
  184. // 打印记录保存
  185. export const printLog = function (data, callback) {
  186. getSystemInfo('NetworkAdapter.1.IPAddress', function (ret) {
  187. // 批量处理
  188. if (data instanceof Array) {
  189. data.map(item => { item.printIp = ret })
  190. printLogSaveBatch(data).then(res => {
  191. callback(res)
  192. })
  193. } else {
  194. // 单条处理
  195. data.printIp = ret
  196. printLogSave(data).then(res => {
  197. callback(res)
  198. })
  199. }
  200. })
  201. }
  202. // 打印pdf
  203. export const jGPrintPdf = function (data, type, taskName, printLogParams, callback, hidePrint) {
  204. if (!data) {
  205. return
  206. }
  207. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  208. if (!LODOP) {
  209. confirm({
  210. title: '提示?',
  211. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  212. okText: '立即下载',
  213. okType: 'danger',
  214. cancelText: '暂不打印',
  215. onOk () {
  216. var agent = navigator.userAgent.toLowerCase()
  217. if (agent.indexOf('win32') >= 0 || agent.indexOf('wow32') >= 0) {
  218. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  219. } else if (agent.indexOf('win64') >= 0 || agent.indexOf('wow64') >= 0) {
  220. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  221. }
  222. }
  223. })
  224. return
  225. }
  226. blobToBaseByPdf(data, function (dataurl) {
  227. // console.log(dataurl)
  228. LODOP.PRINT_INIT(taskName)
  229. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  230. // 执行该语句之后,PRINT指令不再返回那个所谓“打印成功”,才能获取到打印状态
  231. LODOP.SET_PRINT_MODE('CATCH_PRINT_STATUS', true)
  232. // 隐藏打印按钮
  233. if (hidePrint) {
  234. LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true)
  235. }
  236. // TaskID:任务id,Value:job代码
  237. LODOP.On_Return = function (TaskID, Value) {
  238. console.log(TaskID, Value)
  239. // 已打印
  240. if (Value && printLogParams) {
  241. console.log('已打印,统计打印次数')
  242. printLog(printLogParams, callback)
  243. } else {
  244. callback()
  245. }
  246. }
  247. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', dataurl.replace('data:application/pdf;base64,', ''))
  248. LODOP.SET_PRINT_PAGESIZE(3, 2090, 30, '')
  249. if (type == 'preview') { // 预览
  250. LODOP.PREVIEW()
  251. } else if (type == 'print') { // 打印
  252. LODOP.PRINTA()
  253. }
  254. })
  255. }
  256. // 获取打印机状态
  257. export const getStatusValue = function (ValueType, ValueIndex, callback) {
  258. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  259. if (LODOP.CVERSION) {
  260. LODOP.On_Return = function (TaskID, Value) {
  261. callback(Value)
  262. }
  263. }
  264. var strResult = LODOP.GET_VALUE(ValueType, ValueIndex)
  265. if (!LODOP.CVERSION) return callback(strResult); else return callback('')
  266. }