JGPrint.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 } 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){
  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)
  53. }
  54. callback()
  55. })
  56. }
  57. // 下载excel
  58. export const downloadExcel = function (data, fileName) {
  59. if (!data) { return }
  60. const a = moment().format('YYYYMMDDHHmmss')
  61. const fname = 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) {
  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. }
  120. };
  121. LODOP.ADD_PRINT_HTM(0, 0, '100%', '100%', data)
  122. LODOP.SET_PRINT_PAGESIZE(3,2100,100,"");
  123. if (type == 'preview') { // 预览
  124. LODOP.PREVIEW()
  125. } else if (type == 'print') { // 打印
  126. LODOP.PRINTA()
  127. }
  128. }
  129. // 打印
  130. export const printFun = function(url,params,type,taskName,callback,printLogParams){
  131. url(params).then(res => {
  132. if (res.type == 'application/json') {
  133. var reader = new FileReader()
  134. reader.addEventListener('loadend', function () {
  135. const obj = JSON.parse(reader.result)
  136. notification.error({
  137. message: '提示',
  138. description: obj.message
  139. })
  140. })
  141. reader.readAsText(res)
  142. } else {
  143. console.log(res,'printFun')
  144. jGPrintPdf(res,type,taskName,printLogParams,callback)
  145. }
  146. })
  147. }
  148. // 获取系统信息
  149. export const getSystemInfo = function (strINFOType,callback){
  150. LODOP=getLodop();
  151. if (LODOP.CVERSION) CLODOP.On_Return=function(TaskID,Value){callback(Value)};
  152. var strResult=LODOP.GET_SYSTEM_INFO(strINFOType);
  153. if (!LODOP.CVERSION) return strResult; else return "";
  154. }
  155. // 打印记录保存
  156. export const printLog = function(data,callback){
  157. getSystemInfo('NetworkAdapter.1.IPAddress',function(ret){
  158. data.printIp = ret
  159. printLogSave(data).then(res => {
  160. callback(res)
  161. })
  162. })
  163. }
  164. // 打印pdf
  165. export const jGPrintPdf = function (data, type, taskName, printLogParams,callback) {
  166. if (!data) {
  167. return
  168. }
  169. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  170. if (!LODOP) {
  171. confirm({
  172. title: '提示?',
  173. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  174. okText: '立即下载',
  175. okType: 'danger',
  176. cancelText: '暂不打印',
  177. onOk () {
  178. var agent = navigator.userAgent.toLowerCase();
  179. if (agent.indexOf("win32") >= 0 || agent.indexOf("wow32") >= 0) {
  180. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  181. }else if (agent.indexOf("win64") >= 0 || agent.indexOf("wow64") >= 0) {
  182. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  183. }
  184. }
  185. })
  186. return
  187. }
  188. blobToBaseByPdf(data, function (dataurl) {
  189. // console.log(dataurl)
  190. LODOP.PRINT_INIT(taskName)
  191. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  192. //执行该语句之后,PRINT指令不再返回那个所谓“打印成功”,才能获取到打印状态
  193. LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS",true);
  194. //TaskID:任务id,Value:job代码
  195. LODOP.On_Return=function(TaskID,Value){
  196. console.log(TaskID,Value)
  197. // 已打印
  198. if(Value&&printLogParams){
  199. console.log('已打印,统计打印次数')
  200. printLog(printLogParams,callback)
  201. }
  202. };
  203. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', dataurl.replace('data:application/pdf;base64,', ''))
  204. LODOP.SET_PRINT_PAGESIZE(3,2090,30,"");
  205. if (type == 'preview') { // 预览
  206. LODOP.PREVIEW()
  207. } else if (type == 'print') { // 打印
  208. LODOP.PRINTA()
  209. }
  210. })
  211. }
  212. // 获取打印机状态
  213. export const getStatusValue = function (ValueType,ValueIndex,callback){
  214. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  215. if (LODOP.CVERSION) {
  216. LODOP.On_Return=function(TaskID,Value){
  217. callback(Value)
  218. };
  219. }
  220. var strResult=LODOP.GET_VALUE(ValueType,ValueIndex);
  221. if (!LODOP.CVERSION) return callback(strResult); else return callback('');
  222. }