JGPrint.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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.SET_PRINT_PAGESIZE(1,'210mm','297mm',"");
  128. LODOP.ADD_PRINT_HTM(0, 0, '100%', '99%', data)
  129. LODOP.SET_SHOW_MODE('BKIMG_PRINT', 0)
  130. }
  131. if (type == 'preview') { // 预览
  132. LODOP.PREVIEW()
  133. } else if (type == 'print') { // 打印
  134. LODOP.PRINTA()
  135. }
  136. }
  137. // 批量打印销售收款单html
  138. export const jGPlPrint = function (data, type, callback, printLogParams, orginData) {
  139. if (!data) {
  140. return
  141. }
  142. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  143. if (!LODOP) {
  144. confirm({
  145. title: '提示?',
  146. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  147. okText: '立即下载',
  148. okType: 'danger',
  149. cancelText: '暂不打印',
  150. onOk () {
  151. var agent = navigator.userAgent.toLowerCase()
  152. if (agent.indexOf('win32') >= 0 || agent.indexOf('wow32') >= 0) {
  153. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  154. } else if (agent.indexOf('win64') >= 0 || agent.indexOf('wow64') >= 0) {
  155. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  156. }
  157. }
  158. })
  159. return
  160. }
  161. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  162. // 执行该语句之后,PRINT指令不再返回那个所谓“打印成功”,才能获取到打印状态
  163. LODOP.SET_PRINT_MODE('CATCH_PRINT_STATUS', true)
  164. // TaskID:任务id,Value:job代码
  165. LODOP.On_Return = function (TaskID, Value) {
  166. console.log(TaskID, Value)
  167. // 已打印
  168. if (Value && printLogParams) {
  169. console.log('已打印,统计打印次数')
  170. printLog(printLogParams, callback)
  171. } else {
  172. callback()
  173. }
  174. }
  175. LODOP.SET_PRINT_PAGESIZE(1,'210mm','297mm',"");
  176. LODOP.SET_SHOW_MODE('BKIMG_PRINT', 0)
  177. // 循环页
  178. for(var i=0;i<data.length;i=i+2){
  179. if(i%2==0){
  180. LODOP.NewPageA();
  181. }
  182. LODOP.ADD_PRINT_HTM(0, 0, '100%', '99%', data[i]+(data[i+1]||''))
  183. }
  184. if (type == 'preview') { // 预览
  185. LODOP.PREVIEW()
  186. } else if (type == 'print') { // 打印
  187. LODOP.PRINTA()
  188. }
  189. }
  190. // 浏览器打印pdf功能
  191. export const winPrintPdf = function (data, type, callback) {
  192. if (!data) {
  193. return
  194. }
  195. const url = window.URL.createObjectURL(new Blob([data], { type: 'application/pdf' }))
  196. document.getElementById('print').innerHTML = '<iframe id="printfbod" name="printfbod" src="' + url + '" hidden></iframe>'
  197. window.frames['printfbod'].onload = function () {
  198. callback()
  199. }
  200. if (type == 'orginPrint') {
  201. window.frames['printfbod'].focus()
  202. window.frames['printfbod'].print()
  203. } else {
  204. window.open(url)
  205. }
  206. }
  207. // 打印
  208. export const printFun = function (url, params, type, taskName, callback, printLogParams, hidePrint) {
  209. url(params).then(res => {
  210. if (res.type == 'application/json') {
  211. var reader = new FileReader()
  212. reader.addEventListener('loadend', function () {
  213. const obj = JSON.parse(reader.result)
  214. notification.error({
  215. message: '提示',
  216. description: obj.message
  217. })
  218. })
  219. reader.readAsText(res)
  220. } else {
  221. console.log(res, 'printFun')
  222. // 使用浏览器自带打印功能
  223. if (type == 'orginPrint' || type == 'orginPreview') {
  224. winPrintPdf(res, type, callback)
  225. } else {
  226. jGPrintPdf(res, type, taskName, printLogParams, callback, hidePrint)
  227. }
  228. }
  229. })
  230. }
  231. // 获取系统信息
  232. export const getSystemInfo = function (strINFOType, callback) {
  233. LODOP = getLodop()
  234. if (LODOP.CVERSION) CLODOP.On_Return = function (TaskID, Value) { callback(Value) }
  235. var strResult = LODOP.GET_SYSTEM_INFO(strINFOType)
  236. if (!LODOP.CVERSION) return strResult; else return ''
  237. }
  238. // 打印记录保存
  239. export const printLog = function (data, callback) {
  240. getSystemInfo('NetworkAdapter.1.IPAddress', function (ret) {
  241. // 批量处理
  242. if (data instanceof Array) {
  243. data.map(item => { item.printIp = ret })
  244. printLogSaveBatch(data).then(res => {
  245. callback(res)
  246. })
  247. } else {
  248. // 单条处理
  249. data.printIp = ret
  250. printLogSave(data).then(res => {
  251. callback(res)
  252. })
  253. }
  254. })
  255. }
  256. // 打印pdf
  257. export const jGPrintPdf = function (data, type, taskName, printLogParams, callback, hidePrint) {
  258. if (!data) {
  259. return
  260. }
  261. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  262. if (!LODOP) {
  263. confirm({
  264. title: '提示?',
  265. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  266. okText: '立即下载',
  267. okType: 'danger',
  268. cancelText: '暂不打印',
  269. onOk () {
  270. var agent = navigator.userAgent.toLowerCase()
  271. if (agent.indexOf('win32') >= 0 || agent.indexOf('wow32') >= 0) {
  272. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  273. } else if (agent.indexOf('win64') >= 0 || agent.indexOf('wow64') >= 0) {
  274. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  275. }
  276. }
  277. })
  278. return
  279. }
  280. blobToBaseByPdf(data, function (dataurl) {
  281. // console.log(dataurl)
  282. LODOP.PRINT_INIT(taskName)
  283. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  284. // 执行该语句之后,PRINT指令不再返回那个所谓“打印成功”,才能获取到打印状态
  285. LODOP.SET_PRINT_MODE('CATCH_PRINT_STATUS', true)
  286. // 隐藏打印按钮
  287. if (hidePrint) {
  288. LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true)
  289. }
  290. // TaskID:任务id,Value:job代码
  291. LODOP.On_Return = function (TaskID, Value) {
  292. console.log(TaskID, Value)
  293. // 已打印
  294. if (Value && printLogParams) {
  295. console.log('已打印,统计打印次数')
  296. printLog(printLogParams, callback)
  297. } else {
  298. callback()
  299. }
  300. }
  301. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', dataurl.replace('data:application/pdf;base64,', ''))
  302. LODOP.SET_PRINT_PAGESIZE(3, 2090, 30, '')
  303. if (type == 'preview') { // 预览
  304. LODOP.PREVIEW()
  305. } else if (type == 'print') { // 打印
  306. LODOP.PRINTA()
  307. }
  308. })
  309. }
  310. // 获取打印机状态
  311. export const getStatusValue = function (ValueType, ValueIndex, callback) {
  312. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  313. if (LODOP.CVERSION) {
  314. LODOP.On_Return = function (TaskID, Value) {
  315. callback(Value)
  316. }
  317. }
  318. var strResult = LODOP.GET_VALUE(ValueType, ValueIndex)
  319. if (!LODOP.CVERSION) return callback(strResult); else return callback('')
  320. }