king
2020-11-23 f8c3c53f9e29541f8c0e3fcbf682c301fd17e06a
src/views/billprint/index.jsx
@@ -107,29 +107,39 @@
        }
        config.style = config.style || {}
        config.zoom = 1
        config.pageSize = ['A4', 'A3', 'A5'].includes(config.pageSize) ? config.pageSize : 'A4'
        config.pageLayout = config.pageLayout !== 'horizontal' ? 'vertical' : 'horizontal'
        config.pagePadding = config.pagePadding !== 'without' ? 'default' : 'without'
        if (config.pageSize === 'A3') {
          if (config.pageLayout === 'horizontal') {
            config.zoom = 1.35
            config.style.height = 845
          } else {
            config.style.height = 1695
          }
        } else if (config.pageSize === 'A5') {
          config.zoom = 0.7
          if (config.pageLayout === 'horizontal') {
            config.style.height = 840
          } else {
            config.style.height = 1700
          }
        } else {
          if (config.pageLayout === 'horizontal') {
            config.style.height = 845
          } else {
            config.style.height = 1680
        let pageParam = {
          A4: {
            vertical: 1000,
            horizontal: 1200,
            verticaldefault: 1.45789,
            verticalwithout: 1.41428,
            horizontaldefault: 0.685,
            horizontalwithout: 0.705,
          },
          A3: {
            vertical: 1200,
            horizontal: 1600,
            verticaldefault: 1.44404,
            verticalwithout: 1.41414,
            horizontaldefault: 0.6923,
            horizontalwithout: 0.707,
          },
          A5: {
            vertical: 700,
            horizontal: 1000,
            verticaldefault: 1.482,
            verticalwithout: 1.417,
            horizontaldefault: 0.6736,
            horizontalwithout: 0.7047,
          }
        }
        config.width = pageParam[config.pageSize][config.pageLayout]
        config.style.height = Math.floor(config.width * pageParam[config.pageSize][config.pageLayout + config.pagePadding])
        let params = []
        let _pars = []
@@ -457,55 +467,35 @@
  print = () => {
    const { config } = this.state
    let jubuData = document.getElementById('bill-print').innerHTML
    window.document.body.innerHTML = jubuData
    document.getElementsByTagName('body')[0].style.width = '1200px'
    document.getElementsByTagName('body')[0].style.margin = '0 auto'
    document.getElementsByTagName('body')[0].style.zoom = config.zoom
    try {
      if (window.ActiveXObject) {
        let hkeyRoot
        let hkeyPath
        let hkeyKey
        // eslint-disable-next-line
        let RegWsh = new ActiveXObject('WScript.Shell')
      let iframe = document.createElement('IFRAME')
      let linkList = document.getElementsByTagName('link')     // 获取父窗口link标签对象列表
      let styleList = document.getElementsByTagName('style')   // 获取父窗口style标签对象列表
        hkeyRoot = 'HKEY_CURRENT_USER'
        hkeyPath = '\\Software\\Microsoft\\Internet Explorer\\PageSetup\\'
        // 设置页眉/脚的字体样式
        hkeyKey = 'font'
        RegWsh.RegWrite(hkeyRoot + hkeyPath + hkeyKey, 'font-size: 12px; font-family: 黑体; line-height: 24px')
        // 设置页眉
        hkeyKey = 'header'
        RegWsh.RegWrite(hkeyRoot + hkeyPath + hkeyKey, '打印编号')
        // 设置页脚
        hkeyKey = 'footer'
        RegWsh.RegWrite(hkeyRoot + hkeyPath + hkeyKey, '&b第 &p 页/共 &P 页')
        // 设置页边距(0.6 要乘以 2.5为实际打印的尺寸)
        hkeyKey = 'margin_bottom'
        RegWsh.RegWrite(hkeyRoot + hkeyPath + hkeyKey, '0.6')
        hkeyKey = 'margin_left'
        RegWsh.RegWrite(hkeyRoot + hkeyPath + hkeyKey, '0.6')
        hkeyKey = 'margin_right'
        RegWsh.RegWrite(hkeyRoot + hkeyPath + hkeyKey, '0.6')
        hkeyKey = 'margin_top'
        RegWsh.RegWrite(hkeyRoot + hkeyPath + hkeyKey, '0.6')
        window.print()
      } else {
        window.print()
      document.body.appendChild(iframe)
      let doc = iframe.contentWindow.document
      doc.open()
      doc.write(`<!DOCTYPE html><html lang="en"><head>`)
      for (let i = 0;i < linkList.length;i++) {
        if (linkList[i].type === 'text/css') {
          doc.write(`<LINK rel="stylesheet" type="text/css" href="${linkList[i].href}">`)
        }
      }
      doc.write(`<style>body{width: ${config.width}px!important;}</style>`)
      for (let i = 0;i < styleList.length;i++) {
        doc.write('<style>' + styleList[i].innerHTML + '</style>')
      }
      doc.write(`</head><body>`)
      doc.write(jubuData)
      doc.write(`</body></html>`)
      doc.close()
      iframe.contentWindow.focus()
      iframe.contentWindow.print()
      document.getElementsByTagName('body')[0].style.zoom = 1
      document.body.removeChild(iframe)
      setTimeout(() => {
        window.close()