JGPrint.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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, printLogParams)
  115. // 已打印
  116. if (Value) {
  117. if(printLogParams){
  118. console.log('已打印,统计打印次数')
  119. printLog(printLogParams, callback)
  120. }else{
  121. callback({status:200,message:'已打印'})
  122. }
  123. } else {
  124. // 取消打印
  125. callback()
  126. }
  127. }
  128. if (printPageSize) {
  129. LODOP.SET_PRINT_PAGESIZE(3, '210mm', 100, '')
  130. } else {
  131. LODOP.SET_SHOW_MODE('BKIMG_PRINT', 0)
  132. LODOP.SET_PRINT_PAGESIZE(1,'210mm','297mm',"");
  133. }
  134. LODOP.ADD_PRINT_HTM(0, 0, '100%', '99%', data)
  135. if (type == 'preview') { // 预览
  136. LODOP.PREVIEW()
  137. } else if (type == 'print') { // 打印
  138. LODOP.PRINTA()
  139. }
  140. }
  141. // 自动排版, 遍历数组,将数组中每一个数值加起来,如果和小于某个数,则将这些数值存放到一个新数组中。
  142. export const groupLessThan = function(html,isNoMerge) {
  143. const pageMidHeight = 530 // 半页高度
  144. const pageHeight = pageMidHeight*2 // 页高度
  145. const arr = []
  146. const result = []
  147. for (let i = 0; i < html.length; i++) {
  148. const h = html[i].clientHeight // 总高度
  149. const pz = h<=pageMidHeight ? '0.5' : (h/pageHeight).toFixed(2) // 页数
  150. const t = '<div style="height:'+(pz<=0.5?pageMidHeight:'auto')+'px'+';border:1px solid #fff;">'+html[i].innerHTML+'</div>'
  151. arr.push({
  152. h: h,
  153. pz: pz,
  154. t: t,
  155. flag: 0
  156. })
  157. // 不合并纸张打印
  158. if(isNoMerge){
  159. result.push(t)
  160. }
  161. }
  162. // 不合并纸张打印
  163. if(isNoMerge){
  164. return result
  165. }
  166. console.log(arr)
  167. // 找出所有小于半张纸的
  168. const a = arr.filter(item => item.pz <= 0.5)
  169. // 先找出所有超过半张纸的,小于一张纸的
  170. const b = arr.filter(item => item.pz >0.5 && item.pz <= 1)
  171. // 找出所有超过一张纸的
  172. const c = arr.filter(item => item.pz > 1)
  173. // 先给c 找匹配的另一半
  174. for(let i=0;i<c.length;i++){
  175. const pz = Number(c[i].pz.split('.')[1])
  176. // 先从b中找
  177. const hasOther = b.findIndex(item => (item.pz + pz) < 1)
  178. if(hasOther>=0){
  179. result.push(c[i].t + b[hasOther].t)
  180. // 删除hasOther
  181. b.splice(hasOther,1)
  182. }else{
  183. // 再从a中找
  184. const hasOtherA = a.findIndex(item => (item.pz + pz) < 1)
  185. if(hasOtherA>=0){
  186. result.push(c[i].t + a[hasOtherA].t)
  187. // 删除hasOtherA
  188. b.splice(hasOther,1)
  189. }else{
  190. // 都没匹配到
  191. result.push(c[i].t)
  192. }
  193. }
  194. }
  195. // 将b依次加入
  196. for(let i=0;i<b.length;i++){
  197. result.push(b[i].t)
  198. }
  199. // 将a中每2个放到一个容器中
  200. if(a.length%2!=0){
  201. a.push({t:''})
  202. }
  203. for(let i=0;i<a.length;i=i+2){
  204. result.push(a[i].t + a[i+1].t)
  205. }
  206. // console.log(a)
  207. // console.log(b)
  208. // console.log(c)
  209. // console.log(result)
  210. return result
  211. }
  212. // 批量打印销售收款单html
  213. export const jGPlPrint = function (data, type, callback, printLogParams, taskName) {
  214. if (!data) {
  215. return
  216. }
  217. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  218. if (!LODOP) {
  219. confirm({
  220. title: '提示?',
  221. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  222. okText: '立即下载',
  223. okType: 'danger',
  224. cancelText: '暂不打印',
  225. onOk () {
  226. var agent = navigator.userAgent.toLowerCase()
  227. if (agent.indexOf('win32') >= 0 || agent.indexOf('wow32') >= 0) {
  228. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  229. } else if (agent.indexOf('win64') >= 0 || agent.indexOf('wow64') >= 0) {
  230. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  231. }
  232. }
  233. })
  234. return
  235. }
  236. LODOP.PRINT_INIT(taskName)
  237. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  238. // 执行该语句之后,PRINT指令不再返回那个所谓“打印成功”,才能获取到打印状态
  239. // LODOP.SET_PRINT_MODE('CATCH_PRINT_STATUS', true)
  240. // TaskID:任务id,Value:job代码
  241. LODOP.On_Return = function (TaskID, Value) {
  242. console.log(TaskID, Value!=0, printLogParams)
  243. // 已打印
  244. if (Number(Value)) {
  245. if(printLogParams){
  246. console.log('已打印,统计打印次数')
  247. printLog(printLogParams, callback)
  248. }else{
  249. callback({status:200,message:'已打印'})
  250. }
  251. } else {
  252. // 取消打印
  253. callback()
  254. }
  255. }
  256. LODOP.SET_PRINT_PAGESIZE(1,'210mm','297mm',"");
  257. LODOP.SET_SHOW_MODE('BKIMG_PRINT', 0)
  258. if(data.length>1){
  259. // 循环页
  260. for(var i=0;i<data.length;i++){
  261. LODOP.NewPageA();
  262. LODOP.ADD_PRINT_HTM(0, 0, '100%', '99%', data[i])
  263. }
  264. }else{
  265. LODOP.ADD_PRINT_HTM(0, 0, '100%', '99%', data[0])
  266. }
  267. if (type == 'preview') { // 预览
  268. LODOP.PREVIEW()
  269. } else if (type == 'print') { // 打印
  270. LODOP.PRINTA()
  271. }
  272. }
  273. // 浏览器打印pdf功能
  274. export const winPrintPdf = function (data, type, callback) {
  275. if (!data) {
  276. return
  277. }
  278. const url = window.URL.createObjectURL(new Blob([data], { type: 'application/pdf' }))
  279. document.getElementById('print').innerHTML = '<iframe id="printfbod" name="printfbod" src="' + url + '" hidden></iframe>'
  280. window.frames['printfbod'].onload = function () {
  281. callback()
  282. }
  283. if (type == 'orginPrint') {
  284. window.frames['printfbod'].focus()
  285. window.frames['printfbod'].print()
  286. } else {
  287. window.open(url)
  288. }
  289. }
  290. // 打印
  291. export const printFun = function (url, params, type, taskName, callback, printLogParams, hidePrint) {
  292. url(params).then(res => {
  293. if (res.type == 'application/json') {
  294. var reader = new FileReader()
  295. reader.addEventListener('loadend', function () {
  296. const obj = JSON.parse(reader.result)
  297. notification.error({
  298. message: '提示',
  299. description: obj.message
  300. })
  301. })
  302. reader.readAsText(res)
  303. } else {
  304. console.log(res, 'printFun')
  305. // 使用浏览器自带打印功能
  306. if (type == 'orginPrint' || type == 'orginPreview') {
  307. winPrintPdf(res, type, callback)
  308. } else {
  309. jGPrintPdf(res, type, taskName, printLogParams, callback, hidePrint)
  310. }
  311. }
  312. })
  313. }
  314. // 获取系统信息
  315. export const getSystemInfo = function (strINFOType, callback) {
  316. LODOP = getLodop()
  317. if (LODOP.CVERSION) CLODOP.On_Return = function (TaskID, Value) { callback(Value) }
  318. var strResult = LODOP.GET_SYSTEM_INFO(strINFOType)
  319. if (!LODOP.CVERSION) return strResult; else return ''
  320. }
  321. // 打印记录保存
  322. export const printLog = function (data, callback) {
  323. getSystemInfo('NetworkAdapter.1.IPAddress', function (ret) {
  324. // 批量处理
  325. if (data instanceof Array) {
  326. data.map(item => { item.printIp = ret })
  327. printLogSaveBatch(data).then(res => {
  328. callback(res)
  329. })
  330. } else {
  331. // 单条处理
  332. data.printIp = ret
  333. printLogSave(data).then(res => {
  334. callback(res)
  335. })
  336. }
  337. })
  338. }
  339. // 打印pdf
  340. export const jGPrintPdf = function (data, type, taskName, printLogParams, callback, hidePrint) {
  341. if (!data) {
  342. return
  343. }
  344. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  345. if (!LODOP) {
  346. confirm({
  347. title: '提示?',
  348. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  349. okText: '立即下载',
  350. okType: 'danger',
  351. cancelText: '暂不打印',
  352. onOk () {
  353. var agent = navigator.userAgent.toLowerCase()
  354. if (agent.indexOf('win32') >= 0 || agent.indexOf('wow32') >= 0) {
  355. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  356. } else if (agent.indexOf('win64') >= 0 || agent.indexOf('wow64') >= 0) {
  357. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  358. }
  359. }
  360. })
  361. return
  362. }
  363. blobToBaseByPdf(data, function (dataurl) {
  364. // console.log(dataurl)
  365. LODOP.PRINT_INIT(taskName)
  366. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  367. // 执行该语句之后,PRINT指令不再返回那个所谓“打印成功”,才能获取到打印状态
  368. // LODOP.SET_PRINT_MODE('CATCH_PRINT_STATUS', true)
  369. // 隐藏打印按钮
  370. if (hidePrint) {
  371. LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true)
  372. }
  373. // TaskID:任务id,Value:job代码
  374. LODOP.On_Return = function (TaskID, Value) {
  375. console.log(TaskID, Value!=0, printLogParams)
  376. // 已打印
  377. if (Value!=0) {
  378. if(printLogParams){
  379. console.log('已打印,统计打印次数')
  380. printLog(printLogParams, callback)
  381. }else{
  382. callback({status:200,message:'打印已完成'})
  383. }
  384. } else {
  385. callback()
  386. }
  387. }
  388. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', dataurl.replace('data:application/pdf;base64,', ''))
  389. if (type == 'preview') { // 预览
  390. LODOP.PREVIEW()
  391. } else if (type == 'print') { // 打印
  392. LODOP.PRINTA()
  393. }
  394. })
  395. }
  396. // printBase64Fun
  397. export const printBase64Fun = function (url, params, type, taskName, callback, printLogParams, hidePrint) {
  398. url(params).then(res => {
  399. if (res.type == 'application/json') {
  400. var reader = new FileReader()
  401. reader.addEventListener('loadend', function () {
  402. const obj = JSON.parse(reader.result)
  403. notification.error({
  404. message: '提示',
  405. description: obj.message
  406. })
  407. })
  408. reader.readAsText(res)
  409. } else {
  410. console.log(res, 'printFun')
  411. // 使用浏览器自带打印功能
  412. if (type == 'orginPrint' || type == 'orginPreview') {
  413. winPrintPdf(res, type, callback)
  414. } else {
  415. jGPrintBase64Pdf(res.data, type, taskName, printLogParams, callback, hidePrint)
  416. }
  417. }
  418. })
  419. }
  420. // 打印pdf
  421. export const jGPrintBase64Pdf = function (dataurl, type, taskName, printLogParams, callback, hidePrint) {
  422. if (!dataurl) {
  423. return
  424. }
  425. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  426. if (!LODOP) {
  427. confirm({
  428. title: '提示?',
  429. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  430. okText: '立即下载',
  431. okType: 'danger',
  432. cancelText: '暂不打印',
  433. onOk () {
  434. var agent = navigator.userAgent.toLowerCase()
  435. if (agent.indexOf('win32') >= 0 || agent.indexOf('wow32') >= 0) {
  436. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  437. } else if (agent.indexOf('win64') >= 0 || agent.indexOf('wow64') >= 0) {
  438. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  439. }
  440. }
  441. })
  442. return
  443. }
  444. LODOP.PRINT_INIT(taskName)
  445. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  446. // 执行该语句之后,PRINT指令不再返回那个所谓“打印成功”,才能获取到打印状态
  447. // LODOP.SET_PRINT_MODE('CATCH_PRINT_STATUS', true)
  448. // 隐藏打印按钮
  449. if (hidePrint) {
  450. LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true)
  451. }
  452. // TaskID:任务id,Value:job代码
  453. LODOP.On_Return = function (TaskID, Value) {
  454. console.log(TaskID, Value!=0, printLogParams)
  455. // 已打印
  456. if (Value!=0) {
  457. if(printLogParams){
  458. console.log('已打印,统计打印次数')
  459. printLog(printLogParams, callback)
  460. }else{
  461. callback({status:200,message:'打印已完成'})
  462. }
  463. } else {
  464. callback()
  465. }
  466. }
  467. for(let i=0;i<dataurl.length;i++){
  468. LODOP.NewPageA()
  469. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', dataurl[i])
  470. }
  471. if (type == 'preview') { // 预览
  472. LODOP.PREVIEW()
  473. } else if (type == 'print') { // 打印
  474. LODOP.PRINTA()
  475. }
  476. }
  477. // 获取打印机状态
  478. export const getStatusValue = function (ValueType, ValueIndex, callback) {
  479. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  480. if (LODOP.CVERSION) {
  481. LODOP.On_Return = function (TaskID, Value) {
  482. callback(Value)
  483. }
  484. }
  485. var strResult = LODOP.GET_VALUE(ValueType, ValueIndex)
  486. if (!LODOP.CVERSION) return callback(strResult); else return callback('')
  487. }