JGPrint.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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('https://www.lodop.net/download/CLodop_Setup_for_Win64NT_6.611EN.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. // 使用浏览器自带打印功能
  305. if (type == 'orginPrint' || type == 'orginPreview') {
  306. winPrintPdf(res, type, callback)
  307. } else {
  308. jGPrintPdf(res, type, taskName, printLogParams, callback, hidePrint)
  309. }
  310. }
  311. })
  312. }
  313. // 获取系统信息
  314. export const getSystemInfo = function (strINFOType, callback) {
  315. LODOP = getLodop()
  316. if (LODOP.CVERSION) CLODOP.On_Return = function (TaskID, Value) { callback(Value) }
  317. var strResult = LODOP.GET_SYSTEM_INFO(strINFOType)
  318. if (!LODOP.CVERSION) return strResult; else return ''
  319. }
  320. // 打印记录保存
  321. export const printLog = function (data, callback) {
  322. getSystemInfo('NetworkAdapter.1.IPAddress', function (ret) {
  323. // 批量处理
  324. if (data instanceof Array) {
  325. data.map(item => { item.printIp = ret })
  326. printLogSaveBatch(data).then(res => {
  327. callback(res)
  328. })
  329. } else {
  330. // 单条处理
  331. data.printIp = ret
  332. printLogSave(data).then(res => {
  333. callback(res)
  334. })
  335. }
  336. })
  337. }
  338. // 打印pdf
  339. export const jGPrintPdf = function (data, type, taskName, printLogParams, callback, hidePrint) {
  340. if (!data) {
  341. return
  342. }
  343. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  344. if (!LODOP) {
  345. confirm({
  346. title: '提示?',
  347. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  348. okText: '立即下载',
  349. okType: 'danger',
  350. cancelText: '暂不打印',
  351. onOk () {
  352. var agent = navigator.userAgent.toLowerCase()
  353. if (agent.indexOf('win32') >= 0 || agent.indexOf('wow32') >= 0) {
  354. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  355. } else if (agent.indexOf('win64') >= 0 || agent.indexOf('wow64') >= 0) {
  356. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  357. }
  358. }
  359. })
  360. return
  361. }
  362. blobToBaseByPdf(data, function (dataurl) {
  363. // console.log(dataurl)
  364. LODOP.PRINT_INIT(taskName)
  365. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  366. // 执行该语句之后,PRINT指令不再返回那个所谓“打印成功”,才能获取到打印状态
  367. // LODOP.SET_PRINT_MODE('CATCH_PRINT_STATUS', true)
  368. // 隐藏打印按钮
  369. if (hidePrint) {
  370. LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true)
  371. }
  372. // TaskID:任务id,Value:job代码
  373. LODOP.On_Return = function (TaskID, Value) {
  374. console.log(TaskID, Value!=0, printLogParams)
  375. // 已打印
  376. if (Value!=0) {
  377. if(printLogParams){
  378. console.log('已打印,统计打印次数')
  379. printLog(printLogParams, callback)
  380. }else{
  381. callback({status:200,message:'打印已完成'})
  382. }
  383. } else {
  384. callback()
  385. }
  386. }
  387. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', dataurl.replace('data:application/pdf;base64,', ''))
  388. if (type == 'preview') { // 预览
  389. LODOP.PREVIEW()
  390. } else if (type == 'print') { // 打印
  391. LODOP.PRINTA()
  392. }
  393. })
  394. }
  395. // printBase64Fun
  396. export const printBase64Fun = function (url, params, type, taskName, callback, printLogParams, hidePrint) {
  397. url(params).then(res => {
  398. if (res.type == 'application/json') {
  399. var reader = new FileReader()
  400. reader.addEventListener('loadend', function () {
  401. const obj = JSON.parse(reader.result)
  402. notification.error({
  403. message: '提示',
  404. description: obj.message
  405. })
  406. })
  407. reader.readAsText(res)
  408. } else {
  409. // 使用浏览器自带打印功能
  410. if (type == 'orginPrint' || type == 'orginPreview') {
  411. winPrintPdf(res, type, callback)
  412. } else {
  413. jGPrintBase64Pdf(res.data, type, taskName, printLogParams, callback, hidePrint)
  414. }
  415. }
  416. })
  417. }
  418. // 打印pdf
  419. export const jGPrintBase64Pdf = function (dataurl, type, taskName, printLogParams, callback, hidePrint) {
  420. if (!dataurl) {
  421. return
  422. }
  423. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  424. if (!LODOP) {
  425. confirm({
  426. title: '提示?',
  427. content: h => <div>打印控件未安装,请先下载并安装。安装完成后,刷新页面即可打印。</div>,
  428. okText: '立即下载',
  429. okType: 'danger',
  430. cancelText: '暂不打印',
  431. onOk () {
  432. var agent = navigator.userAgent.toLowerCase()
  433. if (agent.indexOf('win32') >= 0 || agent.indexOf('wow32') >= 0) {
  434. window.open('http://www.lodop.net/demolist/CLodop_Setup_for_Win32NT.zip')
  435. } else if (agent.indexOf('win64') >= 0 || agent.indexOf('wow64') >= 0) {
  436. window.open('http://www.lodop.net/download/CLodop_Setup_for_Win64NT_4.155EN.zip')
  437. }
  438. }
  439. })
  440. return
  441. }
  442. LODOP.PRINT_INIT(taskName)
  443. LODOP.SET_SHOW_MODE('HIDE_PAPER_BOARD', 1) // 隐藏底图上有模拟走纸板的条纹线
  444. // 执行该语句之后,PRINT指令不再返回那个所谓“打印成功”,才能获取到打印状态
  445. // LODOP.SET_PRINT_MODE('CATCH_PRINT_STATUS', true)
  446. // 隐藏打印按钮
  447. if (hidePrint) {
  448. LODOP.SET_SHOW_MODE('HIDE_PBUTTIN_PREVIEW', true)
  449. }
  450. // TaskID:任务id,Value:job代码
  451. LODOP.On_Return = function (TaskID, Value) {
  452. console.log(TaskID, Value!=0, printLogParams)
  453. // 已打印
  454. if (Value!=0) {
  455. if(printLogParams){
  456. console.log('已打印,统计打印次数')
  457. printLog(printLogParams, callback)
  458. }else{
  459. callback({status:200,message:'打印已完成'})
  460. }
  461. } else {
  462. callback()
  463. }
  464. }
  465. for(let i=0;i<dataurl.length;i++){
  466. LODOP.NewPageA()
  467. LODOP.ADD_PRINT_PDF(0, 0, '100%', '100%', dataurl[i])
  468. }
  469. if (type == 'preview') { // 预览
  470. LODOP.PREVIEW()
  471. } else if (type == 'print') { // 打印
  472. LODOP.PRINTA()
  473. }
  474. }
  475. // 获取打印机状态
  476. export const getStatusValue = function (ValueType, ValueIndex, callback) {
  477. const LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'))
  478. if (LODOP.CVERSION) {
  479. LODOP.On_Return = function (TaskID, Value) {
  480. callback(Value)
  481. }
  482. }
  483. var strResult = LODOP.GET_VALUE(ValueType, ValueIndex)
  484. if (!LODOP.CVERSION) return callback(strResult); else return callback('')
  485. }