UNPKG

@nyazkhan/react-pdf-viewer

Version:

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.

408 lines (397 loc) 15.4 kB
import React, { CSSProperties, ReactNode } from 'react'; import { PDFDocumentProxy as PDFDocumentProxy$1, PDFPageProxy as PDFPageProxy$1, PageViewport, RenderTask, PDFDocumentLoadingTask } from 'pdfjs-dist'; export * from 'pdfjs-dist'; type PDFDocumentProxy = PDFDocumentProxy$1; type PDFPageProxy = PDFPageProxy$1; type PDFPageViewport = PageViewport; type PDFRenderTask = RenderTask; type PDFLoadingTask = PDFDocumentLoadingTask; interface PDFTextContent { items: PDFTextItem[]; } interface PDFTextItem { str: string; dir: string; width: number; height: number; transform: number[]; fontName: string; } interface PDFRenderContext { canvasContext: CanvasRenderingContext2D; viewport: PDFPageViewport; background?: string; } type ToolbarTool = 'none' | 'pan' | 'selection' | 'annotation'; type ViewMode = 'single' | 'continuous' | 'two-page' | 'book'; type SidebarView = 'none' | 'thumbnails' | 'outline' | 'attachments' | 'layers'; type ZoomMode = 'auto' | 'page-fit' | 'page-width' | 'page-height' | 'actual'; interface PDFOutlineItem { title: string; bold?: boolean; italic?: boolean; color?: [number, number, number]; dest?: any; url?: string; items?: PDFOutlineItem[]; } interface PDFAttachment { filename: string; content: Uint8Array; } interface PDFDocumentInfo { Title?: string; Author?: string; Subject?: string; Creator?: string; Producer?: string; CreationDate?: string; ModDate?: string; Keywords?: string; PDFFormatVersion?: string; } interface PDFSidebarProps { isOpen: boolean; activeView: SidebarView; pdf?: PDFDocumentProxy; currentPage: number; outline?: PDFOutlineItem[]; attachments?: PDFAttachment[]; onToggle: () => void; onViewChange: (view: SidebarView) => void; onPageSelect: (page: number) => void; onOutlineClick: (dest: any) => void; className?: string; style?: CSSProperties; } interface PDFViewerProps { file: string | File | ArrayBuffer | Uint8Array; page?: number; scale?: number; rotation?: number; width?: string | number; height?: string | number; className?: string; style?: CSSProperties; viewMode?: ViewMode; sidebarView?: SidebarView; zoomMode?: ZoomMode; activeTool?: ToolbarTool; enableTextSelection?: boolean; enableAnnotations?: boolean; enableForms?: boolean; enableSearch?: boolean; enableThumbnails?: boolean; enableKeyboardShortcuts?: boolean; renderToolbar?: boolean; renderSidebar?: boolean; customToolbar?: ReactNode; showPageControls?: boolean; showZoomControls?: boolean; showRotateControls?: boolean; showViewModeControls?: boolean; showOpenOption?: boolean; showSearchOption?: boolean; showPrintOption?: boolean; showDownloadOption?: boolean; showToolSelection?: boolean; showFitOptions?: boolean; showPresentationMode?: boolean; showDocumentInfo?: boolean; hideToolbarComponents?: Array<'open' | 'print' | 'download' | 'search' | 'tools' | 'zoom' | 'view' | 'presentation' | 'info'>; hideSidebarTabs?: Array<'thumbnails' | 'outline' | 'attachments' | 'layers'>; customToolbarActions?: Array<{ id: string; label: string; icon: string; onClick: () => void; }>; highlights?: PDFHighlight$1[]; onDocumentLoad?: (pdf: PDFDocumentProxy) => void; onPageChange?: (page: number) => void; onScaleChange?: (scale: number) => void; onRotationChange?: (rotation: number) => void; onViewModeChange?: (mode: ViewMode) => void; onSidebarToggle?: (isOpen: boolean) => void; onSidebarViewChange?: (view: SidebarView) => void; onZoomModeChange?: (mode: ZoomMode) => void; onToolChange?: (tool: ToolbarTool) => void; onError?: (error: Error) => void; onPageRender?: (page: PDFPageProxy) => void; onSearch?: (term: string) => void; onSearchNext?: () => void; onSearchPrevious?: () => void; onClearSearch?: () => void; onPrint?: () => void; onDownload?: () => void; onOpenFile?: (file: File) => void; onPresentationMode?: () => void; onDocumentInfo?: (info: PDFDocumentInfo) => void; onZoomToFit?: () => void; onZoomToWidth?: () => void; onActualSize?: () => void; } interface PDFToolbarProps { currentPage: number; totalPages: number; scale: number; viewMode?: ViewMode; zoomMode?: ZoomMode; activeTool?: ToolbarTool; searchTerm?: string; searchResults?: number; currentSearchResult?: number; sidebarOpen?: boolean; onPageChange: (page: number) => void; onScaleChange: (scale: number) => void; onPrevPage: () => void; onNextPage: () => void; onZoomIn: () => void; onZoomOut: () => void; onRotate?: () => void; onOpenFile?: () => void; onPrint?: () => void; onDownload?: () => void; onSearch?: (term: string) => void; onSearchNext?: () => void; onSearchPrevious?: () => void; onClearSearch?: () => void; onToolChange?: (tool: ToolbarTool) => void; onZoomToFit?: () => void; onZoomToWidth?: () => void; onViewModeChange?: (mode: ViewMode) => void; onZoomModeChange?: (mode: ZoomMode) => void; onSidebarToggle?: () => void; onPresentationMode?: () => void; onDocumentInfo?: () => void; className?: string; style?: CSSProperties; showPageControls?: boolean; showZoomControls?: boolean; showRotateControls?: boolean; showViewModeControls?: boolean; showOpenOption?: boolean; showSearchOption?: boolean; showPrintOption?: boolean; showDownloadOption?: boolean; showToolSelection?: boolean; showFitOptions?: boolean; showPresentationMode?: boolean; } interface PDFPageProps { pageNumber: number; scale?: number; rotation?: number; pdf: PDFDocumentProxy; onPageRender?: (page: PDFPageProxy) => void; onError?: (error: Error) => void; className?: string; style?: CSSProperties; enableTextSelection?: boolean; highlights?: PDFHighlight$1[]; } interface PDFHighlight$1 { id: string; pageNumber: number; rects: PDFRect[]; color?: string; opacity?: number; content?: string; onClick?: (highlight: PDFHighlight$1) => void; } interface PDFRect { left: number; top: number; width: number; height: number; } interface PDFHighlightProps { highlights: PDFHighlight$1[]; pageNumber: number; viewport: PDFPageViewport; onHighlightClick?: (highlight: PDFHighlight$1) => void; className?: string; style?: CSSProperties; } /** * Configure PDF.js worker source with retry logic * Call this before using any PDF components */ declare const configurePDFWorker: (workerSrc?: string, force?: boolean) => void; /** * Get the current worker configuration */ declare const getWorkerSrc: () => string; /** * Reset worker configuration (useful for troubleshooting) */ declare const resetWorkerConfiguration: () => void; /** * Retry worker configuration with next fallback */ declare const retryWorkerConfiguration: () => boolean; /** * Check if worker is properly configured */ declare const isWorkerConfiguredProperly: () => boolean; /** * Get current retry count for debugging */ declare const getWorkerRetryCount: () => number; declare const PDFViewer: React.NamedExoticComponent<PDFViewerProps>; declare const PDFPage: React.FC<PDFPageProps>; declare const PDFToolbar: React.FC<PDFToolbarProps>; declare const PDFSidebar: React.FC<PDFSidebarProps>; declare const PDFHighlight: React.FC<PDFHighlightProps>; interface KeyboardShortcutsHandlers { onNextPage: () => void; onPreviousPage: () => void; onFirstPage: () => void; onLastPage: () => void; onZoomIn: () => void; onZoomOut: () => void; onActualSize: () => void; onRotateClockwise: () => void; onRotateCounterClockwise: () => void; onPresentationMode: () => void; onHandTool: () => void; onTextSelection: () => void; onFind: () => void; onFindNext: () => void; onFindPrevious: () => void; onDownload: () => void; onPrint: () => void; onOpenFile: () => void; onGoToPage: () => void; onToggleSidebar: () => void; } declare const useKeyboardShortcuts: (enabled?: boolean, handlers?: Partial<KeyboardShortcutsHandlers>) => null; /** * Create highlights for text search across all pages of a PDF document * @param pdfDocument - The loaded PDF document * @param searchText - Text to search and highlight * @param color - Highlight color (default: '#ffff00') * @param opacity - Highlight opacity (default: 0.3) * @returns Array of highlight objects */ declare function createHighlightsForText(pdfDocument: PDFDocumentProxy, searchText: string, color?: string, opacity?: number): Promise<PDFHighlight$1[]>; /** * Extract document metadata and information * @param pdfDocument - The loaded PDF document * @returns Document information object */ declare function extractDocumentInfo(pdfDocument: PDFDocumentProxy): Promise<{ constructor: Function; toString(): string; toLocaleString(): string; valueOf(): Object; hasOwnProperty(v: PropertyKey): boolean; isPrototypeOf(v: Object): boolean; propertyIsEnumerable(v: PropertyKey): boolean; numPages: number; fingerprints: (string | null)[]; } | null>; /** * Search for text in a specific page * @param pdfDocument - The loaded PDF document * @param pageNumber - Page number to search (1-based) * @param searchText - Text to search for * @returns Array of matches with position information */ declare function searchTextInPage(pdfDocument: PDFDocumentProxy, pageNumber: number, searchText: string): Promise<Array<{ text: string; pageNumber: number; position: { x: number; y: number; }; }>>; /** * Get text content from a specific page * @param pdfDocument - The loaded PDF document * @param pageNumber - Page number (1-based) * @returns Plain text content of the page */ declare function getPageTextContent(pdfDocument: PDFDocumentProxy, pageNumber: number): Promise<string>; /** * Download PDF file from URL with CORS handling * @param url - PDF file URL * @returns File blob or null if failed */ declare function downloadPDFFromUrl(url: string): Promise<Blob | null>; /** * Create a blob URL from file data * @param file - File object or blob * @returns Blob URL string */ declare function createBlobUrl(file: File | Blob): string; /** * Cleanup blob URL to free memory * @param url - Blob URL to cleanup */ declare function cleanupBlobUrl(url: string): void; /** * Predefined sample texts for testing highlighting functionality */ declare const SAMPLE_HIGHLIGHT_TEXTS: { readonly traceMonkey: "r functions in order to verify that the call stack is refreshedat any point it needs to be used. In order to access the call stack,a function must be annotated as either FORCESSTACK or RE-QUIRESSTACK. These annotations are also required in order to callREQUIRESSTACK functions, which are presumed to access the callstack transitively. FORCESSTACK is a trusted annotation, appliedto only 5 functions, that means the function refreshes the call stack.REQUIRESSTACK is an untrusted a"; readonly title: "Trace-based Just-in-Time Type Specialization for Dynamic"; readonly introduction: "Dynamic languages such as JavaScript, Python, and Ruby, are popular since they are expressive, accessible to non-experts, and make deployment as easy as distributing a source file"; readonly compilation: "We present a trace-based compilation technique for dynamic languages that reconciles speed of compilation with excellent performance of the generated machine code"; }; /** * Sample PDF URLs for testing */ declare const SAMPLE_PDF_URLS: { readonly mozilla_tracemonkey: "https://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf"; readonly local_tracemonkey: "/compressed.tracemonkey-pldi-09.pdf"; }; /** * Get current worker status and information * @returns Worker information object */ declare function getWorkerInfo(): { configured: boolean; workerSrc: string; retryCount: number; }; /** * Test PDF.js worker functionality * @returns Promise that resolves to test result */ declare function testWorker(): Promise<{ success: boolean; message: string; }>; /** * Reset PDF.js worker configuration */ declare function resetWorker(): void; /** * Utility class for managing PDF operations */ declare class PDFUtils { static createHighlights: typeof createHighlightsForText; static extractInfo: typeof extractDocumentInfo; static searchInPage: typeof searchTextInPage; static getPageText: typeof getPageTextContent; static downloadFromUrl: typeof downloadPDFFromUrl; static createBlobUrl: typeof createBlobUrl; static cleanupBlobUrl: typeof cleanupBlobUrl; static getWorkerInfo: typeof getWorkerInfo; static testWorker: typeof testWorker; static resetWorker: typeof resetWorker; static samples: { texts: { readonly traceMonkey: "r functions in order to verify that the call stack is refreshedat any point it needs to be used. In order to access the call stack,a function must be annotated as either FORCESSTACK or RE-QUIRESSTACK. These annotations are also required in order to callREQUIRESSTACK functions, which are presumed to access the callstack transitively. FORCESSTACK is a trusted annotation, appliedto only 5 functions, that means the function refreshes the call stack.REQUIRESSTACK is an untrusted a"; readonly title: "Trace-based Just-in-Time Type Specialization for Dynamic"; readonly introduction: "Dynamic languages such as JavaScript, Python, and Ruby, are popular since they are expressive, accessible to non-experts, and make deployment as easy as distributing a source file"; readonly compilation: "We present a trace-based compilation technique for dynamic languages that reconciles speed of compilation with excellent performance of the generated machine code"; }; urls: { readonly mozilla_tracemonkey: "https://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf"; readonly local_tracemonkey: "/compressed.tracemonkey-pldi-09.pdf"; }; }; } export { type KeyboardShortcutsHandlers, type PDFAttachment, type PDFDocumentInfo, type PDFDocumentProxy, PDFHighlight, type PDFHighlightProps, type PDFHighlight$1 as PDFHighlightType, type PDFLoadingTask, type PDFOutlineItem, PDFPage, type PDFPageProps, type PDFPageProxy, type PDFPageViewport, type PDFRect, type PDFRenderContext, type PDFRenderTask, PDFSidebar, type PDFSidebarProps, type PDFTextContent, type PDFTextItem, PDFToolbar, type PDFToolbarProps, PDFUtils, PDFViewer, type PDFViewerProps, SAMPLE_HIGHLIGHT_TEXTS, SAMPLE_PDF_URLS, type SidebarView, type ToolbarTool, type ViewMode, type ZoomMode, cleanupBlobUrl, configurePDFWorker, createBlobUrl, createHighlightsForText, PDFViewer as default, downloadPDFFromUrl, extractDocumentInfo, getPageTextContent, getWorkerInfo, getWorkerRetryCount, getWorkerSrc, isWorkerConfiguredProperly, resetWorker, resetWorkerConfiguration, retryWorkerConfiguration, searchTextInPage, testWorker, useKeyboardShortcuts };