JGPrint.js 8.3 KB

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