Skip to content

Instantly share code, notes, and snippets.

@vinhlh
Created May 22, 2016 08:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinhlh/1d285f20bd909a9a3efd85161fdd1ab8 to your computer and use it in GitHub Desktop.
Save vinhlh/1d285f20bd909a9a3efd85161fdd1ab8 to your computer and use it in GitHub Desktop.
React PDF Viewer component using PDF.js (setup the iframe url by https://gist.github.com/vinhlh/22c92f1a9f4fdb72fdbabefee34cef4d)
import React, { Component, PropTypes } from 'react'
import { PDF_VIEWER } from '../configs'
const propTypes = {
ratio : PropTypes.number,
file : PropTypes.string.isRequired
}
const defaultProps = {
ratio: 56.25
}
class PdfViewer extends Component {
shouldComponentUpdate(newProps) {
return this.props.file != newProps.file
}
render() {
const { ratio, file } = this.props
return (
<div className="pdf-viewer" style={{ paddingBottom: `${ratio}%` }} content="Loading">
<iframe src={`${PDF_VIEWER}?file=${file}`} />
</div>
)
}
}
PdfViewer.propTypes = propTypes
PdfViewer.defaultProps = defaultProps
export default PdfViewer
@vinhlh
Copy link
Author

vinhlh commented May 22, 2016

.pdf-viewer {
    position: relative;
    width:height;
    padding-bottom: 56.25%;

    iframe {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment