pdf-tsx 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +350 -337
- package/dist/cjs/components/PDFSidebar.cjs +1 -1
- package/dist/cjs/components/PDFSidebar.module.cjs +1 -1
- package/dist/cjs/components/PDFViewer.cjs +1 -1
- package/dist/cjs/components/PDFViewer.module.cjs +1 -1
- package/dist/cjs/components/features/PDFAnnotations.cjs +1 -1
- package/dist/cjs/components/features/PDFAnnotations.module.cjs +1 -1
- package/dist/cjs/components/features/PDFChangeFile.cjs +1 -1
- package/dist/cjs/components/features/PDFDownloadButton.cjs +1 -1
- package/dist/cjs/components/features/PDFNavigation.cjs +1 -1
- package/dist/cjs/components/features/PDFNavigation.module.cjs +1 -1
- package/dist/cjs/components/features/PDFOutline.cjs +1 -1
- package/dist/cjs/components/features/PDFOutline.module.cjs +1 -1
- package/dist/cjs/components/features/PDFPageFit.cjs +1 -1
- package/dist/cjs/components/features/PDFPrintButton.cjs +1 -1
- package/dist/cjs/components/features/PDFRotationControl.cjs +1 -1
- package/dist/cjs/components/features/PDFSearch.cjs +1 -1
- package/dist/cjs/components/features/PDFSearch.module.cjs +1 -1
- package/dist/cjs/components/features/PDFSidebarToggle.cjs +1 -1
- package/dist/cjs/components/features/PDFSignatures.cjs +1 -1
- package/dist/cjs/components/features/PDFSignatures.module.cjs +1 -1
- package/dist/cjs/components/features/PDFThemeToggle.cjs +1 -1
- package/dist/cjs/components/features/PDFThumbnails.cjs +1 -1
- package/dist/cjs/components/features/PDFThumbnails.module.cjs +1 -1
- package/dist/cjs/components/features/PDFZoomControls.cjs +1 -1
- package/dist/cjs/components/features/PDFZoomControls.module.cjs +1 -1
- package/dist/cjs/components/ui/ToolbarButton.module.cjs +1 -1
- package/dist/cjs/components/ui/icons.cjs +1 -1
- package/dist/cjs/context/ThemeContext.cjs +1 -1
- package/dist/cjs/context/themeContextValue.cjs +1 -0
- package/dist/cjs/hooks/useAnnotationLayer.cjs +1 -1
- package/dist/cjs/hooks/useAnnotations.cjs +1 -1
- package/dist/cjs/hooks/useKeyboardShortcuts.cjs +1 -1
- package/dist/cjs/hooks/useOutline.cjs +1 -1
- package/dist/cjs/hooks/useOverlayRects.cjs +1 -0
- package/dist/cjs/hooks/usePDFLoader.cjs +1 -0
- package/dist/cjs/hooks/usePDFRenderer.cjs +1 -0
- package/dist/cjs/hooks/useScrollTracking.cjs +1 -1
- package/dist/cjs/hooks/useTextLayer.cjs +1 -1
- package/dist/cjs/hooks/useTextSearch.cjs +1 -1
- package/dist/cjs/hooks/useTheme.cjs +1 -0
- package/dist/cjs/hooks/useThumbnails.cjs +1 -1
- package/dist/cjs/pdf-tsx.css +1 -1
- package/dist/components/PDFSidebar.d.ts +1 -2
- package/dist/components/features/PDFSidebarToggle.d.ts +1 -1
- package/dist/components/ui/icons.d.ts +0 -14
- package/dist/context/ThemeContext.d.ts +0 -6
- package/dist/context/themeContextValue.d.ts +7 -0
- package/dist/es/components/PDFSidebar.js +26 -33
- package/dist/es/components/PDFSidebar.module.js +9 -9
- package/dist/es/components/PDFViewer.js +240 -269
- package/dist/es/components/PDFViewer.module.js +15 -15
- package/dist/es/components/features/PDFAnnotations.js +57 -64
- package/dist/es/components/features/PDFAnnotations.module.js +12 -12
- package/dist/es/components/features/PDFChangeFile.js +3 -3
- package/dist/es/components/features/PDFDownloadButton.js +2 -2
- package/dist/es/components/features/PDFNavigation.js +3 -3
- package/dist/es/components/features/PDFNavigation.module.js +3 -3
- package/dist/es/components/features/PDFOutline.js +7 -7
- package/dist/es/components/features/PDFOutline.module.js +5 -5
- package/dist/es/components/features/PDFPageFit.js +1 -1
- package/dist/es/components/features/PDFPrintButton.js +5 -4
- package/dist/es/components/features/PDFRotationControl.js +1 -1
- package/dist/es/components/features/PDFSearch.js +4 -4
- package/dist/es/components/features/PDFSearch.module.js +5 -5
- package/dist/es/components/features/PDFSidebarToggle.js +11 -10
- package/dist/es/components/features/PDFSignatures.js +158 -116
- package/dist/es/components/features/PDFSignatures.module.js +16 -16
- package/dist/es/components/features/PDFThemeToggle.js +5 -5
- package/dist/es/components/features/PDFThumbnails.js +54 -30
- package/dist/es/components/features/PDFThumbnails.module.js +9 -7
- package/dist/es/components/features/PDFZoomControls.js +1 -1
- package/dist/es/components/features/PDFZoomControls.module.js +1 -1
- package/dist/es/components/ui/ToolbarButton.module.js +2 -2
- package/dist/es/components/ui/icons.js +1 -1
- package/dist/es/context/ThemeContext.js +20 -25
- package/dist/es/context/themeContextValue.js +5 -0
- package/dist/es/hooks/useAnnotationLayer.js +11 -11
- package/dist/es/hooks/useAnnotations.js +0 -1
- package/dist/es/hooks/useKeyboardShortcuts.js +12 -15
- package/dist/es/hooks/useOutline.js +31 -12
- package/dist/es/hooks/useOverlayRects.js +72 -0
- package/dist/es/hooks/usePDFLoader.js +26 -0
- package/dist/es/hooks/usePDFRenderer.js +83 -0
- package/dist/es/hooks/useScrollTracking.js +21 -20
- package/dist/es/hooks/useTextLayer.js +13 -12
- package/dist/es/hooks/useTextSearch.js +30 -18
- package/dist/es/hooks/useTheme.js +10 -0
- package/dist/es/hooks/useThumbnails.js +86 -24
- package/dist/es/pdf-tsx.css +1 -1
- package/dist/hooks/useAnnotationLayer.d.ts +2 -2
- package/dist/hooks/useAnnotations.d.ts +0 -5
- package/dist/hooks/useKeyboardShortcuts.d.ts +1 -2
- package/dist/hooks/useOverlayRects.d.ts +21 -0
- package/dist/hooks/usePDFLoader.d.ts +4 -0
- package/dist/hooks/usePDFRenderer.d.ts +16 -0
- package/dist/hooks/useScrollTracking.d.ts +2 -1
- package/dist/hooks/useTextLayer.d.ts +2 -2
- package/dist/hooks/useTheme.d.ts +2 -0
- package/dist/hooks/useThumbnails.d.ts +3 -2
- package/dist/pdf-tsx.cjs.js +5 -0
- package/dist/pdf-tsx.css +2 -0
- package/dist/pdf-tsx.es.js +2637 -0
- package/dist/vite.svg +1 -0
- package/package.json +61 -61
package/README.md
CHANGED
|
@@ -1,337 +1,350 @@
|
|
|
1
|
-
# pdf-tsx
|
|
2
|
-
|
|
3
|
-
A fully-featured, composable PDF viewer component for React.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install pdf-tsx
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
### Peer dependencies
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
npm install react react-dom pdfjs-dist
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
---
|
|
18
|
-
|
|
19
|
-
## CSS
|
|
20
|
-
|
|
21
|
-
Import the stylesheet once in your app entry point:
|
|
22
|
-
|
|
23
|
-
```ts
|
|
24
|
-
import 'pdf-tsx/dist/es/pdf-tsx.css'
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
---
|
|
28
|
-
|
|
29
|
-
## Quick start
|
|
30
|
-
|
|
31
|
-
`PDFViewer` requires a `workerSrc` prop — the URL of the pdfjs worker script. The easiest way is to serve it from the `pdfjs-dist` package directly:
|
|
32
|
-
|
|
33
|
-
```tsx
|
|
34
|
-
import { PDFViewer } from 'pdf-tsx'
|
|
35
|
-
import workerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?url'
|
|
36
|
-
|
|
37
|
-
function App() {
|
|
38
|
-
const [file, setFile] = useState<File | null>(null)
|
|
39
|
-
|
|
40
|
-
if (!file) return <input type="file" accept=".pdf" onChange={e => setFile(e.target.files![0])} />
|
|
41
|
-
|
|
42
|
-
return <PDFViewer file={file} workerSrc={workerUrl} />
|
|
43
|
-
}
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
> The `?url` suffix is a Vite feature that returns the asset URL as a string. If you use a different bundler, import the worker URL accordingly or pass a CDN URL:
|
|
47
|
-
> ```ts
|
|
48
|
-
> workerSrc="https://unpkg.com/pdfjs-dist@5/build/pdf.worker.min.mjs"
|
|
49
|
-
> ```
|
|
50
|
-
|
|
51
|
-
---
|
|
52
|
-
|
|
53
|
-
## Icons
|
|
54
|
-
|
|
55
|
-
Feature components do not bundle any icon library. Each component that shows an icon requires you to pass it explicitly as a prop — use any icon library or custom SVG you prefer.
|
|
56
|
-
|
|
57
|
-
```tsx
|
|
58
|
-
import { FiZoomIn, FiZoomOut } from 'react-icons/fi'
|
|
59
|
-
|
|
60
|
-
<PDFZoomControls zoomInIcon={<FiZoomIn size={16} />} zoomOutIcon={<FiZoomOut size={16} />} />
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
---
|
|
64
|
-
|
|
65
|
-
## PDFViewer
|
|
66
|
-
|
|
67
|
-
The main component. Must wrap all feature components.
|
|
68
|
-
|
|
69
|
-
```tsx
|
|
70
|
-
<PDFViewer
|
|
71
|
-
file={file}
|
|
72
|
-
workerSrc={workerUrl}
|
|
73
|
-
sidebar={<PDFSidebar panels={[...]} />}
|
|
74
|
-
toolbar={<>...</>}
|
|
75
|
-
onChangeFile={(newFile) => setFile(newFile)}
|
|
76
|
-
/>
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
| Prop | Type | Required | Description |
|
|
80
|
-
|------------------|---------------------------------------------------------|----------|------------------------------------------------------|
|
|
81
|
-
| `file` | `File` | yes | The PDF file object to display |
|
|
82
|
-
| `workerSrc` | `string` | yes | URL of the pdfjs worker script (see Quick start) |
|
|
83
|
-
| `sidebar` | `ReactNode` | no | Content rendered on the left side |
|
|
84
|
-
| `toolbar` | `ReactNode` | no | Content rendered in the top toolbar |
|
|
85
|
-
| `onChangeFile` | `(file: File) => void` | no | Called when the user opens a different file |
|
|
86
|
-
| `defaultTheme` | `"light" \| "dark"` | no | Initial theme mode (default: `"light"`) |
|
|
87
|
-
| `themeOverrides` | `{ light?: Partial<Theme>; dark?: Partial<Theme> }` | no | Override individual theme tokens (see Theming) |
|
|
88
|
-
| `defaultFit` | `"width" \| "page"` | no | Initial zoom mode when a PDF is loaded |
|
|
89
|
-
| `className` | `string` | no | CSS class applied to the root layout element |
|
|
90
|
-
| `style` | `React.CSSProperties` | no | Inline style applied to the root layout element |
|
|
91
|
-
|
|
92
|
-
---
|
|
93
|
-
|
|
94
|
-
## Theming
|
|
95
|
-
|
|
96
|
-
`PDFViewer` ships with built-in light and dark themes. The default is `"light"`. Use `defaultTheme` to set the initial mode and `themeOverrides` to override individual tokens without replacing the whole theme.
|
|
97
|
-
|
|
98
|
-
```tsx
|
|
99
|
-
import { PDFViewer, darkTheme } from 'pdf-tsx'
|
|
100
|
-
|
|
101
|
-
<PDFViewer
|
|
102
|
-
themeOverrides={{
|
|
103
|
-
dark: { accent: '#e11d48', accentHover: '#be123c', accentDark: '#9f1239' },
|
|
104
|
-
light: { accent: '#e11d48', accentHover: '#be123c', accentDark: '#9f1239' },
|
|
105
|
-
}}
|
|
106
|
-
...
|
|
107
|
-
/>
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
Both `darkTheme` and `lightTheme` are exported so you can spread them as a base:
|
|
111
|
-
|
|
112
|
-
```tsx
|
|
113
|
-
import { darkTheme } from 'pdf-tsx'
|
|
114
|
-
|
|
115
|
-
themeOverrides={{ dark: { ...darkTheme, bg: '#0f0f0f' } }}
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
**Theme tokens** (`Theme` interface):
|
|
119
|
-
|
|
120
|
-
| Token | Description |
|
|
121
|
-
|--------------------|--------------------------------------|
|
|
122
|
-
| `bg` | Main background |
|
|
123
|
-
| `surface` | Card / panel surface |
|
|
124
|
-
| `surfaceHover` | Surface hover state |
|
|
125
|
-
| `border` | Default border color |
|
|
126
|
-
| `borderLight` | Subtle border color |
|
|
127
|
-
| `scrollArea` | Scroll container background |
|
|
128
|
-
| `textPrimary` | Primary text |
|
|
129
|
-
| `textSecondary` | Secondary text |
|
|
130
|
-
| `textMuted` | Muted / placeholder text |
|
|
131
|
-
| `textPlaceholder` | Input placeholder text |
|
|
132
|
-
| `accent` | Accent / interactive color |
|
|
133
|
-
| `accentHover` | Accent hover state |
|
|
134
|
-
| `accentDark` | Accent dark variant |
|
|
135
|
-
| `accentLight` | Accent light variant |
|
|
136
|
-
| `accentSubtle` | Accent with low opacity |
|
|
137
|
-
| `panelBg` | Sidebar panel background |
|
|
138
|
-
| `panelBorder` | Sidebar panel border |
|
|
139
|
-
| `panelHeaderBg` | Sidebar panel header background |
|
|
140
|
-
| `shadowPage` | Drop shadow color for PDF pages |
|
|
141
|
-
|
|
142
|
-
---
|
|
143
|
-
|
|
144
|
-
## PDFSidebar
|
|
145
|
-
|
|
146
|
-
A collapsible icon sidebar. Each panel has an icon button — clicking it toggles the panel open/closed. When collapsed via `PDFSidebarToggle`, the entire sidebar is hidden (`null`).
|
|
147
|
-
|
|
148
|
-
```tsx
|
|
149
|
-
import { PDFSidebar } from 'pdf-tsx'
|
|
150
|
-
import { FiImage, FiList, FiChevronsLeft } from 'react-icons/fi'
|
|
151
|
-
|
|
152
|
-
<PDFSidebar
|
|
153
|
-
collapseIcon={<FiChevronsLeft size={16} />}
|
|
154
|
-
panels={[
|
|
155
|
-
{ icon: <FiImage size={18} />, label: 'Thumbnails', content: <PDFThumbnails /> },
|
|
156
|
-
{ icon: <FiList size={18} />, label: 'Outline', content: <PDFOutline /> },
|
|
157
|
-
]}
|
|
158
|
-
/>
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
| Prop | Type | Description |
|
|
162
|
-
|----------------|------------------|------------------------------------------------------------------|
|
|
163
|
-
| `panels` | `SidebarPanel[]` | Array of panels to display |
|
|
164
|
-
| `collapseIcon` | `ReactNode` | If provided, renders a toggle button at the top of the icon bar to hide the sidebar |
|
|
165
|
-
|
|
166
|
-
**SidebarPanel**
|
|
167
|
-
|
|
168
|
-
| Field | Type | Description |
|
|
169
|
-
|-----------|-------------|----------------------------------------|
|
|
170
|
-
| `icon` | `ReactNode` | Icon shown in the sidebar button bar |
|
|
171
|
-
| `label` | `string` | Panel title shown in the header |
|
|
172
|
-
| `content` | `ReactNode` | Content rendered inside the open panel |
|
|
173
|
-
|
|
174
|
-
### Sidebar hide/show pattern
|
|
175
|
-
|
|
176
|
-
Use `PDFSidebarToggle` in the toolbar alongside `PDFSidebar`. When the sidebar is open, the collapse button lives inside the sidebar. When collapsed, the expand button appears in the toolbar.
|
|
177
|
-
|
|
178
|
-
```tsx
|
|
179
|
-
import { PDFSidebar, PDFSidebarToggle } from 'pdf-tsx'
|
|
180
|
-
import { FiChevronsLeft, FiChevronsRight } from 'react-icons/fi'
|
|
181
|
-
|
|
182
|
-
<PDFViewer
|
|
183
|
-
sidebar={
|
|
184
|
-
<PDFSidebar
|
|
185
|
-
collapseIcon={<FiChevronsLeft size={16} />}
|
|
186
|
-
panels={[...]}
|
|
187
|
-
/>
|
|
188
|
-
}
|
|
189
|
-
toolbar={
|
|
190
|
-
<>
|
|
191
|
-
<PDFSidebarToggle
|
|
192
|
-
collapseIcon={<FiChevronsLeft size={16} />}
|
|
193
|
-
expandIcon={<FiChevronsRight size={16} />}
|
|
194
|
-
/>
|
|
195
|
-
{/* other toolbar items */}
|
|
196
|
-
</>
|
|
197
|
-
}
|
|
198
|
-
/>
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
---
|
|
202
|
-
|
|
203
|
-
## Feature components
|
|
204
|
-
|
|
205
|
-
All feature components must be rendered inside `<PDFViewer>`. They read and write state via `usePDFViewer()` internally.
|
|
206
|
-
|
|
207
|
-
| Component | Icon props | Description |
|
|
208
|
-
|------------------------|-----------------------------------------------------|------------------------------------------------------|
|
|
209
|
-
| `PDFThumbnails` | — | Page thumbnail strip for quick navigation |
|
|
210
|
-
| `PDFOutline` | — | Document outline / bookmarks tree |
|
|
211
|
-
| `PDFAnnotations` | — | Display PDF annotations |
|
|
212
|
-
| `PDFAnnotationsIcon` | `icon` | Sidebar icon with annotation count badge |
|
|
213
|
-
| `PDFSignatures` | — | Display PDF digital signatures |
|
|
214
|
-
| `PDFSignaturesIcon` | `icon` | Sidebar icon with signature count badge |
|
|
215
|
-
| `PDFSearch` | `searchIcon` `prevIcon` `nextIcon` `clearIcon` | Full-text search with match highlighting |
|
|
216
|
-
| `PDFZoomControls` | `zoomInIcon` `zoomOutIcon` | Zoom in / zoom out buttons with percentage display |
|
|
217
|
-
| `PDFPageFit` | `fitWidthIcon` `fitPageIcon` | Fit-to-width and fit-to-page buttons |
|
|
218
|
-
| `PDFRotationControl` | `icon` | Rotate page clockwise |
|
|
219
|
-
| `PDFNavigation` | `prevIcon` `nextIcon` | Previous / next page buttons with current page input |
|
|
220
|
-
| `PDFDownloadButton` | `icon` | Download the current PDF file |
|
|
221
|
-
| `PDFChangeFile` | `icon` | Button to open a new PDF file |
|
|
222
|
-
| `PDFThemeToggle` | `lightIcon` `darkIcon` | Toggle between dark and light mode |
|
|
223
|
-
| `PDFPrintButton` | `icon` | Print the PDF using the browser's native print dialog|
|
|
224
|
-
| `PDFSidebarToggle` | `collapseIcon` `expandIcon` | Toolbar button to expand the sidebar — visible only when sidebar is hidden (see Sidebar hide/show pattern) |
|
|
225
|
-
|
|
226
|
-
### Count badges
|
|
227
|
-
|
|
228
|
-
`PDFSignaturesIcon` and `PDFAnnotationsIcon` are icon wrapper components intended for use as the `icon` prop in `PDFSidebar` panels. They render the provided icon with a small count badge in the top-right corner, automatically populated from the loaded PDF.
|
|
229
|
-
|
|
230
|
-
```tsx
|
|
231
|
-
import { PDFSidebar, PDFSignatures, PDFSignaturesIcon, PDFAnnotations, PDFAnnotationsIcon } from 'pdf-tsx'
|
|
232
|
-
import { FiPenTool, FiMessageSquare } from 'react-icons/fi'
|
|
233
|
-
|
|
234
|
-
<PDFSidebar panels={[
|
|
235
|
-
{
|
|
236
|
-
icon: <PDFSignaturesIcon icon={<FiPenTool size={17} />} />,
|
|
237
|
-
label: 'Signatures',
|
|
238
|
-
content: <PDFSignatures />,
|
|
239
|
-
},
|
|
240
|
-
{
|
|
241
|
-
icon: <PDFAnnotationsIcon icon={<FiMessageSquare size={17} />} />,
|
|
242
|
-
label: 'Annotations',
|
|
243
|
-
content: <PDFAnnotations />,
|
|
244
|
-
},
|
|
245
|
-
]} />
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
The badge uses `var(--pdf-accent)` and respects `themeOverrides`.
|
|
249
|
-
|
|
250
|
-
---
|
|
251
|
-
|
|
252
|
-
## Full example
|
|
253
|
-
|
|
254
|
-
```tsx
|
|
255
|
-
import { useState, Suspense, lazy } from 'react'
|
|
256
|
-
import {
|
|
257
|
-
PDFSidebar, PDFSidebarToggle, PDFThumbnails, PDFOutline, PDFSearch,
|
|
258
|
-
PDFZoomControls, PDFPageFit, PDFRotationControl,
|
|
259
|
-
PDFNavigation, PDFDownloadButton, PDFChangeFile,
|
|
260
|
-
PDFThemeToggle, PDFPrintButton,
|
|
261
|
-
PDFSignatures, PDFSignaturesIcon,
|
|
262
|
-
PDFAnnotations, PDFAnnotationsIcon,
|
|
263
|
-
} from 'pdf-tsx'
|
|
264
|
-
import {
|
|
265
|
-
FiImage, FiList, FiPenTool, FiMessageSquare,
|
|
266
|
-
FiSearch, FiChevronUp, FiChevronDown, FiX,
|
|
267
|
-
FiZoomIn, FiZoomOut, FiAlignJustify, FiMaximize2, FiRotateCw,
|
|
268
|
-
FiChevronLeft, FiChevronRight, FiDownload, FiFolderPlus,
|
|
269
|
-
FiPrinter, FiSun, FiMoon, FiChevronsLeft, FiChevronsRight,
|
|
270
|
-
} from 'react-icons/fi'
|
|
271
|
-
import workerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?url'
|
|
272
|
-
|
|
273
|
-
const PDFViewer = lazy(() => import('pdf-tsx').then(m => ({ default: m.PDFViewer })))
|
|
274
|
-
|
|
275
|
-
function App() {
|
|
276
|
-
const [file, setFile] = useState<File | null>(null)
|
|
277
|
-
|
|
278
|
-
if (!file) {
|
|
279
|
-
return (
|
|
280
|
-
<input
|
|
281
|
-
type="file"
|
|
282
|
-
accept=".pdf"
|
|
283
|
-
onChange={e => setFile(e.target.files![0])}
|
|
284
|
-
/>
|
|
285
|
-
)
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
return (
|
|
289
|
-
<Suspense fallback={<div>Loading...</div>}>
|
|
290
|
-
<PDFViewer
|
|
291
|
-
file={file}
|
|
292
|
-
workerSrc={workerUrl}
|
|
293
|
-
onChangeFile={setFile}
|
|
294
|
-
sidebar={
|
|
295
|
-
<PDFSidebar
|
|
296
|
-
collapseIcon={<FiChevronsLeft size={16} />}
|
|
297
|
-
panels={[
|
|
298
|
-
{ icon: <FiImage size={18} />, label: 'Thumbnails', content: <PDFThumbnails /> },
|
|
299
|
-
{ icon: <FiList size={18} />, label: 'Outline', content: <PDFOutline /> },
|
|
300
|
-
{ icon: <PDFSignaturesIcon icon={<FiPenTool size={17} />} />, label: 'Signatures', content: <PDFSignatures /> },
|
|
301
|
-
{ icon: <PDFAnnotationsIcon icon={<FiMessageSquare size={17} />} />, label: 'Annotations', content: <PDFAnnotations /> },
|
|
302
|
-
]}
|
|
303
|
-
/>
|
|
304
|
-
}
|
|
305
|
-
toolbar={
|
|
306
|
-
<>
|
|
307
|
-
<PDFSidebarToggle
|
|
308
|
-
collapseIcon={<FiChevronsLeft size={16} />}
|
|
309
|
-
expandIcon={<FiChevronsRight size={16} />}
|
|
310
|
-
/>
|
|
311
|
-
<PDFSearch
|
|
312
|
-
searchIcon={<FiSearch size={14} />}
|
|
313
|
-
prevIcon={<FiChevronUp size={14} />}
|
|
314
|
-
nextIcon={<FiChevronDown size={14} />}
|
|
315
|
-
clearIcon={<FiX size={14} />}
|
|
316
|
-
/>
|
|
317
|
-
<PDFZoomControls zoomOutIcon={<FiZoomOut size={16} />} zoomInIcon={<FiZoomIn size={16} />} />
|
|
318
|
-
<PDFPageFit fitWidthIcon={<FiAlignJustify size={16} />} fitPageIcon={<FiMaximize2 size={16} />} />
|
|
319
|
-
<PDFRotationControl icon={<FiRotateCw size={16} />} />
|
|
320
|
-
<PDFNavigation prevIcon={<FiChevronLeft size={16} />} nextIcon={<FiChevronRight size={16} />} />
|
|
321
|
-
<PDFChangeFile icon={<FiFolderPlus size={16} />} />
|
|
322
|
-
<PDFDownloadButton icon={<FiDownload size={15} />} />
|
|
323
|
-
<PDFThemeToggle lightIcon={<FiSun size={16} />} darkIcon={<FiMoon size={16} />} />
|
|
324
|
-
<PDFPrintButton icon={<FiPrinter size={16} />} />
|
|
325
|
-
</>
|
|
326
|
-
}
|
|
327
|
-
/>
|
|
328
|
-
</Suspense>
|
|
329
|
-
)
|
|
330
|
-
}
|
|
331
|
-
```
|
|
332
|
-
|
|
333
|
-
---
|
|
334
|
-
|
|
335
|
-
##
|
|
336
|
-
|
|
337
|
-
|
|
1
|
+
# pdf-tsx
|
|
2
|
+
|
|
3
|
+
A fully-featured, composable PDF viewer component for React.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install pdf-tsx
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### Peer dependencies
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install react react-dom pdfjs-dist
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## CSS
|
|
20
|
+
|
|
21
|
+
Import the stylesheet once in your app entry point:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import 'pdf-tsx/dist/es/pdf-tsx.css'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Quick start
|
|
30
|
+
|
|
31
|
+
`PDFViewer` requires a `workerSrc` prop — the URL of the pdfjs worker script. The easiest way is to serve it from the `pdfjs-dist` package directly:
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { PDFViewer } from 'pdf-tsx'
|
|
35
|
+
import workerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?url'
|
|
36
|
+
|
|
37
|
+
function App() {
|
|
38
|
+
const [file, setFile] = useState<File | null>(null)
|
|
39
|
+
|
|
40
|
+
if (!file) return <input type="file" accept=".pdf" onChange={e => setFile(e.target.files![0])} />
|
|
41
|
+
|
|
42
|
+
return <PDFViewer file={file} workerSrc={workerUrl} />
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
> The `?url` suffix is a Vite feature that returns the asset URL as a string. If you use a different bundler, import the worker URL accordingly or pass a CDN URL:
|
|
47
|
+
> ```ts
|
|
48
|
+
> workerSrc="https://unpkg.com/pdfjs-dist@5/build/pdf.worker.min.mjs"
|
|
49
|
+
> ```
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Icons
|
|
54
|
+
|
|
55
|
+
Feature components do not bundle any icon library. Each component that shows an icon requires you to pass it explicitly as a prop — use any icon library or custom SVG you prefer.
|
|
56
|
+
|
|
57
|
+
```tsx
|
|
58
|
+
import { FiZoomIn, FiZoomOut } from 'react-icons/fi'
|
|
59
|
+
|
|
60
|
+
<PDFZoomControls zoomInIcon={<FiZoomIn size={16} />} zoomOutIcon={<FiZoomOut size={16} />} />
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## PDFViewer
|
|
66
|
+
|
|
67
|
+
The main component. Must wrap all feature components.
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
<PDFViewer
|
|
71
|
+
file={file}
|
|
72
|
+
workerSrc={workerUrl}
|
|
73
|
+
sidebar={<PDFSidebar panels={[...]} />}
|
|
74
|
+
toolbar={<>...</>}
|
|
75
|
+
onChangeFile={(newFile) => setFile(newFile)}
|
|
76
|
+
/>
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
| Prop | Type | Required | Description |
|
|
80
|
+
|------------------|---------------------------------------------------------|----------|------------------------------------------------------|
|
|
81
|
+
| `file` | `File` | yes | The PDF file object to display |
|
|
82
|
+
| `workerSrc` | `string` | yes | URL of the pdfjs worker script (see Quick start) |
|
|
83
|
+
| `sidebar` | `ReactNode` | no | Content rendered on the left side |
|
|
84
|
+
| `toolbar` | `ReactNode` | no | Content rendered in the top toolbar |
|
|
85
|
+
| `onChangeFile` | `(file: File) => void` | no | Called when the user opens a different file |
|
|
86
|
+
| `defaultTheme` | `"light" \| "dark"` | no | Initial theme mode (default: `"light"`) |
|
|
87
|
+
| `themeOverrides` | `{ light?: Partial<Theme>; dark?: Partial<Theme> }` | no | Override individual theme tokens (see Theming) |
|
|
88
|
+
| `defaultFit` | `"width" \| "page"` | no | Initial zoom mode when a PDF is loaded |
|
|
89
|
+
| `className` | `string` | no | CSS class applied to the root layout element |
|
|
90
|
+
| `style` | `React.CSSProperties` | no | Inline style applied to the root layout element |
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## Theming
|
|
95
|
+
|
|
96
|
+
`PDFViewer` ships with built-in light and dark themes. The default is `"light"`. Use `defaultTheme` to set the initial mode and `themeOverrides` to override individual tokens without replacing the whole theme.
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
import { PDFViewer, darkTheme } from 'pdf-tsx'
|
|
100
|
+
|
|
101
|
+
<PDFViewer
|
|
102
|
+
themeOverrides={{
|
|
103
|
+
dark: { accent: '#e11d48', accentHover: '#be123c', accentDark: '#9f1239' },
|
|
104
|
+
light: { accent: '#e11d48', accentHover: '#be123c', accentDark: '#9f1239' },
|
|
105
|
+
}}
|
|
106
|
+
...
|
|
107
|
+
/>
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Both `darkTheme` and `lightTheme` are exported so you can spread them as a base:
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
import { darkTheme } from 'pdf-tsx'
|
|
114
|
+
|
|
115
|
+
themeOverrides={{ dark: { ...darkTheme, bg: '#0f0f0f' } }}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**Theme tokens** (`Theme` interface):
|
|
119
|
+
|
|
120
|
+
| Token | Description |
|
|
121
|
+
|--------------------|--------------------------------------|
|
|
122
|
+
| `bg` | Main background |
|
|
123
|
+
| `surface` | Card / panel surface |
|
|
124
|
+
| `surfaceHover` | Surface hover state |
|
|
125
|
+
| `border` | Default border color |
|
|
126
|
+
| `borderLight` | Subtle border color |
|
|
127
|
+
| `scrollArea` | Scroll container background |
|
|
128
|
+
| `textPrimary` | Primary text |
|
|
129
|
+
| `textSecondary` | Secondary text |
|
|
130
|
+
| `textMuted` | Muted / placeholder text |
|
|
131
|
+
| `textPlaceholder` | Input placeholder text |
|
|
132
|
+
| `accent` | Accent / interactive color |
|
|
133
|
+
| `accentHover` | Accent hover state |
|
|
134
|
+
| `accentDark` | Accent dark variant |
|
|
135
|
+
| `accentLight` | Accent light variant |
|
|
136
|
+
| `accentSubtle` | Accent with low opacity |
|
|
137
|
+
| `panelBg` | Sidebar panel background |
|
|
138
|
+
| `panelBorder` | Sidebar panel border |
|
|
139
|
+
| `panelHeaderBg` | Sidebar panel header background |
|
|
140
|
+
| `shadowPage` | Drop shadow color for PDF pages |
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## PDFSidebar
|
|
145
|
+
|
|
146
|
+
A collapsible icon sidebar. Each panel has an icon button — clicking it toggles the panel open/closed. When collapsed via `PDFSidebarToggle`, the entire sidebar is hidden (`null`).
|
|
147
|
+
|
|
148
|
+
```tsx
|
|
149
|
+
import { PDFSidebar } from 'pdf-tsx'
|
|
150
|
+
import { FiImage, FiList, FiChevronsLeft } from 'react-icons/fi'
|
|
151
|
+
|
|
152
|
+
<PDFSidebar
|
|
153
|
+
collapseIcon={<FiChevronsLeft size={16} />}
|
|
154
|
+
panels={[
|
|
155
|
+
{ icon: <FiImage size={18} />, label: 'Thumbnails', content: <PDFThumbnails /> },
|
|
156
|
+
{ icon: <FiList size={18} />, label: 'Outline', content: <PDFOutline /> },
|
|
157
|
+
]}
|
|
158
|
+
/>
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
| Prop | Type | Description |
|
|
162
|
+
|----------------|------------------|------------------------------------------------------------------|
|
|
163
|
+
| `panels` | `SidebarPanel[]` | Array of panels to display |
|
|
164
|
+
| `collapseIcon` | `ReactNode` | If provided, renders a toggle button at the top of the icon bar to hide the sidebar |
|
|
165
|
+
|
|
166
|
+
**SidebarPanel**
|
|
167
|
+
|
|
168
|
+
| Field | Type | Description |
|
|
169
|
+
|-----------|-------------|----------------------------------------|
|
|
170
|
+
| `icon` | `ReactNode` | Icon shown in the sidebar button bar |
|
|
171
|
+
| `label` | `string` | Panel title shown in the header |
|
|
172
|
+
| `content` | `ReactNode` | Content rendered inside the open panel |
|
|
173
|
+
|
|
174
|
+
### Sidebar hide/show pattern
|
|
175
|
+
|
|
176
|
+
Use `PDFSidebarToggle` in the toolbar alongside `PDFSidebar`. When the sidebar is open, the collapse button lives inside the sidebar. When collapsed, the expand button appears in the toolbar.
|
|
177
|
+
|
|
178
|
+
```tsx
|
|
179
|
+
import { PDFSidebar, PDFSidebarToggle } from 'pdf-tsx'
|
|
180
|
+
import { FiChevronsLeft, FiChevronsRight } from 'react-icons/fi'
|
|
181
|
+
|
|
182
|
+
<PDFViewer
|
|
183
|
+
sidebar={
|
|
184
|
+
<PDFSidebar
|
|
185
|
+
collapseIcon={<FiChevronsLeft size={16} />}
|
|
186
|
+
panels={[...]}
|
|
187
|
+
/>
|
|
188
|
+
}
|
|
189
|
+
toolbar={
|
|
190
|
+
<>
|
|
191
|
+
<PDFSidebarToggle
|
|
192
|
+
collapseIcon={<FiChevronsLeft size={16} />}
|
|
193
|
+
expandIcon={<FiChevronsRight size={16} />}
|
|
194
|
+
/>
|
|
195
|
+
{/* other toolbar items */}
|
|
196
|
+
</>
|
|
197
|
+
}
|
|
198
|
+
/>
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## Feature components
|
|
204
|
+
|
|
205
|
+
All feature components must be rendered inside `<PDFViewer>`. They read and write state via `usePDFViewer()` internally.
|
|
206
|
+
|
|
207
|
+
| Component | Icon props | Description |
|
|
208
|
+
|------------------------|-----------------------------------------------------|------------------------------------------------------|
|
|
209
|
+
| `PDFThumbnails` | — | Page thumbnail strip for quick navigation |
|
|
210
|
+
| `PDFOutline` | — | Document outline / bookmarks tree |
|
|
211
|
+
| `PDFAnnotations` | — | Display PDF annotations |
|
|
212
|
+
| `PDFAnnotationsIcon` | `icon` | Sidebar icon with annotation count badge |
|
|
213
|
+
| `PDFSignatures` | — | Display PDF digital signatures |
|
|
214
|
+
| `PDFSignaturesIcon` | `icon` | Sidebar icon with signature count badge |
|
|
215
|
+
| `PDFSearch` | `searchIcon` `prevIcon` `nextIcon` `clearIcon` | Full-text search with match highlighting |
|
|
216
|
+
| `PDFZoomControls` | `zoomInIcon` `zoomOutIcon` | Zoom in / zoom out buttons with percentage display |
|
|
217
|
+
| `PDFPageFit` | `fitWidthIcon` `fitPageIcon` | Fit-to-width and fit-to-page buttons |
|
|
218
|
+
| `PDFRotationControl` | `icon` | Rotate page clockwise |
|
|
219
|
+
| `PDFNavigation` | `prevIcon` `nextIcon` | Previous / next page buttons with current page input |
|
|
220
|
+
| `PDFDownloadButton` | `icon` | Download the current PDF file |
|
|
221
|
+
| `PDFChangeFile` | `icon` | Button to open a new PDF file |
|
|
222
|
+
| `PDFThemeToggle` | `lightIcon` `darkIcon` | Toggle between dark and light mode |
|
|
223
|
+
| `PDFPrintButton` | `icon` | Print the PDF using the browser's native print dialog|
|
|
224
|
+
| `PDFSidebarToggle` | `collapseIcon` `expandIcon` | Toolbar button to expand the sidebar — visible only when sidebar is hidden (see Sidebar hide/show pattern) |
|
|
225
|
+
|
|
226
|
+
### Count badges
|
|
227
|
+
|
|
228
|
+
`PDFSignaturesIcon` and `PDFAnnotationsIcon` are icon wrapper components intended for use as the `icon` prop in `PDFSidebar` panels. They render the provided icon with a small count badge in the top-right corner, automatically populated from the loaded PDF.
|
|
229
|
+
|
|
230
|
+
```tsx
|
|
231
|
+
import { PDFSidebar, PDFSignatures, PDFSignaturesIcon, PDFAnnotations, PDFAnnotationsIcon } from 'pdf-tsx'
|
|
232
|
+
import { FiPenTool, FiMessageSquare } from 'react-icons/fi'
|
|
233
|
+
|
|
234
|
+
<PDFSidebar panels={[
|
|
235
|
+
{
|
|
236
|
+
icon: <PDFSignaturesIcon icon={<FiPenTool size={17} />} />,
|
|
237
|
+
label: 'Signatures',
|
|
238
|
+
content: <PDFSignatures />,
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
icon: <PDFAnnotationsIcon icon={<FiMessageSquare size={17} />} />,
|
|
242
|
+
label: 'Annotations',
|
|
243
|
+
content: <PDFAnnotations />,
|
|
244
|
+
},
|
|
245
|
+
]} />
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
The badge uses `var(--pdf-accent)` and respects `themeOverrides`.
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## Full example
|
|
253
|
+
|
|
254
|
+
```tsx
|
|
255
|
+
import { useState, Suspense, lazy } from 'react'
|
|
256
|
+
import {
|
|
257
|
+
PDFSidebar, PDFSidebarToggle, PDFThumbnails, PDFOutline, PDFSearch,
|
|
258
|
+
PDFZoomControls, PDFPageFit, PDFRotationControl,
|
|
259
|
+
PDFNavigation, PDFDownloadButton, PDFChangeFile,
|
|
260
|
+
PDFThemeToggle, PDFPrintButton,
|
|
261
|
+
PDFSignatures, PDFSignaturesIcon,
|
|
262
|
+
PDFAnnotations, PDFAnnotationsIcon,
|
|
263
|
+
} from 'pdf-tsx'
|
|
264
|
+
import {
|
|
265
|
+
FiImage, FiList, FiPenTool, FiMessageSquare,
|
|
266
|
+
FiSearch, FiChevronUp, FiChevronDown, FiX,
|
|
267
|
+
FiZoomIn, FiZoomOut, FiAlignJustify, FiMaximize2, FiRotateCw,
|
|
268
|
+
FiChevronLeft, FiChevronRight, FiDownload, FiFolderPlus,
|
|
269
|
+
FiPrinter, FiSun, FiMoon, FiChevronsLeft, FiChevronsRight,
|
|
270
|
+
} from 'react-icons/fi'
|
|
271
|
+
import workerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?url'
|
|
272
|
+
|
|
273
|
+
const PDFViewer = lazy(() => import('pdf-tsx').then(m => ({ default: m.PDFViewer })))
|
|
274
|
+
|
|
275
|
+
function App() {
|
|
276
|
+
const [file, setFile] = useState<File | null>(null)
|
|
277
|
+
|
|
278
|
+
if (!file) {
|
|
279
|
+
return (
|
|
280
|
+
<input
|
|
281
|
+
type="file"
|
|
282
|
+
accept=".pdf"
|
|
283
|
+
onChange={e => setFile(e.target.files![0])}
|
|
284
|
+
/>
|
|
285
|
+
)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return (
|
|
289
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
290
|
+
<PDFViewer
|
|
291
|
+
file={file}
|
|
292
|
+
workerSrc={workerUrl}
|
|
293
|
+
onChangeFile={setFile}
|
|
294
|
+
sidebar={
|
|
295
|
+
<PDFSidebar
|
|
296
|
+
collapseIcon={<FiChevronsLeft size={16} />}
|
|
297
|
+
panels={[
|
|
298
|
+
{ icon: <FiImage size={18} />, label: 'Thumbnails', content: <PDFThumbnails /> },
|
|
299
|
+
{ icon: <FiList size={18} />, label: 'Outline', content: <PDFOutline /> },
|
|
300
|
+
{ icon: <PDFSignaturesIcon icon={<FiPenTool size={17} />} />, label: 'Signatures', content: <PDFSignatures /> },
|
|
301
|
+
{ icon: <PDFAnnotationsIcon icon={<FiMessageSquare size={17} />} />, label: 'Annotations', content: <PDFAnnotations /> },
|
|
302
|
+
]}
|
|
303
|
+
/>
|
|
304
|
+
}
|
|
305
|
+
toolbar={
|
|
306
|
+
<>
|
|
307
|
+
<PDFSidebarToggle
|
|
308
|
+
collapseIcon={<FiChevronsLeft size={16} />}
|
|
309
|
+
expandIcon={<FiChevronsRight size={16} />}
|
|
310
|
+
/>
|
|
311
|
+
<PDFSearch
|
|
312
|
+
searchIcon={<FiSearch size={14} />}
|
|
313
|
+
prevIcon={<FiChevronUp size={14} />}
|
|
314
|
+
nextIcon={<FiChevronDown size={14} />}
|
|
315
|
+
clearIcon={<FiX size={14} />}
|
|
316
|
+
/>
|
|
317
|
+
<PDFZoomControls zoomOutIcon={<FiZoomOut size={16} />} zoomInIcon={<FiZoomIn size={16} />} />
|
|
318
|
+
<PDFPageFit fitWidthIcon={<FiAlignJustify size={16} />} fitPageIcon={<FiMaximize2 size={16} />} />
|
|
319
|
+
<PDFRotationControl icon={<FiRotateCw size={16} />} />
|
|
320
|
+
<PDFNavigation prevIcon={<FiChevronLeft size={16} />} nextIcon={<FiChevronRight size={16} />} />
|
|
321
|
+
<PDFChangeFile icon={<FiFolderPlus size={16} />} />
|
|
322
|
+
<PDFDownloadButton icon={<FiDownload size={15} />} />
|
|
323
|
+
<PDFThemeToggle lightIcon={<FiSun size={16} />} darkIcon={<FiMoon size={16} />} />
|
|
324
|
+
<PDFPrintButton icon={<FiPrinter size={16} />} />
|
|
325
|
+
</>
|
|
326
|
+
}
|
|
327
|
+
/>
|
|
328
|
+
</Suspense>
|
|
329
|
+
)
|
|
330
|
+
}
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
---
|
|
334
|
+
|
|
335
|
+
## Performance
|
|
336
|
+
|
|
337
|
+
`pdf-tsx` uses **virtual rendering**: only the pages near the current view are rendered at any one time. The default window is the current page ±2. Pages with an active highlight or search match are always included regardless of position.
|
|
338
|
+
|
|
339
|
+
This means:
|
|
340
|
+
- Initial load renders at most 5 pages, regardless of document length
|
|
341
|
+
- Memory and CPU usage stay flat as the user scrolls through a large document
|
|
342
|
+
- Pages that have already been rendered are kept in the DOM as the user scrolls, avoiding redundant re-renders
|
|
343
|
+
|
|
344
|
+
No configuration is needed — this is the default behaviour.
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
348
|
+
## License
|
|
349
|
+
|
|
350
|
+
MIT © Riccardo Grossano — see [LICENSE](./LICENSE) for full terms.
|