king
2024-02-20 bd9dfa6b6ff25dbab21ba9a249fc9d9f4d03dce2
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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 label = ''
    let icon = ''
    let type = 'link'
    let className = ''
    let style = {...btn.style}
 
    if (loading) {
      style.opacity = 0
    }
 
    if (btn.show === 'button') {
      label = btn.label
      icon = btn.icon || ''
    } else if (btn.show === 'link') {
      label = <span>{btn.label}{btn.icon ? <MkIcon style={{marginLeft: '8px'}} type={btn.icon}/> : ''}</span>
      icon = ''
    } else if (btn.show === 'icon') {
      icon = btn.icon || ''
    } else if (!btn.$toolbtn) {
      icon = btn.icon || ''
      label = btn.label
      className = 'mk-btn mk-' + btn.class
    } else {
      type = ''
      icon = btn.icon || ''
      label = btn.label
      className = 'mk-btn mk-' + btn.class
    }
    
    return (
      <Button
        type={type}
        title={btn.show === 'icon' ? btn.label : ''}
        style={style}
        icon={icon}
        className={className}
        onClick={(e) => {e.stopPropagation(); this.actionTrigger()}}
      >{label}</Button>
    )
  }
}
 
export default ExportPDF