
Research
/Security News
Critical Vulnerability in NestJS Devtools: Localhost RCE via Sandbox Escape
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
@nyazkhan/react-pdf-viewer
Advanced tools
A comprehensive React TypeScript component library for viewing and interacting with PDF files using Mozilla PDF.js. Features include text selection, highlighting, search, sidebar, multiple view modes, and complete PDF.js web viewer functionality.
A comprehensive React TypeScript component library for viewing and interacting with PDF files using Mozilla PDF.js. Features complete PDF.js web viewer functionality with modern React patterns, full TypeScript support, and enhanced user experience.
npm install @nyazkhan/react-pdf-viewer pdfjs-dist
yarn add @nyazkhan/react-pdf-viewer pdfjs-dist
pnpm add @nyazkhan/react-pdf-viewer pdfjs-dist
import React from 'react'
import { PDFViewer } from '@nyazkhan/react-pdf-viewer'
import '@nyazkhan/react-pdf-viewer/styles/viewer.css'
function App() {
return (
<div style={{ height: '100vh' }}>
<PDFViewer
file="/path/to/your/document.pdf"
width="100%"
height="100%"
/>
</div>
)
}
export default App
import React, { useState } from 'react'
import {
PDFViewer,
type PDFDocumentProxy,
type PDFHighlightType,
type ViewMode,
type SidebarView
} from '@nyazkhan/react-pdf-viewer'
import '@nyazkhan/react-pdf-viewer/styles/viewer.css'
function AdvancedPDFViewer() {
const [highlights, setHighlights] = useState<PDFHighlightType[]>([])
const [viewMode, setViewMode] = useState<ViewMode>('single')
const [sidebarView, setSidebarView] = useState<SidebarView>('thumbnails')
const handleDocumentLoad = (pdf: PDFDocumentProxy) => {
console.log(`Loaded PDF with ${pdf.numPages} pages`)
}
const handleSearch = (term: string) => {
console.log(`Searching for: ${term}`)
}
return (
<PDFViewer
file="/sample.pdf"
highlights={highlights}
viewMode={viewMode}
sidebarView={sidebarView}
enableTextSelection={true}
enableKeyboardShortcuts={true}
onDocumentLoad={handleDocumentLoad}
onViewModeChange={setViewMode}
onSidebarViewChange={setSidebarView}
onSearch={handleSearch}
width="100%"
height="100vh"
showPageControls={true}
showZoomControls={true}
showSearchOption={true}
showPrintOption={true}
showDownloadOption={true}
/>
)
}
import React, { useState, useCallback } from 'react'
import { PDFViewer, type PDFHighlightType, type PDFDocumentProxy } from '@nyazkhan/react-pdf-viewer'
function PDFWithHighlighting() {
const [highlights, setHighlights] = useState<PDFHighlightType[]>([])
const [pdfDocument, setPdfDocument] = useState<PDFDocumentProxy | null>(null)
const createHighlight = useCallback(async (searchText: string) => {
if (!pdfDocument) return
const newHighlights: PDFHighlightType[] = []
for (let pageNum = 1; pageNum <= pdfDocument.numPages; pageNum++) {
const page = await pdfDocument.getPage(pageNum)
const textContent = await page.getTextContent()
// Your highlighting logic here
// ... (implementation details)
}
setHighlights(newHighlights)
}, [pdfDocument])
return (
<div>
<input
type="text"
placeholder="Enter text to highlight"
onKeyPress={(e) => {
if (e.key === 'Enter') {
createHighlight(e.currentTarget.value)
}
}}
/>
<PDFViewer
file="/document.pdf"
highlights={highlights}
onDocumentLoad={setPdfDocument}
width="100%"
height="600px"
/>
</div>
)
}
Prop | Type | Default | Description |
---|---|---|---|
file | string | File | ArrayBuffer | Uint8Array | - | Required. PDF file to display |
width | string | number | "100%" | Viewer width |
height | string | number | "600px" | Viewer height |
page | number | 1 | Initial page number |
scale | number | 1.0 | Initial zoom scale |
rotation | number | 0 | Initial rotation (0, 90, 180, 270) |
viewMode | ViewMode | "single" | Page view mode |
sidebarView | SidebarView | "none" | Sidebar view type |
highlights | PDFHighlightType[] | [] | Text highlights to display |
enableTextSelection | boolean | true | Enable text selection |
enableKeyboardShortcuts | boolean | true | Enable keyboard shortcuts |
renderToolbar | boolean | true | Show toolbar |
renderSidebar | boolean | true | Show sidebar |
Prop | Type | Description |
---|---|---|
onDocumentLoad | (pdf: PDFDocumentProxy) => void | Called when PDF loads |
onPageChange | (page: number) => void | Called when page changes |
onScaleChange | (scale: number) => void | Called when zoom changes |
onViewModeChange | (mode: ViewMode) => void | Called when view mode changes |
onSearch | (term: string) => void | Called when searching |
onError | (error: Error) => void | Called on errors |
type ViewMode = 'single' | 'continuous' | 'two-page' | 'book'
type SidebarView = 'none' | 'thumbnails' | 'outline' | 'attachments' | 'layers'
type ToolbarTool = 'none' | 'hand' | 'selection' | 'annotation'
interface PDFHighlightType {
id: string
pageNumber: number
rects: PDFRect[]
color?: string
opacity?: number
content?: string
}
interface PDFRect {
left: number
top: number
width: number
height: number
}
Shortcut | Action |
---|---|
n , j , Space | Next page |
p , k , Shift+Space | Previous page |
Ctrl/Cmd + + | Zoom in |
Ctrl/Cmd + - | Zoom out |
Ctrl/Cmd + 0 | Actual size |
Ctrl/Cmd + F | Find/Search |
Ctrl/Cmd + G | Find next |
Ctrl/Cmd + O | Open file |
Ctrl/Cmd + S | Save/Download |
Ctrl/Cmd + P | |
F4 | Toggle sidebar |
Home | First page |
End | Last page |
r | Rotate clockwise |
Shift + r | Rotate counter-clockwise |
Import the default styles:
@import '@nyazkhan/react-pdf-viewer/styles/viewer.css';
Or customize with CSS variables:
.pdf-viewer {
--pdf-toolbar-bg: #2c3e50;
--pdf-toolbar-color: white;
--pdf-sidebar-bg: #34495e;
--pdf-page-border: #ddd;
--pdf-highlight-color: #ffff00;
--pdf-selection-color: #3498db;
}
MIT © Nyaz Khan
Contributions are welcome! Please see our Contributing Guide for details.
Found a bug? Please open an issue with a detailed description.
Made with ❤️ for the React community
FAQs
A comprehensive React TypeScript component library for viewing and interacting with PDF files using Mozilla PDF.js. Features include text selection, highlighting, search, sidebar, multiple view modes, and complete PDF.js web viewer functionality.
We found that @nyazkhan/react-pdf-viewer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.