JGPrint.js 9.3 KB

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