I am trying to use react-pdf in my app and I have downloaded a code from github for integrating react-pdf in my project. This is working fine and showing the pdf but the size of the pdf is not the original size and it is showing the pdf with 50% decreased size. How can I fix the size problem
const PDFViewer3 = ({
pdfUrl,
fieldsToHighlights = [] // Default to empty array
}) => {
const [numPages, setNumPages] = useState(null)
const [scales, setScales] = useState([]) // Store scaling factors for each page
const [pageHeights, setPageHeights] = useState([]) // Store viewport heights for each page
// Ensure PDF.js worker is set
pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`
const onDocumentLoadSuccess = pdf => {
setNumPages(pdf.numPages)
}
const handleRenderSuccess = (page, pageNumber, scale) => {
const viewport = page.getViewport({ scale: 1 })
setScales(prevScales => {
const newScales = [...prevScales]
newScales[pageNumber - 1] = scale // Save the scale for the page
return newScales
})
setPageHeights(prevHeights => {
const newHeights = [...prevHeights]
newHeights[pageNumber - 1] = viewport.height // Save the viewport height for the page
return newHeights
})
}
const renderHighlights = (pageNumber, scale) => {
const fieldsOnPage = fieldsToHighlights.filter(
field => field.page === pageNumber
)
if (!fieldsOnPage.length) return null
return fieldsOnPage.map((field, index) => (
<div
key={index}
style={{
position: "absolute",
backgroundColor: "yellow",
opacity: 0.4,
left: `${field.x * scale}px`,
// Adjust the y-coordinate by using the dynamically calculated page height
top: `${(pageHeights[pageNumber - 1] - field.y - field.height) *
scale}px`,
width: `${field.width * scale}px`,
height: `${field.height * scale}px`
}}
/>
))
}
return (
<div
style={{ position: "relative", width: "98vw", height: "100vh", justifyItems:'center' }}
className="pdf-view flex"
>
<Document
file={pdfUrl}
onLoadSuccess={onDocumentLoadSuccess}
onContextMenu={e => e.preventDefault()}
>
{Array.from(new Array(numPages), (el, index) => (
<div key={`page_${index + 1}`} style={{ position: "relative" }}>
<Page
pageNumber={index + 1}
onRenderSuccess={page => {
const viewport = page.getViewport({ scale: 1 })
handleRenderSuccess(page, index + 1, viewport.scale)
}}
/>
{scales[index] &&
pageHeights[index] &&
renderHighlights(index + 1, scales[index])}
</div>
))}
</Document>
</div>
)
}
export default PDFViewer3