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
|