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 +486 -459
- package/dist/cjs/components/PDFViewer.cjs +1 -1
- package/dist/cjs/components/PDFViewer.module.cjs +1 -1
- package/dist/cjs/components/features/PDFNavigation.cjs +1 -1
- package/dist/cjs/labels.cjs +1 -1
- package/dist/cjs/pdf-tsx.css +1 -1
- package/dist/components/PDFViewer.d.ts +2 -0
- package/dist/context/PDFViewerContext.d.ts +1 -0
- package/dist/es/components/PDFViewer.js +303 -251
- package/dist/es/components/PDFViewer.module.js +23 -21
- package/dist/es/components/features/PDFNavigation.js +19 -18
- package/dist/es/labels.js +6 -2
- package/dist/es/pdf-tsx.css +1 -1
- package/dist/labels.d.ts +2 -0
- package/package.json +1 -1
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 {
|
|
59
|
-
|
|
60
|
-
<PDFZoomControls zoomInIcon={<
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
|
124
|
-
|
|
125
|
-
| `
|
|
126
|
-
| `
|
|
127
|
-
| `
|
|
128
|
-
| `
|
|
129
|
-
| `
|
|
130
|
-
| `
|
|
131
|
-
| `
|
|
132
|
-
| `
|
|
133
|
-
| `
|
|
134
|
-
| `
|
|
135
|
-
| `
|
|
136
|
-
| `
|
|
137
|
-
| `
|
|
138
|
-
| `
|
|
139
|
-
| `
|
|
140
|
-
| `
|
|
141
|
-
| `
|
|
142
|
-
| `
|
|
143
|
-
| `
|
|
144
|
-
| `
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
/>
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
|
173
|
-
|
|
174
|
-
| `
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
<
|
|
216
|
-
<
|
|
217
|
-
{
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
**
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
|
251
|
-
|
|
252
|
-
| `
|
|
253
|
-
| `
|
|
254
|
-
| `
|
|
255
|
-
| `
|
|
256
|
-
| `
|
|
257
|
-
| `
|
|
258
|
-
| `
|
|
259
|
-
| `
|
|
260
|
-
| `
|
|
261
|
-
| `
|
|
262
|
-
| `
|
|
263
|
-
| `
|
|
264
|
-
| `
|
|
265
|
-
| `
|
|
266
|
-
| `
|
|
267
|
-
| `
|
|
268
|
-
| `
|
|
269
|
-
| `
|
|
270
|
-
| `
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
|
277
|
-
|
|
278
|
-
| `
|
|
279
|
-
| `
|
|
280
|
-
| `
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
/>
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
```
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
} from '
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
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.
|