pdf-tsx 0.12.0 → 0.14.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/README.md CHANGED
@@ -1,459 +1,486 @@
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
- | `language` | `"it" \| "en"` | no | UI language (default: `"en"`) |
90
- | `className` | `string` | no | CSS class applied to the root layout element |
91
- | `style` | `React.CSSProperties` | no | Inline style applied to the root layout element |
92
-
93
- ---
94
-
95
- ## Theming
96
-
97
- `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.
98
-
99
- ```tsx
100
- import { PDFViewer, darkTheme } from 'pdf-tsx'
101
-
102
- <PDFViewer
103
- themeOverrides={{
104
- dark: { accent: '#e11d48', accentHover: '#be123c', accentDark: '#9f1239' },
105
- light: { accent: '#e11d48', accentHover: '#be123c', accentDark: '#9f1239' },
106
- }}
107
- ...
108
- />
109
- ```
110
-
111
- Both `darkTheme` and `lightTheme` are exported so you can spread them as a base:
112
-
113
- ```tsx
114
- import { darkTheme } from 'pdf-tsx'
115
-
116
- themeOverrides={{ dark: { ...darkTheme, bg: '#0f0f0f' } }}
117
- ```
118
-
119
- **Theme tokens** (`Theme` interface):
120
-
121
- | Token | CSS variable | Description |
122
- |--------------------|-----------------------------|--------------------------------------|
123
- | `bg` | `--pdf-bg` | Main background |
124
- | `surface` | `--pdf-surface` | Card / panel surface |
125
- | `surfaceHover` | `--pdf-surface-hover` | Surface hover state |
126
- | `border` | `--pdf-border` | Default border color |
127
- | `borderLight` | `--pdf-border-light` | Subtle border color |
128
- | `scrollArea` | `--pdf-scroll-area` | Scroll container background |
129
- | `textPrimary` | `--pdf-text-primary` | Primary text |
130
- | `textSecondary` | `--pdf-text-secondary` | Secondary text |
131
- | `textMuted` | `--pdf-text-muted` | Muted / disabled text |
132
- | `textPlaceholder` | `--pdf-text-placeholder` | Input placeholder text |
133
- | `accent` | `--pdf-accent` | Accent / interactive color |
134
- | `accentHover` | `--pdf-accent-hover` | Accent hover state |
135
- | `accentDark` | `--pdf-accent-dark` | Accent dark variant |
136
- | `accentLight` | `--pdf-accent-light` | Accent light variant |
137
- | `accentSubtle` | `--pdf-accent-subtle` | Accent with low opacity (backgrounds)|
138
- | `panelBg` | `--pdf-panel-bg` | Sidebar panel background |
139
- | `panelBorder` | `--pdf-panel-border` | Sidebar panel border |
140
- | `panelHeaderBg` | `--pdf-panel-header-bg` | Sidebar panel header background |
141
- | `shadowPage` | `--pdf-shadow-page` | Drop shadow color for PDF pages |
142
- | `badgeBg` | `--pdf-badge-bg` | Count badge background (signatures, annotations, attachments) |
143
- | `toolbarSeparator` | `--pdf-toolbar-separator` | `PDFToolbarSeparator` line color |
144
- | `fontFamily` | `--pdf-font-family` | Font family for all viewer UI text |
145
-
146
- ---
147
-
148
- ## PDFSidebar
149
-
150
- 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`).
151
-
152
- ```tsx
153
- import { PDFSidebar } from 'pdf-tsx'
154
- import { FiImage, FiList } from 'react-icons/fi'
155
-
156
- <PDFSidebar
157
- panels={[
158
- { icon: <FiImage size={18} />, label: 'Thumbnails', content: <PDFThumbnails /> },
159
- { icon: <FiList size={18} />, label: 'Outline', content: <PDFOutline /> },
160
- ]}
161
- />
162
- ```
163
-
164
- | Prop | Type | Description |
165
- |----------|------------------|------------------------------|
166
- | `panels` | `SidebarPanel[]` | Array of panels to display |
167
-
168
- **SidebarPanel**
169
-
170
- | Field | Type | Description |
171
- |-----------|-------------|----------------------------------------|
172
- | `icon` | `ReactNode` | Icon shown in the sidebar button bar |
173
- | `label` | `string` | Panel title shown in the header |
174
- | `content` | `ReactNode` | Content rendered inside the open panel |
175
-
176
- ### Sidebar hide/show pattern
177
-
178
- 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.
179
-
180
- ```tsx
181
- import { PDFSidebar, PDFSidebarToggle, PDFToolbar } from 'pdf-tsx'
182
- import { FiChevronsLeft, FiChevronsRight, FiMoreHorizontal } from 'react-icons/fi'
183
-
184
- <PDFViewer
185
- sidebar={
186
- <PDFSidebar panels={[...]} />
187
- }
188
- toolbar={
189
- <PDFToolbar overflowIcon={<FiMoreHorizontal size={16} />}>
190
- <PDFSidebarToggle
191
- collapseIcon={<FiChevronsLeft size={16} />}
192
- expandIcon={<FiChevronsRight size={16} />}
193
- />
194
- {/* other toolbar items */}
195
- </PDFToolbar>
196
- }
197
- />
198
- ```
199
-
200
- ---
201
-
202
- ## PDFToolbar (responsive toolbar)
203
-
204
- `PDFToolbar` wraps your toolbar items and automatically collapses those that don't fit the available width into an overflow `···` dropdown. It uses `ResizeObserver` to react to container size changes — no configuration required.
205
-
206
- ```tsx
207
- import { PDFToolbar } from 'pdf-tsx'
208
- import { FiMoreHorizontal } from 'react-icons/fi'
209
-
210
- <PDFViewer
211
- toolbar={
212
- <PDFToolbar overflowIcon={<FiMoreHorizontal size={16} />}>
213
- <PDFSidebarToggle collapseIcon={<FiChevronsLeft size={16} />} expandIcon={<FiChevronsRight size={16} />} />
214
- <PDFSearch searchIcon={<FiSearch size={14} />} prevIcon={<FiChevronUp size={14} />} nextIcon={<FiChevronDown size={14} />} clearIcon={<FiX size={14} />} />
215
- <PDFZoomControls zoomOutIcon={<FiZoomOut size={16} />} zoomInIcon={<FiZoomIn size={16} />} />
216
- <PDFNavigation prevIcon={<FiChevronLeft size={16} />} nextIcon={<FiChevronRight size={16} />} />
217
- {/* more items */}
218
- </PDFToolbar>
219
- }
220
- />
221
- ```
222
-
223
- | Prop | Type | Description |
224
- |----------------|-------------|-------------------------------------------------------|
225
- | `children` | `ReactNode` | Toolbar items in priority order (left = highest) |
226
- | `overflowIcon` | `ReactNode` | Icon for the `···` overflow button (default: `"···"`) |
227
-
228
- **Dropdown labels** — all built-in feature components carry a `toolbarLabelKey` static property. When an item is moved to the overflow dropdown, `PDFToolbar` resolves the label from the active language automatically. No extra configuration needed.
229
-
230
- **Custom items** — wrap any custom component in `PDFToolbarItem` to give it a dropdown label:
231
-
232
- ```tsx
233
- import { PDFToolbarItem } from 'pdf-tsx'
234
-
235
- <PDFToolbar overflowIcon={<FiMoreHorizontal size={16} />}>
236
- <PDFToolbarItem label="Custom">
237
- <MyCustomButton />
238
- </PDFToolbarItem>
239
- </PDFToolbar>
240
- ```
241
-
242
- ---
243
-
244
- ## Feature components
245
-
246
- All feature components must be rendered inside `<PDFViewer>`. They read and write state via `usePDFViewer()` internally.
247
-
248
- | Component | Icon props | Description |
249
- |------------------------|-----------------------------------------------------|------------------------------------------------------|
250
- | `PDFToolbar` | `overflowIcon` | Responsive toolbar — collapses items into `···` dropdown when space runs out |
251
- | `PDFToolbarItem` | — | Wrapper for custom toolbar items to add a dropdown label |
252
- | `PDFToolbarSeparator` | | Vertical divider between toolbar groups hidden automatically in the overflow dropdown |
253
- | `PDFThumbnails` | — | Page thumbnail strip for quick navigation |
254
- | `PDFOutline` | — | Document outline / bookmarks tree |
255
- | `PDFAnnotations` | — | Display PDF annotations |
256
- | `PDFAnnotationsIcon` | `icon` | Sidebar icon with annotation count badge |
257
- | `PDFSignatures` | — | Display PDF digital signatures |
258
- | `PDFSignaturesIcon` | `icon` | Sidebar icon with signature count badge |
259
- | `PDFAttachments` | — | List embedded file attachments with download buttons |
260
- | `PDFAttachmentsIcon` | `icon` | Sidebar icon with attachment count badge |
261
- | `PDFSearch` | `searchIcon` `prevIcon` `nextIcon` `clearIcon` | Full-text search with match highlighting. Accepts optional `defaultSearch` to pre-fill and auto-navigate |
262
- | `PDFZoomControls` | `zoomInIcon` `zoomOutIcon` | Zoom in / zoom out buttons with percentage display |
263
- | `PDFPageFit` | `fitWidthIcon` `fitPageIcon` | Fit-to-width and fit-to-page buttons |
264
- | `PDFRotationControl` | `icon` | Rotate page clockwise |
265
- | `PDFNavigation` | `prevIcon` `nextIcon` | Previous / next page buttons with current page input |
266
- | `PDFDownloadButton` | `icon` | Download the current PDF file |
267
- | `PDFChangeFile` | `icon` | Button to open a new PDF file |
268
- | `PDFThemeToggle` | `lightIcon` `darkIcon` | Toggle between dark and light mode |
269
- | `PDFPrintButton` | `icon` | Print the PDF using the browser's native print dialog|
270
- | `PDFSidebarToggle` | `collapseIcon` `expandIcon` | Toolbar button to expand the sidebar — visible only when sidebar is hidden (see Sidebar hide/show pattern) |
271
-
272
- ### PDFSearch
273
-
274
- | Prop | Type | Required | Description |
275
- |----------------|-------------|----------|-----------------------------------------------------------------------------|
276
- | `searchIcon` | `ReactNode` | yes | Icon shown inside the search input |
277
- | `prevIcon` | `ReactNode` | yes | Icon for the "previous match" button |
278
- | `nextIcon` | `ReactNode` | yes | Icon for the "next match" button |
279
- | `clearIcon` | `ReactNode` | yes | Icon for the "clear search" button |
280
- | `initialSearch`| `string` | no | Pre-fills the search input and automatically navigates to the first match once the PDF is loaded. Navigation fires once — subsequent manual edits are unaffected. |
281
-
282
- ```tsx
283
- <PDFSearch
284
- initialSearch="invoice"
285
- searchIcon={<FiSearch size={14} />}
286
- prevIcon={<FiChevronUp size={14} />}
287
- nextIcon={<FiChevronDown size={14} />}
288
- clearIcon={<FiX size={14} />}
289
- />
290
- ```
291
-
292
- ---
293
-
294
- ### Count badges
295
-
296
- `PDFSignaturesIcon`, `PDFAnnotationsIcon`, and `PDFAttachmentsIcon` 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.
297
-
298
- ```tsx
299
- import {
300
- PDFSidebar,
301
- PDFSignatures, PDFSignaturesIcon,
302
- PDFAnnotations, PDFAnnotationsIcon,
303
- PDFAttachments, PDFAttachmentsIcon,
304
- } from 'pdf-tsx'
305
- import { FiPenTool, FiMessageSquare, FiPaperclip } from 'react-icons/fi'
306
-
307
- <PDFSidebar panels={[
308
- {
309
- icon: <PDFSignaturesIcon icon={<FiPenTool size={17} />} />,
310
- label: 'Signatures',
311
- content: <PDFSignatures />,
312
- },
313
- {
314
- icon: <PDFAnnotationsIcon icon={<FiMessageSquare size={17} />} />,
315
- label: 'Annotations',
316
- content: <PDFAnnotations />,
317
- },
318
- {
319
- icon: <PDFAttachmentsIcon icon={<FiPaperclip size={17} />} />,
320
- label: 'Attachments',
321
- content: <PDFAttachments />,
322
- },
323
- ]} />
324
- ```
325
-
326
- The badge uses `var(--pdf-accent)` and respects `themeOverrides`.
327
-
328
- ---
329
-
330
- ## Localisation
331
-
332
- `PDFViewer` ships with English (default) and Italian label sets. Switch via the `language` prop:
333
-
334
- ```tsx
335
- <PDFViewer language="it" ... />
336
- ```
337
-
338
- Both label objects are exported if you need to inspect them:
339
-
340
- ```tsx
341
- import { italianLabels, englishLabels } from 'pdf-tsx'
342
- ```
343
-
344
- The `Labels` interface (also exported) describes every string key. The built-in toolbar overflow labels are resolved automatically from the active language — no extra configuration needed.
345
-
346
- ---
347
-
348
- ## Full example
349
-
350
- ```tsx
351
- import { useState, Suspense, lazy } from 'react'
352
- import {
353
- PDFSidebar, PDFSidebarToggle, PDFToolbar, PDFToolbarItem, PDFToolbarSeparator,
354
- PDFThumbnails, PDFOutline, PDFSearch,
355
- PDFZoomControls, PDFPageFit, PDFRotationControl,
356
- PDFNavigation, PDFDownloadButton, PDFChangeFile,
357
- PDFThemeToggle, PDFPrintButton,
358
- PDFSignatures, PDFSignaturesIcon,
359
- PDFAnnotations, PDFAnnotationsIcon,
360
- PDFAttachments, PDFAttachmentsIcon,
361
- } from 'pdf-tsx'
362
- import {
363
- FiImage, FiList, FiPenTool, FiMessageSquare, FiPaperclip,
364
- FiSearch, FiChevronUp, FiChevronDown, FiX,
365
- FiZoomIn, FiZoomOut, FiAlignJustify, FiMaximize2, FiRotateCw,
366
- FiChevronLeft, FiChevronRight, FiDownload, FiFolderPlus,
367
- FiPrinter, FiSun, FiMoon, FiChevronsLeft, FiChevronsRight, FiMoreHorizontal,
368
- } from 'react-icons/fi'
369
- import workerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?url'
370
-
371
- const PDFViewer = lazy(() => import('pdf-tsx').then(m => ({ default: m.PDFViewer })))
372
-
373
- function App() {
374
- const [file, setFile] = useState<File | null>(null)
375
-
376
- if (!file) {
377
- return (
378
- <input
379
- type="file"
380
- accept=".pdf"
381
- onChange={e => setFile(e.target.files![0])}
382
- />
383
- )
384
- }
385
-
386
- return (
387
- <Suspense fallback={<div>Loading...</div>}>
388
- <PDFViewer
389
- file={file}
390
- workerSrc={workerUrl}
391
- defaultFit="width"
392
- onChangeFile={setFile}
393
- sidebar={
394
- <PDFSidebar
395
- panels={[
396
- { icon: <FiImage size={18} />, label: 'Thumbnails', content: <PDFThumbnails /> },
397
- { icon: <FiList size={18} />, label: 'Outline', content: <PDFOutline /> },
398
- { icon: <PDFSignaturesIcon icon={<FiPenTool size={17} />} />, label: 'Signatures', content: <PDFSignatures /> },
399
- { icon: <PDFAnnotationsIcon icon={<FiMessageSquare size={17} />} />, label: 'Annotations', content: <PDFAnnotations /> },
400
- { icon: <PDFAttachmentsIcon icon={<FiPaperclip size={17} />} />, label: 'Attachments', content: <PDFAttachments /> },
401
- ]}
402
- />
403
- }
404
- toolbar={
405
- <PDFToolbar overflowIcon={<FiMoreHorizontal size={16} />}>
406
- <PDFSidebarToggle
407
- collapseIcon={<FiChevronsLeft size={16} />}
408
- expandIcon={<FiChevronsRight size={16} />}
409
- />
410
- <PDFToolbarSeparator />
411
- <PDFSearch
412
- searchIcon={<FiSearch size={14} />}
413
- prevIcon={<FiChevronUp size={14} />}
414
- nextIcon={<FiChevronDown size={14} />}
415
- clearIcon={<FiX size={14} />}
416
- />
417
- <PDFToolbarSeparator />
418
- <PDFZoomControls zoomOutIcon={<FiZoomOut size={16} />} zoomInIcon={<FiZoomIn size={16} />} />
419
- <PDFRotationControl icon={<FiRotateCw size={16} />} />
420
- <PDFPageFit fitWidthIcon={<FiAlignJustify size={16} />} fitPageIcon={<FiMaximize2 size={16} />} />
421
- <PDFToolbarSeparator />
422
- <PDFNavigation prevIcon={<FiChevronLeft size={16} />} nextIcon={<FiChevronRight size={16} />} />
423
- <PDFToolbarSeparator />
424
- <PDFThemeToggle lightIcon={<FiSun size={16} />} darkIcon={<FiMoon size={16} />} />
425
- <PDFPrintButton icon={<FiPrinter size={16} />} />
426
- <PDFChangeFile icon={<FiFolderPlus size={16} />} />
427
- <PDFDownloadButton icon={<FiDownload size={15} />} />
428
- {/* custom button with overflow label */}
429
- <PDFToolbarItem label="Share">
430
- <button onClick={() => {}}>Share</button>
431
- </PDFToolbarItem>
432
- </PDFToolbar>
433
- }
434
- />
435
- </Suspense>
436
- )
437
- }
438
- ```
439
-
440
- ---
441
-
442
- ## Performance
443
-
444
- `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.
445
-
446
- This means:
447
- - Initial load renders at most 5 pages, regardless of document length
448
- - Memory and CPU usage stay flat as the user scrolls through a large document
449
- - Pages that have already been rendered are kept in the DOM as the user scrolls, avoiding redundant re-renders
450
-
451
- Render cascades are minimised via manual memoization: the context value is wrapped in `useMemo` so a zoom or page change does not re-render unrelated consumers, and inner sub-components (`AnnotationCard`, `SignatureCard`, `OutlineItem`) are wrapped in `React.memo`.
452
-
453
- No configuration is needed — this is the default behaviour.
454
-
455
- ---
456
-
457
- ## License
458
-
459
- MIT © Riccardo Grossano — see [LICENSE](./LICENSE) for full terms.
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 { LuZoomIn, LuZoomOut } from 'react-icons/lu'
59
+
60
+ <PDFZoomControls zoomInIcon={<LuZoomIn size={16} />} zoomOutIcon={<LuZoomOut 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
+ | `language` | `"it" \| "en"` | no | UI language (default: `"en"`) |
90
+ | `className` | `string` | no | CSS class applied to the root layout element |
91
+ | `style` | `React.CSSProperties` | no | Inline style applied to the root layout element |
92
+ | `requireRead` | `boolean` | no | Show a mandatory-reading banner (see Mandatory reading) |
93
+ | `onDocumentRead` | `() => void` | no | Called once when the user reaches the last page |
94
+
95
+ ---
96
+
97
+ ## Theming
98
+
99
+ `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.
100
+
101
+ ```tsx
102
+ import { PDFViewer, darkTheme } from 'pdf-tsx'
103
+
104
+ <PDFViewer
105
+ themeOverrides={{
106
+ dark: { accent: '#e11d48', accentHover: '#be123c', accentDark: '#9f1239' },
107
+ light: { accent: '#e11d48', accentHover: '#be123c', accentDark: '#9f1239' },
108
+ }}
109
+ ...
110
+ />
111
+ ```
112
+
113
+ Both `darkTheme` and `lightTheme` are exported so you can spread them as a base:
114
+
115
+ ```tsx
116
+ import { darkTheme } from 'pdf-tsx'
117
+
118
+ themeOverrides={{ dark: { ...darkTheme, bg: '#0f0f0f' } }}
119
+ ```
120
+
121
+ **Theme tokens** (`Theme` interface):
122
+
123
+ | Token | CSS variable | Description |
124
+ |--------------------|-----------------------------|--------------------------------------|
125
+ | `bg` | `--pdf-bg` | Main background |
126
+ | `surface` | `--pdf-surface` | Card / panel surface |
127
+ | `surfaceHover` | `--pdf-surface-hover` | Surface hover state |
128
+ | `border` | `--pdf-border` | Default border color |
129
+ | `borderLight` | `--pdf-border-light` | Subtle border color |
130
+ | `scrollArea` | `--pdf-scroll-area` | Scroll container background |
131
+ | `textPrimary` | `--pdf-text-primary` | Primary text |
132
+ | `textSecondary` | `--pdf-text-secondary` | Secondary text |
133
+ | `textMuted` | `--pdf-text-muted` | Muted / disabled text |
134
+ | `textPlaceholder` | `--pdf-text-placeholder` | Input placeholder text |
135
+ | `accent` | `--pdf-accent` | Accent / interactive color |
136
+ | `accentHover` | `--pdf-accent-hover` | Accent hover state |
137
+ | `accentDark` | `--pdf-accent-dark` | Accent dark variant |
138
+ | `accentLight` | `--pdf-accent-light` | Accent light variant |
139
+ | `accentSubtle` | `--pdf-accent-subtle` | Accent with low opacity (backgrounds)|
140
+ | `panelBg` | `--pdf-panel-bg` | Sidebar panel background |
141
+ | `panelBorder` | `--pdf-panel-border` | Sidebar panel border |
142
+ | `panelHeaderBg` | `--pdf-panel-header-bg` | Sidebar panel header background |
143
+ | `shadowPage` | `--pdf-shadow-page` | Drop shadow color for PDF pages |
144
+ | `badgeBg` | `--pdf-badge-bg` | Count badge background (signatures, annotations, attachments) |
145
+ | `toolbarSeparator` | `--pdf-toolbar-separator` | `PDFToolbarSeparator` line color |
146
+ | `fontFamily` | `--pdf-font-family` | Font family for all viewer UI text |
147
+
148
+ ---
149
+
150
+ ## PDFSidebar
151
+
152
+ 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`).
153
+
154
+ ```tsx
155
+ import { PDFSidebar } from 'pdf-tsx'
156
+ import { LuLayoutGrid, LuBookOpen } from 'react-icons/lu'
157
+
158
+ <PDFSidebar
159
+ panels={[
160
+ { icon: <LuLayoutGrid size={18} />, label: 'Thumbnails', content: <PDFThumbnails /> },
161
+ { icon: <LuBookOpen size={18} />, label: 'Outline', content: <PDFOutline /> },
162
+ ]}
163
+ />
164
+ ```
165
+
166
+ | Prop | Type | Description |
167
+ |----------|------------------|------------------------------|
168
+ | `panels` | `SidebarPanel[]` | Array of panels to display |
169
+
170
+ **SidebarPanel**
171
+
172
+ | Field | Type | Description |
173
+ |-----------|-------------|----------------------------------------|
174
+ | `icon` | `ReactNode` | Icon shown in the sidebar button bar |
175
+ | `label` | `string` | Panel title shown in the header |
176
+ | `content` | `ReactNode` | Content rendered inside the open panel |
177
+
178
+ ### Sidebar hide/show pattern
179
+
180
+ 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.
181
+
182
+ ```tsx
183
+ import { PDFSidebar, PDFSidebarToggle, PDFToolbar } from 'pdf-tsx'
184
+ import { LuPanelLeftClose, LuPanelLeftOpen, LuEllipsis } from 'react-icons/lu'
185
+
186
+ <PDFViewer
187
+ sidebar={
188
+ <PDFSidebar panels={[...]} />
189
+ }
190
+ toolbar={
191
+ <PDFToolbar overflowIcon={<LuEllipsis size={16} />}>
192
+ <PDFSidebarToggle
193
+ collapseIcon={<LuPanelLeftClose size={16} />}
194
+ expandIcon={<LuPanelLeftOpen size={16} />}
195
+ />
196
+ {/* other toolbar items */}
197
+ </PDFToolbar>
198
+ }
199
+ />
200
+ ```
201
+
202
+ ---
203
+
204
+ ## PDFToolbar (responsive toolbar)
205
+
206
+ `PDFToolbar` wraps your toolbar items and automatically collapses those that don't fit the available width into an overflow `···` dropdown. It uses `ResizeObserver` to react to container size changes — no configuration required.
207
+
208
+ ```tsx
209
+ import { PDFToolbar } from 'pdf-tsx'
210
+ import { LuEllipsis, LuPanelLeftClose, LuPanelLeftOpen, LuSearch, LuChevronUp, LuChevronDown, LuX, LuZoomOut, LuZoomIn, LuChevronLeft, LuChevronRight } from 'react-icons/lu'
211
+
212
+ <PDFViewer
213
+ toolbar={
214
+ <PDFToolbar overflowIcon={<LuEllipsis size={16} />}>
215
+ <PDFSidebarToggle collapseIcon={<LuPanelLeftClose size={16} />} expandIcon={<LuPanelLeftOpen size={16} />} />
216
+ <PDFSearch searchIcon={<LuSearch size={14} />} prevIcon={<LuChevronUp size={14} />} nextIcon={<LuChevronDown size={14} />} clearIcon={<LuX size={14} />} />
217
+ <PDFZoomControls zoomOutIcon={<LuZoomOut size={16} />} zoomInIcon={<LuZoomIn size={16} />} />
218
+ <PDFNavigation prevIcon={<LuChevronLeft size={16} />} nextIcon={<LuChevronRight size={16} />} />
219
+ {/* … more items */}
220
+ </PDFToolbar>
221
+ }
222
+ />
223
+ ```
224
+
225
+ | Prop | Type | Description |
226
+ |----------------|-------------|-------------------------------------------------------|
227
+ | `children` | `ReactNode` | Toolbar items in priority order (left = highest) |
228
+ | `overflowIcon` | `ReactNode` | Icon for the `···` overflow button (default: `"···"`) |
229
+
230
+ **Dropdown labels** — all built-in feature components carry a `toolbarLabelKey` static property. When an item is moved to the overflow dropdown, `PDFToolbar` resolves the label from the active language automatically. No extra configuration needed.
231
+
232
+ **Custom items** — wrap any custom component in `PDFToolbarItem` to give it a dropdown label:
233
+
234
+ ```tsx
235
+ import { PDFToolbarItem } from 'pdf-tsx'
236
+
237
+ <PDFToolbar overflowIcon={<LuEllipsis size={16} />}>
238
+ <PDFToolbarItem label="Custom">
239
+ <MyCustomButton />
240
+ </PDFToolbarItem>
241
+ </PDFToolbar>
242
+ ```
243
+
244
+ ---
245
+
246
+ ## Feature components
247
+
248
+ All feature components must be rendered inside `<PDFViewer>`. They read and write state via `usePDFViewer()` internally.
249
+
250
+ | Component | Icon props | Description |
251
+ |------------------------|-----------------------------------------------------|------------------------------------------------------|
252
+ | `PDFToolbar` | `overflowIcon` | Responsive toolbar collapses items into `···` dropdown when space runs out |
253
+ | `PDFToolbarItem` | — | Wrapper for custom toolbar items to add a dropdown label |
254
+ | `PDFToolbarSeparator` | — | Vertical divider between toolbar groups — hidden automatically in the overflow dropdown |
255
+ | `PDFThumbnails` | — | Page thumbnail strip for quick navigation |
256
+ | `PDFOutline` | | Document outline / bookmarks tree |
257
+ | `PDFAnnotations` | — | Display PDF annotations |
258
+ | `PDFAnnotationsIcon` | `icon` | Sidebar icon with annotation count badge |
259
+ | `PDFSignatures` | — | Display PDF digital signatures |
260
+ | `PDFSignaturesIcon` | `icon` | Sidebar icon with signature count badge |
261
+ | `PDFAttachments` | — | List embedded file attachments with download buttons |
262
+ | `PDFAttachmentsIcon` | `icon` | Sidebar icon with attachment count badge |
263
+ | `PDFSearch` | `searchIcon` `prevIcon` `nextIcon` `clearIcon` | Full-text search with match highlighting. Accepts optional `defaultSearch` to pre-fill and auto-navigate |
264
+ | `PDFZoomControls` | `zoomInIcon` `zoomOutIcon` | Zoom in / zoom out buttons with percentage display |
265
+ | `PDFPageFit` | `fitWidthIcon` `fitPageIcon` | Fit-to-width and fit-to-page buttons |
266
+ | `PDFRotationControl` | `icon` | Rotate page clockwise |
267
+ | `PDFNavigation` | `prevIcon` `nextIcon` | Previous / next page buttons with current page input |
268
+ | `PDFDownloadButton` | `icon` | Download the current PDF file |
269
+ | `PDFChangeFile` | `icon` | Button to open a new PDF file |
270
+ | `PDFThemeToggle` | `lightIcon` `darkIcon` | Toggle between dark and light mode |
271
+ | `PDFPrintButton` | `icon` | Print the PDF using the browser's native print dialog|
272
+ | `PDFSidebarToggle` | `collapseIcon` `expandIcon` | Toolbar button to expand the sidebar — visible only when sidebar is hidden (see Sidebar hide/show pattern) |
273
+
274
+ ### PDFSearch
275
+
276
+ | Prop | Type | Required | Description |
277
+ |----------------|-------------|----------|-----------------------------------------------------------------------------|
278
+ | `searchIcon` | `ReactNode` | yes | Icon shown inside the search input |
279
+ | `prevIcon` | `ReactNode` | yes | Icon for the "previous match" button |
280
+ | `nextIcon` | `ReactNode` | yes | Icon for the "next match" button |
281
+ | `clearIcon` | `ReactNode` | yes | Icon for the "clear search" button |
282
+ | `initialSearch`| `string` | no | Pre-fills the search input and automatically navigates to the first match once the PDF is loaded. Navigation fires once — subsequent manual edits are unaffected. |
283
+
284
+ ```tsx
285
+ <PDFSearch
286
+ initialSearch="invoice"
287
+ searchIcon={<LuSearch size={14} />}
288
+ prevIcon={<LuChevronUp size={14} />}
289
+ nextIcon={<LuChevronDown size={14} />}
290
+ clearIcon={<LuX size={14} />}
291
+ />
292
+ ```
293
+
294
+ ---
295
+
296
+ ### Count badges
297
+
298
+ `PDFSignaturesIcon`, `PDFAnnotationsIcon`, and `PDFAttachmentsIcon` 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.
299
+
300
+ ```tsx
301
+ import {
302
+ PDFSidebar,
303
+ PDFSignatures, PDFSignaturesIcon,
304
+ PDFAnnotations, PDFAnnotationsIcon,
305
+ PDFAttachments, PDFAttachmentsIcon,
306
+ } from 'pdf-tsx'
307
+ import { LuSignature, LuStickyNote, LuPaperclip } from 'react-icons/lu'
308
+
309
+ <PDFSidebar panels={[
310
+ {
311
+ icon: <PDFSignaturesIcon icon={<LuSignature size={17} />} />,
312
+ label: 'Signatures',
313
+ content: <PDFSignatures />,
314
+ },
315
+ {
316
+ icon: <PDFAnnotationsIcon icon={<LuStickyNote size={17} />} />,
317
+ label: 'Annotations',
318
+ content: <PDFAnnotations />,
319
+ },
320
+ {
321
+ icon: <PDFAttachmentsIcon icon={<LuPaperclip size={17} />} />,
322
+ label: 'Attachments',
323
+ content: <PDFAttachments />,
324
+ },
325
+ ]} />
326
+ ```
327
+
328
+ The badge uses `var(--pdf-accent)` and respects `themeOverrides`.
329
+
330
+ ---
331
+
332
+ ## Mandatory reading
333
+
334
+ Use `requireRead` when the user must scroll through the entire document before proceeding (e.g. terms and conditions, consent forms).
335
+
336
+ ```tsx
337
+ const [canProceed, setCanProceed] = useState(false)
338
+
339
+ <PDFViewer
340
+ requireRead
341
+ onDocumentRead={() => setCanProceed(true)}
342
+ ...
343
+ />
344
+
345
+ <button disabled={!canProceed}>Accetto</button>
346
+ ```
347
+
348
+ `PDFViewer` renders a sticky banner at the bottom of the scroll area:
349
+
350
+ - **Pending** — accent color, arrow-down icon, label `requireReadPending` ("Scroll to the end to continue")
351
+ - **Confirmed** — green (`#16a34a`), checkmark icon, label `requireReadDone` ("Document read") — the banner stays visible after confirmation
352
+
353
+ `onDocumentRead` fires at most once per loaded file. Both banner labels are part of the `Labels` interface and are translated automatically when `language="it"` is set.
354
+
355
+ ---
356
+
357
+ ## Localisation
358
+
359
+ `PDFViewer` ships with English (default) and Italian label sets. Switch via the `language` prop:
360
+
361
+ ```tsx
362
+ <PDFViewer language="it" ... />
363
+ ```
364
+
365
+ Both label objects are exported if you need to inspect them:
366
+
367
+ ```tsx
368
+ import { italianLabels, englishLabels } from 'pdf-tsx'
369
+ ```
370
+
371
+ The `Labels` interface (also exported) describes every string key. The built-in toolbar overflow labels are resolved automatically from the active language — no extra configuration needed.
372
+
373
+ ---
374
+
375
+ ## Full example
376
+
377
+ ```tsx
378
+ import { useState, Suspense, lazy } from 'react'
379
+ import {
380
+ PDFSidebar, PDFSidebarToggle, PDFToolbar, PDFToolbarItem, PDFToolbarSeparator,
381
+ PDFThumbnails, PDFOutline, PDFSearch,
382
+ PDFZoomControls, PDFPageFit, PDFRotationControl,
383
+ PDFNavigation, PDFDownloadButton, PDFChangeFile,
384
+ PDFThemeToggle, PDFPrintButton,
385
+ PDFSignatures, PDFSignaturesIcon,
386
+ PDFAnnotations, PDFAnnotationsIcon,
387
+ PDFAttachments, PDFAttachmentsIcon,
388
+ } from 'pdf-tsx'
389
+ import {
390
+ LuLayoutGrid, LuBookOpen, LuSignature, LuStickyNote, LuPaperclip,
391
+ LuSearch, LuChevronUp, LuChevronDown, LuX,
392
+ LuZoomIn, LuZoomOut, LuArrowLeftRight, LuScan, LuRotateCw,
393
+ LuChevronLeft, LuChevronRight, LuDownload, LuFolderPlus,
394
+ LuPrinter, LuSun, LuMoon, LuPanelLeftClose, LuPanelLeftOpen, LuEllipsis,
395
+ } from 'react-icons/lu'
396
+ import workerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?url'
397
+
398
+ const PDFViewer = lazy(() => import('pdf-tsx').then(m => ({ default: m.PDFViewer })))
399
+
400
+ function App() {
401
+ const [file, setFile] = useState<File | null>(null)
402
+
403
+ if (!file) {
404
+ return (
405
+ <input
406
+ type="file"
407
+ accept=".pdf"
408
+ onChange={e => setFile(e.target.files![0])}
409
+ />
410
+ )
411
+ }
412
+
413
+ return (
414
+ <Suspense fallback={<div>Loading...</div>}>
415
+ <PDFViewer
416
+ file={file}
417
+ workerSrc={workerUrl}
418
+ defaultFit="width"
419
+ onChangeFile={setFile}
420
+ sidebar={
421
+ <PDFSidebar
422
+ panels={[
423
+ { icon: <LuLayoutGrid size={18} />, label: 'Thumbnails', content: <PDFThumbnails /> },
424
+ { icon: <LuBookOpen size={18} />, label: 'Outline', content: <PDFOutline /> },
425
+ { icon: <PDFSignaturesIcon icon={<LuSignature size={17} />} />, label: 'Signatures', content: <PDFSignatures /> },
426
+ { icon: <PDFAnnotationsIcon icon={<LuStickyNote size={17} />} />, label: 'Annotations', content: <PDFAnnotations /> },
427
+ { icon: <PDFAttachmentsIcon icon={<LuPaperclip size={17} />} />, label: 'Attachments', content: <PDFAttachments /> },
428
+ ]}
429
+ />
430
+ }
431
+ toolbar={
432
+ <PDFToolbar overflowIcon={<LuEllipsis size={16} />}>
433
+ <PDFSidebarToggle
434
+ collapseIcon={<LuPanelLeftClose size={16} />}
435
+ expandIcon={<LuPanelLeftOpen size={16} />}
436
+ />
437
+ <PDFToolbarSeparator />
438
+ <PDFSearch
439
+ searchIcon={<LuSearch size={14} />}
440
+ prevIcon={<LuChevronUp size={14} />}
441
+ nextIcon={<LuChevronDown size={14} />}
442
+ clearIcon={<LuX size={14} />}
443
+ />
444
+ <PDFToolbarSeparator />
445
+ <PDFZoomControls zoomOutIcon={<LuZoomOut size={16} />} zoomInIcon={<LuZoomIn size={16} />} />
446
+ <PDFRotationControl icon={<LuRotateCw size={16} />} />
447
+ <PDFPageFit fitWidthIcon={<LuArrowLeftRight size={16} />} fitPageIcon={<LuScan size={16} />} />
448
+ <PDFToolbarSeparator />
449
+ <PDFNavigation prevIcon={<LuChevronLeft size={16} />} nextIcon={<LuChevronRight size={16} />} />
450
+ <PDFToolbarSeparator />
451
+ <PDFThemeToggle lightIcon={<LuSun size={16} />} darkIcon={<LuMoon size={16} />} />
452
+ <PDFPrintButton icon={<LuPrinter size={16} />} />
453
+ <PDFChangeFile icon={<LuFolderPlus size={16} />} />
454
+ <PDFDownloadButton icon={<LuDownload size={15} />} />
455
+ {/* custom button with overflow label */}
456
+ <PDFToolbarItem label="Share">
457
+ <button onClick={() => {}}>Share</button>
458
+ </PDFToolbarItem>
459
+ </PDFToolbar>
460
+ }
461
+ />
462
+ </Suspense>
463
+ )
464
+ }
465
+ ```
466
+
467
+ ---
468
+
469
+ ## Performance
470
+
471
+ `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.
472
+
473
+ This means:
474
+ - Initial load renders at most 5 pages, regardless of document length
475
+ - Memory and CPU usage stay flat as the user scrolls through a large document
476
+ - Pages that have already been rendered are kept in the DOM as the user scrolls, avoiding redundant re-renders
477
+
478
+ Render cascades are minimised via manual memoization: the context value is wrapped in `useMemo` so a zoom or page change does not re-render unrelated consumers, and inner sub-components (`AnnotationCard`, `SignatureCard`, `OutlineItem`) are wrapped in `React.memo`.
479
+
480
+ No configuration is needed — this is the default behaviour.
481
+
482
+ ---
483
+
484
+ ## License
485
+
486
+ MIT © Riccardo Grossano — see [LICENSE](./LICENSE) for full terms.