king
2024-05-09 a975aa770d1f5d0ae133f0e87a28b5d6b94734ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Button } from 'antd'
import html2Canvas from 'html2canvas'
import moment from 'moment'
import JsPDF from 'jspdf'
 
import MKEmitter from '@/utils/events.js'
import MkIcon from '@/components/mk-icon'
 
// import './index.scss'
 
class ExportPDF extends Component {
  static propTpyes = {
    btn: PropTypes.object
  }
 
  state = {
    loading: false,
  }
 
  componentDidMount () {
    MKEmitter.addListener('triggerBtnId', this.actionTrigger)
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('triggerBtnId', this.actionTrigger)
  }
 
  /**
   * @description 触发按钮操作
   */
  actionTrigger = (triggerId) => {
    const { btn } = this.props
    const { loading } = this.state
 
    if (loading) return
    if (triggerId && btn.uuid !== triggerId) return
 
    this.setState({
      loading: true
    }, () => {
      this.getCanvas()
    })
  }
 
  getCanvas = () => {
    const { btn } = this.props
 
    let wrap = document.getElementById('menu' + btn.$MenuID)
 
    const opts = {
      scale: 1.5,        // 缩放比例,提高生成图片清晰度
      useCORS: false,    // 允许加载跨域的图片
      allowTaint: false, // 允许图片跨域,和 useCORS 二者不可共同使用
      tainttest: false,  // 检测每张图片已经加载完成
      logging: false     // 日志开关,发布的时候记得改成 false
    }
    
    // eslint-disable-next-line
    html2Canvas(wrap, opts).then(canvas => {
      // const contentWidth = parseInt(canvas.style.width) * 2
      // const contentHeight = parseInt(canvas.style.height) * 2
      const contentWidth = canvas.width
      const contentHeight = canvas.height
      const pageHeight = (contentWidth / 592.28) * 841.89
      let leftHeight = contentHeight
 
      const imgWidth = 595.28
      const imgHeight = (592.28 / contentWidth) * contentHeight
      const pageData = canvas.toDataURL('image/jpeg', 1.0)
      let position = 0
 
      let title = btn.logLabel + moment().format('YYYYMMDDHHmmss')
      const PDF = new JsPDF('', 'pt', 'a4')
 
      if (leftHeight < pageHeight) {
        PDF.addImage(pageData, 'JPEG', 10, 0, imgWidth - 20, imgHeight)
      } else {
        while (leftHeight > 0) {
          PDF.addImage(pageData, 'JPEG', 10, position, imgWidth - 20, imgHeight)
 
          leftHeight -= pageHeight
          position -= 841.89
 
          if (leftHeight > 0) {
            PDF.addPage()
          }
        }
      }
      
      PDF.save(title + '.pdf')
 
      this.setState({
        loading: false
      })
    })
  }
 
  render() {
    const { btn } = this.props
    const { loading } = this.state
 
    let style = {...btn.style}
 
    if (loading) {
      style.opacity = 0
    }
 
    let label = ''
 
    if (btn.show === 'link') {
      label = <span>{btn.label}{btn.icon ? <MkIcon style={{marginLeft: '8px'}} type={btn.icon} /> : ''}</span>
    } else if (btn.show === 'icon') {
      label = <MkIcon type={btn.icon} />
    } else {
      label = <span>{btn.icon ? <MkIcon style={{marginRight: '8px'}} type={btn.icon} /> : ''}{btn.label}</span>
    }
    
    return (
      <Button
        type="link"
        title={btn.show === 'icon' ? btn.label : ''}
        style={style}
        className={btn.hover || ''}
        onClick={(e) => {e.stopPropagation(); this.actionTrigger()}}
      >{label}</Button>
    )
  }
}
 
export default ExportPDF