All files / src/gadgets/file-attachment/attachment-preview pdf.js

100% Statements 33/33
100% Branches 29/29
100% Functions 10/10
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                          8x 8x 8x                                   8x       10x 8x   2x         8x 2x 2x 6x         2x 2x         20x       6x         28x       48x               8x 8x       3x   8x       2x   2x       1x         6x 2x 2x 2x 2x 2x 2x 2x       2x            
/* Copyright © 2005-2017 Kuali, Inc. - All Rights Reserved
 * You may use and modify this code under the terms of the Kuali, Inc.
 * Pre-Release License Agreement. You may not distribute it.
 *
 * You should have received a copy of the Kuali, Inc. Pre-Release License
 * Agreement with this file. If not, please write to license@kuali.co.
 */
 
import pdfjs from 'pdfjs-dist'
import React from 'react'
 
class ReactPdf extends React.Component {
  constructor (props) {
    super(props)
    this.state = {}
    pdfjs.workerSrc = `${props.pdfUrl}/pdf.worker.js`
  }
 
  static propTypes = {
    file: React.PropTypes.string,
    content: React.PropTypes.string,
    page: React.PropTypes.number,
    scale: React.PropTypes.number,
    onDocumentComplete: React.PropTypes.func,
    onPageComplete: React.PropTypes.func
  }
 
  static defaultProps = {
    page: 1,
    scale: 1.0
  }
 
  componentDidMount () {
    this._loadPDFDocument(this.props)
  }
 
  _loadPDFDocument = props => {
    if (props.file && typeof props.file === 'string') {
      pdfjs.getDocument(props.file).then(this._onDocumentComplete)
    } else {
      console.error('ReactPDF component only works with urls')
    }
  }
 
  componentWillReceiveProps (newProps) {
    if (newProps.file && newProps.file !== this.props.file) {
      this.setState({ page: null })
      this._loadPDFDocument(newProps)
    } else if (
      this.state.pdf &&
      newProps.page &&
      newProps.page !== this.props.page
    ) {
      this.setState({ page: null })
      this.state.pdf.getPage(newProps.page).then(this._onPageComplete)
    }
  }
 
  componentDidUpdate (prevProps, prevState) {
    if (
      this.state.page !== prevState.page ||
      this.props.scale !== prevProps.scale
    ) {
      this._renderPdf()
    }
  }
 
  render () {
    return (
      <div style={{ overflow: 'auto' }}>
        <canvas
          ref={ref => {
            this.pdfCanvasRef = ref
          }}
        />
      </div>
    )
  }
 
  _onDocumentComplete = pdf => {
    this.setState({ pdf })
    if (
      this.props.onDocumentComplete &&
      typeof this.props.onDocumentComplete === 'function'
    ) {
      this.props.onDocumentComplete(pdf.numPages)
    }
    pdf.getPage(this.props.page).then(this._onPageComplete)
  }
 
  _onPageComplete = page => {
    this.setState({ page })
 
    if (
      this.props.onPageComplete &&
      typeof this.props.onPageComplete === 'function'
    ) {
      this.props.onPageComplete(page.pageIndex + 1)
    }
  }
 
  _renderPdf = () => {
    if (this.pdfCanvasRef && this.state.page) {
      const canvas = this.pdfCanvasRef
      const context = canvas.getContext('2d')
      const scale = this.props.scale
      const viewport = this.state.page.getViewport(scale)
      canvas.height = viewport.height
      canvas.width = viewport.width
      const renderContext = {
        canvasContext: context,
        viewport
      }
      this.state.page.render(renderContext)
    }
  }
}
 
export default ReactPdf