Last active
June 19, 2020 00:28
-
-
Save ntalcus/c3a7ed1dd6b763f7bb94aff81bbe356c to your computer and use it in GitHub Desktop.
non-functinonal styling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { useRef, useState, useEffect } from "react"; | |
| import PropTypes from "prop-types"; | |
| import pdfjs from "pdfjs-dist"; | |
| import pdfjsWorker from "pdfjs-dist/build/pdf.worker.entry"; | |
| import textLayerBuilder from "pdfjs-dist/lib/web/text_layer_builder"; | |
| import "./PDFViewer.css"; | |
| import "pdfjs-dist/web/pdf_viewer.css"; | |
| pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker; | |
| // lifted from here: https://github.com/mozilla/pdf.js/pull/11220/files | |
| const PdfComponent = ({ src }) => { | |
| const canvasRef = useRef(null); | |
| const container = useRef(null); | |
| const textRef = useRef(null); | |
| const [PageNum, setPageNum] = useState(1); | |
| useEffect(() => { | |
| const fetchPdf = async () => { | |
| const loadingTask = pdfjs.getDocument(src); | |
| const pdf = await loadingTask.promise; | |
| const page = await pdf.getPage(PageNum); | |
| const scale = 1.5; | |
| const viewport = page.getViewport({ scale: scale }); | |
| const canvas = canvasRef.current; | |
| const context = canvas.getContext("2d"); | |
| canvas.height = viewport.height; | |
| canvas.width = viewport.width; | |
| const renderContext = { | |
| canvasContext: context, | |
| viewport: viewport, | |
| }; | |
| const renderTask = page.render(renderContext); | |
| await renderTask.promise; | |
| // const borders = canvas.getBoundingClientRect(); | |
| // console.log(borders); | |
| page.getTextContent().then((textContent) => { | |
| textRef.current.style.left = canvasRef.current.style.left + "px"; | |
| textRef.current.style.top = canvasRef.current.style.top + "px"; | |
| textRef.current.style.height = viewport.height + "px"; | |
| textRef.current.style.width = viewport.width + "px"; | |
| pdfjs.renderTextLayer({ | |
| textContent: textContent, | |
| container: textRef.current, | |
| viewport: viewport, | |
| textDivs: [], | |
| }); | |
| }); | |
| }; | |
| fetchPdf(); | |
| }, [src]); | |
| // const style = { | |
| // left: canvasRef.current.style.left, | |
| // top: canvasRef.current.style.top, | |
| // height: canvasRef.current.style.top, | |
| // width: canvasRef.current.style.width, | |
| // }; | |
| return ( | |
| <div ref={container} className="pdfViewer"> | |
| <canvas | |
| className="page" | |
| ref={canvasRef} | |
| width={window.innerWidth} | |
| height={window.innerHeight} | |
| /> | |
| <div | |
| id="textLayer" | |
| // style={style} | |
| ref={textRef} | |
| ></div> | |
| </div> | |
| ); | |
| }; | |
| PdfComponent.propTypes = { | |
| src: PropTypes.string, | |
| }; | |
| export default PdfComponent; | |
| import React, { useRef, useState, useEffect } from "react"; | |
| import PropTypes from "prop-types"; | |
| import pdfjs from "pdfjs-dist"; | |
| import pdfjsWorker from "pdfjs-dist/build/pdf.worker.entry"; | |
| import textLayerBuilder from "pdfjs-dist/lib/web/text_layer_builder"; | |
| import "./PDFViewer.css"; | |
| import "pdfjs-dist/web/pdf_viewer.css"; | |
| pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker; | |
| // lifted from here: https://github.com/mozilla/pdf.js/pull/11220/files | |
| const PdfComponent = ({ src }) => { | |
| const canvasRef = useRef(null); | |
| const container = useRef(null); | |
| const textRef = useRef(null); | |
| const [PageNum, setPageNum] = useState(1); | |
| useEffect(() => { | |
| const fetchPdf = async () => { | |
| const loadingTask = pdfjs.getDocument(src); | |
| const pdf = await loadingTask.promise; | |
| const page = await pdf.getPage(PageNum); | |
| const scale = 1.5; | |
| const viewport = page.getViewport({ scale: scale }); | |
| const canvas = canvasRef.current; | |
| const context = canvas.getContext("2d"); | |
| canvas.height = viewport.height; | |
| canvas.width = viewport.width; | |
| const renderContext = { | |
| canvasContext: context, | |
| viewport: viewport, | |
| }; | |
| const renderTask = page.render(renderContext); | |
| await renderTask.promise; | |
| // const borders = canvas.getBoundingClientRect(); | |
| // console.log(borders); | |
| page.getTextContent().then((textContent) => { | |
| textRef.current.style.left = canvasRef.current.style.left + "px"; | |
| textRef.current.style.top = canvasRef.current.style.top + "px"; | |
| textRef.current.style.height = viewport.height + "px"; | |
| textRef.current.style.width = viewport.width + "px"; | |
| pdfjs.renderTextLayer({ | |
| textContent: textContent, | |
| container: textRef.current, | |
| viewport: viewport, | |
| textDivs: [], | |
| }); | |
| }); | |
| }; | |
| fetchPdf(); | |
| }, [src]); | |
| // const style = { | |
| // left: canvasRef.current.style.left, | |
| // top: canvasRef.current.style.top, | |
| // height: canvasRef.current.style.top, | |
| // width: canvasRef.current.style.width, | |
| // }; | |
| return ( | |
| <div ref={container} className="pdfViewer"> | |
| <canvas | |
| className="page" | |
| ref={canvasRef} | |
| width={window.innerWidth} | |
| height={window.innerHeight} | |
| /> | |
| <div | |
| id="textLayer" | |
| // style={style} | |
| ref={textRef} | |
| ></div> | |
| </div> | |
| ); | |
| }; | |
| PdfComponent.propTypes = { | |
| src: PropTypes.string, | |
| }; | |
| export default PdfComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment