pdf-tsx 0.17.9 → 1.0.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.
Files changed (127) hide show
  1. package/LICENSE +78 -21
  2. package/README.md +632 -512
  3. package/dist/cjs/components/PDFSidebar.cjs +1 -1
  4. package/dist/cjs/components/PDFViewer.cjs +1 -1
  5. package/dist/cjs/components/PDFViewer.module.cjs +1 -1
  6. package/dist/cjs/components/features/PDFAnnotations.cjs +1 -1
  7. package/dist/cjs/components/features/PDFAttachments.cjs +1 -1
  8. package/dist/cjs/components/features/PDFChangeFile.cjs +1 -1
  9. package/dist/cjs/components/features/PDFDownloadButton.cjs +1 -1
  10. package/dist/cjs/components/features/PDFNavigation.cjs +1 -1
  11. package/dist/cjs/components/features/PDFOutline.cjs +1 -1
  12. package/dist/cjs/components/features/PDFPageFit.cjs +1 -1
  13. package/dist/cjs/components/features/PDFPrintButton.cjs +1 -1
  14. package/dist/cjs/components/features/PDFRotationControl.cjs +1 -1
  15. package/dist/cjs/components/features/PDFSearch.cjs +1 -1
  16. package/dist/cjs/components/features/PDFSidebarToggle.cjs +1 -1
  17. package/dist/cjs/components/features/PDFSignatures.cjs +1 -1
  18. package/dist/cjs/components/features/PDFThemeToggle.cjs +1 -1
  19. package/dist/cjs/components/features/PDFThumbnails.cjs +1 -1
  20. package/dist/cjs/components/features/PDFZoomControls.cjs +1 -1
  21. package/dist/cjs/components/ui/PDFToolbar.cjs +1 -1
  22. package/dist/cjs/components/ui/PDFToolbarDropdownContext.cjs +1 -1
  23. package/dist/cjs/components/ui/PDFToolbarItem.cjs +1 -1
  24. package/dist/cjs/components/ui/PDFToolbarSeparator.cjs +1 -1
  25. package/dist/cjs/components/ui/ToolbarButton.cjs +1 -1
  26. package/dist/cjs/components/ui/icons.cjs +1 -1
  27. package/dist/cjs/context/LabelsContext.cjs +1 -1
  28. package/dist/cjs/context/PDFViewerContext.cjs +1 -1
  29. package/dist/cjs/context/ThemeContext.cjs +1 -1
  30. package/dist/cjs/context/labelsContextValue.cjs +1 -1
  31. package/dist/cjs/context/themeContextValue.cjs +1 -1
  32. package/dist/cjs/hooks/useAnnotationLayer.cjs +1 -1
  33. package/dist/cjs/hooks/useAnnotations.cjs +1 -1
  34. package/dist/cjs/hooks/useAttachments.cjs +1 -1
  35. package/dist/cjs/hooks/useKeyboardShortcuts.cjs +1 -1
  36. package/dist/cjs/hooks/useLabels.cjs +1 -1
  37. package/dist/cjs/hooks/useOutline.cjs +1 -1
  38. package/dist/cjs/hooks/useOverlayRects.cjs +1 -1
  39. package/dist/cjs/hooks/usePDFLoader.cjs +1 -1
  40. package/dist/cjs/hooks/usePDFRenderer.cjs +1 -1
  41. package/dist/cjs/hooks/usePinchZoom.cjs +1 -1
  42. package/dist/cjs/hooks/useScrollTracking.cjs +1 -1
  43. package/dist/cjs/hooks/useSignatures.cjs +1 -1
  44. package/dist/cjs/hooks/useTextLayer.cjs +1 -1
  45. package/dist/cjs/hooks/useTextSearch.cjs +1 -1
  46. package/dist/cjs/hooks/useTheme.cjs +1 -1
  47. package/dist/cjs/hooks/useThumbnails.cjs +1 -1
  48. package/dist/cjs/index.cjs +1 -1
  49. package/dist/cjs/pdf-tsx.css +1 -1
  50. package/dist/cjs/utils/pdfFit.cjs +1 -1
  51. package/dist/components/PDFViewer.d.ts +2 -1
  52. package/dist/components/features/PDFSignButton.d.ts +10 -0
  53. package/dist/components/features/SignaturePad.d.ts +10 -0
  54. package/dist/es/components/PDFViewer.module.js +23 -23
  55. package/dist/es/pdf-tsx.css +1 -1
  56. package/dist/hooks/usePDFLoader.d.ts +1 -1
  57. package/dist/pdf-tsx.cjs.js +5 -0
  58. package/dist/pdf-tsx.css +2 -0
  59. package/dist/pdf-tsx.es.js +2637 -0
  60. package/dist/src/components/PDFSidebar.d.ts +11 -0
  61. package/dist/src/components/PDFViewer.d.ts +22 -0
  62. package/dist/src/components/features/PDFAnnotations.d.ts +5 -0
  63. package/dist/src/components/features/PDFAttachments.d.ts +5 -0
  64. package/dist/src/components/features/PDFChangeFile.d.ts +9 -0
  65. package/dist/src/components/features/PDFDownloadButton.d.ts +9 -0
  66. package/dist/src/components/features/PDFNavigation.d.ts +10 -0
  67. package/dist/src/components/features/PDFOutline.d.ts +1 -0
  68. package/dist/src/components/features/PDFPageFit.d.ts +10 -0
  69. package/dist/src/components/features/PDFPrintButton.d.ts +9 -0
  70. package/dist/src/components/features/PDFRotationControl.d.ts +9 -0
  71. package/dist/src/components/features/PDFSearch.d.ts +13 -0
  72. package/dist/src/components/features/PDFSidebarToggle.d.ts +10 -0
  73. package/dist/src/components/features/PDFSignatures.d.ts +5 -0
  74. package/dist/src/components/features/PDFThemeToggle.d.ts +10 -0
  75. package/dist/src/components/features/PDFThumbnails.d.ts +1 -0
  76. package/dist/src/components/features/PDFZoomControls.d.ts +10 -0
  77. package/dist/src/components/ui/PDFToolbar.d.ts +8 -0
  78. package/dist/src/components/ui/PDFToolbarDropdownContext.d.ts +1 -0
  79. package/dist/src/components/ui/PDFToolbarItem.d.ts +7 -0
  80. package/dist/src/components/ui/PDFToolbarSeparator.d.ts +4 -0
  81. package/dist/src/components/ui/ToolbarButton.d.ts +6 -0
  82. package/dist/src/components/ui/icons.d.ts +7 -0
  83. package/dist/src/context/LabelsContext.d.ts +7 -0
  84. package/dist/src/context/PDFViewerContext.d.ts +30 -0
  85. package/dist/src/context/ThemeContext.d.ts +12 -0
  86. package/dist/src/context/labelsContextValue.d.ts +2 -0
  87. package/dist/src/context/themeContextValue.d.ts +7 -0
  88. package/dist/src/hooks/useAnnotationLayer.d.ts +34 -0
  89. package/dist/src/hooks/useAnnotations.d.ts +15 -0
  90. package/dist/src/hooks/useAttachments.d.ts +11 -0
  91. package/dist/src/hooks/useKeyboardShortcuts.d.ts +9 -0
  92. package/dist/src/hooks/useLabels.d.ts +2 -0
  93. package/dist/src/hooks/useOutline.d.ts +14 -0
  94. package/dist/src/hooks/useOverlayRects.d.ts +21 -0
  95. package/dist/src/hooks/usePDFLoader.d.ts +5 -0
  96. package/dist/src/hooks/usePDFRenderer.d.ts +16 -0
  97. package/dist/src/hooks/useScrollTracking.d.ts +10 -0
  98. package/dist/src/hooks/useSignatures.d.ts +17 -0
  99. package/dist/src/hooks/useTextLayer.d.ts +10 -0
  100. package/dist/src/hooks/useTextSearch.d.ts +12 -0
  101. package/dist/src/hooks/useTheme.d.ts +2 -0
  102. package/dist/src/hooks/useThumbnails.d.ts +10 -0
  103. package/dist/src/index.d.ts +24 -0
  104. package/dist/src/labels.d.ts +53 -0
  105. package/dist/src/theme.d.ts +27 -0
  106. package/dist/src/utils/pdfFit.d.ts +2 -0
  107. package/dist/src/utils/url.d.ts +1 -0
  108. package/dist/utils/addSignatureToPdf.d.ts +9 -0
  109. package/dist/vite.svg +1 -0
  110. package/dist/wasm/LICENSE_JBIG2 +196 -0
  111. package/dist/wasm/LICENSE_OPENJPEG +39 -0
  112. package/dist/wasm/LICENSE_PDFJS_JBIG2 +13 -0
  113. package/dist/wasm/LICENSE_PDFJS_OPENJPEG +22 -0
  114. package/dist/wasm/LICENSE_PDFJS_QCMS +22 -0
  115. package/dist/wasm/LICENSE_QCMS +21 -0
  116. package/dist/wasm/jbig2.wasm +0 -0
  117. package/dist/wasm/jbig2_nowasm_fallback.js +15 -0
  118. package/dist/wasm/openjpeg.wasm +0 -0
  119. package/dist/wasm/openjpeg_nowasm_fallback.js +17 -0
  120. package/dist/wasm/qcms_bg.wasm +0 -0
  121. package/dist/wasm/quickjs-eval.js +16 -0
  122. package/dist/wasm/quickjs-eval.wasm +0 -0
  123. package/package.json +64 -64
  124. package/dist/utils/polyfills.d.ts +0 -1
  125. /package/dist/{hooks → src/hooks}/usePinchZoom.d.ts +0 -0
  126. /package/dist/{utils → src/utils}/pdfjsPolyfill.d.ts +0 -0
  127. /package/dist/{utils → src/utils}/zoom.d.ts +0 -0
package/README.md CHANGED
@@ -1,512 +1,632 @@
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
- ## Keyboard shortcuts
470
-
471
- When focus is not on an input or textarea, the following keys are active:
472
-
473
- | Key | Action |
474
- |-----------------------------|------------------|
475
- | `ArrowLeft` / `ArrowUp` | Previous page |
476
- | `ArrowRight` / `ArrowDown` | Next page |
477
- | `r` / `R` | Rotate +90° |
478
-
479
- ## Gesture & zoom controls
480
-
481
- Zoom can be controlled via toolbar buttons (`PDFZoomControls`), keyboard/trackpad gestures, and touch gestures — no configuration required.
482
-
483
- | Input | Action |
484
- |------------------------------|---------------------------------------------|
485
- | `Ctrl + scroll` (mouse) | Zoom in / out |
486
- | `Cmd + scroll` (macOS) | Zoom in / out |
487
- | Trackpad pinch (desktop) | Zoom in / out |
488
- | Two-finger pinch (touch) | Zoom in / out |
489
- | Double tap (touch) | Toggle between 2× zoom and 1× (reset) |
490
-
491
- Zoom is clamped to the range `[0.2, 3.0]`. Any manual gesture clears the active `fitMode` (`"width"` / `"page"`).
492
-
493
- ---
494
-
495
- ## Performance
496
-
497
- `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.
498
-
499
- This means:
500
- - Initial load renders at most 5 pages, regardless of document length
501
- - Memory and CPU usage stay flat as the user scrolls through a large document
502
- - Pages that have already been rendered are kept in the DOM as the user scrolls, avoiding redundant re-renders
503
-
504
- 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`.
505
-
506
- No configuration is needed — this is the default behaviour.
507
-
508
- ---
509
-
510
- ## License
511
-
512
- 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)
41
+ return (
42
+ <input
43
+ type="file"
44
+ accept=".pdf"
45
+ onChange={(e) => setFile(e.target.files![0])}
46
+ />
47
+ );
48
+
49
+ return <PDFViewer file={file} workerSrc={workerUrl} />;
50
+ }
51
+ ```
52
+
53
+ > 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:
54
+ >
55
+ > ```ts
56
+ > workerSrc = "https://unpkg.com/pdfjs-dist@5/build/pdf.worker.min.mjs";
57
+ > ```
58
+
59
+ ---
60
+
61
+ ## Icons
62
+
63
+ 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.
64
+
65
+ ```tsx
66
+ import { LuZoomIn, LuZoomOut } from "react-icons/lu";
67
+
68
+ <PDFZoomControls
69
+ zoomInIcon={<LuZoomIn size={16} />}
70
+ zoomOutIcon={<LuZoomOut size={16} />}
71
+ />;
72
+ ```
73
+
74
+ ---
75
+
76
+ ## PDFViewer
77
+
78
+ The main component. Must wrap all feature components.
79
+
80
+ ```tsx
81
+ <PDFViewer
82
+ file={file}
83
+ workerSrc={workerUrl}
84
+ sidebar={<PDFSidebar panels={[...]} />}
85
+ toolbar={<>...</>}
86
+ onChangeFile={(newFile) => setFile(newFile)}
87
+ />
88
+ ```
89
+
90
+ | Prop | Type | Required | Description |
91
+ | ---------------- | --------------------------------------------------- | -------- | ------------------------------------------------------- |
92
+ | `file` | `File` | yes | The PDF file object to display |
93
+ | `workerSrc` | `string` | yes | URL of the pdfjs worker script (see Quick start) |
94
+ | `sidebar` | `ReactNode` | no | Content rendered on the left side |
95
+ | `toolbar` | `ReactNode` | no | Content rendered in the top toolbar |
96
+ | `onChangeFile` | `(file: File) => void` | no | Called when the user opens a different file |
97
+ | `defaultTheme` | `"light" \| "dark"` | no | Initial theme mode (default: `"light"`) |
98
+ | `themeOverrides` | `{ light?: Partial<Theme>; dark?: Partial<Theme> }` | no | Override individual theme tokens (see Theming) |
99
+ | `defaultFit` | `"width" \| "page"` | no | Initial zoom mode when a PDF is loaded |
100
+ | `language` | `"it" \| "en"` | no | UI language (default: `"en"`) |
101
+ | `className` | `string` | no | CSS class applied to the root layout element |
102
+ | `style` | `React.CSSProperties` | no | Inline style applied to the root layout element |
103
+ | `requireRead` | `boolean` | no | Show a mandatory-reading banner (see Mandatory reading) |
104
+ | `onDocumentRead` | `() => void` | no | Called once when the user reaches the last page |
105
+
106
+ ---
107
+
108
+ ## Theming
109
+
110
+ `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.
111
+
112
+ ```tsx
113
+ import { PDFViewer, darkTheme } from 'pdf-tsx'
114
+
115
+ <PDFViewer
116
+ themeOverrides={{
117
+ dark: { accent: '#e11d48', accentHover: '#be123c', accentDark: '#9f1239' },
118
+ light: { accent: '#e11d48', accentHover: '#be123c', accentDark: '#9f1239' },
119
+ }}
120
+ ...
121
+ />
122
+ ```
123
+
124
+ Both `darkTheme` and `lightTheme` are exported so you can spread them as a base:
125
+
126
+ ```tsx
127
+ import { darkTheme } from 'pdf-tsx'
128
+
129
+ themeOverrides={{ dark: { ...darkTheme, bg: '#0f0f0f' } }}
130
+ ```
131
+
132
+ **Theme tokens** (`Theme` interface):
133
+
134
+ | Token | CSS variable | Description |
135
+ | ------------------ | ------------------------- | ------------------------------------------------------------- |
136
+ | `bg` | `--pdf-bg` | Main background |
137
+ | `surface` | `--pdf-surface` | Card / panel surface |
138
+ | `surfaceHover` | `--pdf-surface-hover` | Surface hover state |
139
+ | `border` | `--pdf-border` | Default border color |
140
+ | `borderLight` | `--pdf-border-light` | Subtle border color |
141
+ | `scrollArea` | `--pdf-scroll-area` | Scroll container background |
142
+ | `textPrimary` | `--pdf-text-primary` | Primary text |
143
+ | `textSecondary` | `--pdf-text-secondary` | Secondary text |
144
+ | `textMuted` | `--pdf-text-muted` | Muted / disabled text |
145
+ | `textPlaceholder` | `--pdf-text-placeholder` | Input placeholder text |
146
+ | `accent` | `--pdf-accent` | Accent / interactive color |
147
+ | `accentHover` | `--pdf-accent-hover` | Accent hover state |
148
+ | `accentDark` | `--pdf-accent-dark` | Accent dark variant |
149
+ | `accentLight` | `--pdf-accent-light` | Accent light variant |
150
+ | `accentSubtle` | `--pdf-accent-subtle` | Accent with low opacity (backgrounds) |
151
+ | `panelBg` | `--pdf-panel-bg` | Sidebar panel background |
152
+ | `panelBorder` | `--pdf-panel-border` | Sidebar panel border |
153
+ | `panelHeaderBg` | `--pdf-panel-header-bg` | Sidebar panel header background |
154
+ | `shadowPage` | `--pdf-shadow-page` | Drop shadow color for PDF pages |
155
+ | `badgeBg` | `--pdf-badge-bg` | Count badge background (signatures, annotations, attachments) |
156
+ | `toolbarSeparator` | `--pdf-toolbar-separator` | `PDFToolbarSeparator` line color |
157
+ | `fontFamily` | `--pdf-font-family` | Font family for all viewer UI text |
158
+
159
+ ---
160
+
161
+ ## PDFSidebar
162
+
163
+ 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`).
164
+
165
+ ```tsx
166
+ import { PDFSidebar } from "pdf-tsx";
167
+ import { LuLayoutGrid, LuBookOpen } from "react-icons/lu";
168
+
169
+ <PDFSidebar
170
+ panels={[
171
+ {
172
+ icon: <LuLayoutGrid size={18} />,
173
+ label: "Thumbnails",
174
+ content: <PDFThumbnails />,
175
+ },
176
+ {
177
+ icon: <LuBookOpen size={18} />,
178
+ label: "Outline",
179
+ content: <PDFOutline />,
180
+ },
181
+ ]}
182
+ />;
183
+ ```
184
+
185
+ | Prop | Type | Description |
186
+ | -------- | ---------------- | -------------------------- |
187
+ | `panels` | `SidebarPanel[]` | Array of panels to display |
188
+
189
+ **SidebarPanel**
190
+
191
+ | Field | Type | Description |
192
+ | --------- | ----------- | -------------------------------------- |
193
+ | `icon` | `ReactNode` | Icon shown in the sidebar button bar |
194
+ | `label` | `string` | Panel title shown in the header |
195
+ | `content` | `ReactNode` | Content rendered inside the open panel |
196
+
197
+ ### Sidebar hide/show pattern
198
+
199
+ 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.
200
+
201
+ ```tsx
202
+ import { PDFSidebar, PDFSidebarToggle, PDFToolbar } from 'pdf-tsx'
203
+ import { LuPanelLeftClose, LuPanelLeftOpen, LuEllipsis } from 'react-icons/lu'
204
+
205
+ <PDFViewer
206
+ sidebar={
207
+ <PDFSidebar panels={[...]} />
208
+ }
209
+ toolbar={
210
+ <PDFToolbar overflowIcon={<LuEllipsis size={16} />}>
211
+ <PDFSidebarToggle
212
+ collapseIcon={<LuPanelLeftClose size={16} />}
213
+ expandIcon={<LuPanelLeftOpen size={16} />}
214
+ />
215
+ {/* other toolbar items */}
216
+ </PDFToolbar>
217
+ }
218
+ />
219
+ ```
220
+
221
+ ---
222
+
223
+ ## PDFToolbar (responsive toolbar)
224
+
225
+ `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.
226
+
227
+ ```tsx
228
+ import { PDFToolbar } from "pdf-tsx";
229
+ import {
230
+ LuEllipsis,
231
+ LuPanelLeftClose,
232
+ LuPanelLeftOpen,
233
+ LuSearch,
234
+ LuChevronUp,
235
+ LuChevronDown,
236
+ LuX,
237
+ LuZoomOut,
238
+ LuZoomIn,
239
+ LuChevronLeft,
240
+ LuChevronRight,
241
+ } from "react-icons/lu";
242
+
243
+ <PDFViewer
244
+ toolbar={
245
+ <PDFToolbar overflowIcon={<LuEllipsis size={16} />}>
246
+ <PDFSidebarToggle
247
+ collapseIcon={<LuPanelLeftClose size={16} />}
248
+ expandIcon={<LuPanelLeftOpen size={16} />}
249
+ />
250
+ <PDFSearch
251
+ searchIcon={<LuSearch size={14} />}
252
+ prevIcon={<LuChevronUp size={14} />}
253
+ nextIcon={<LuChevronDown size={14} />}
254
+ clearIcon={<LuX size={14} />}
255
+ />
256
+ <PDFZoomControls
257
+ zoomOutIcon={<LuZoomOut size={16} />}
258
+ zoomInIcon={<LuZoomIn size={16} />}
259
+ />
260
+ <PDFNavigation
261
+ prevIcon={<LuChevronLeft size={16} />}
262
+ nextIcon={<LuChevronRight size={16} />}
263
+ />
264
+ {/* more items */}
265
+ </PDFToolbar>
266
+ }
267
+ />;
268
+ ```
269
+
270
+ | Prop | Type | Description |
271
+ | -------------- | ----------- | ----------------------------------------------------- |
272
+ | `children` | `ReactNode` | Toolbar items in priority order (left = highest) |
273
+ | `overflowIcon` | `ReactNode` | Icon for the `···` overflow button (default: `"···"`) |
274
+
275
+ **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.
276
+
277
+ **Custom items** — wrap any custom component in `PDFToolbarItem` to give it a dropdown label:
278
+
279
+ ```tsx
280
+ import { PDFToolbarItem } from "pdf-tsx";
281
+
282
+ <PDFToolbar overflowIcon={<LuEllipsis size={16} />}>
283
+ <PDFToolbarItem label="Custom">
284
+ <MyCustomButton />
285
+ </PDFToolbarItem>
286
+ </PDFToolbar>;
287
+ ```
288
+
289
+ ---
290
+
291
+ ## Feature components
292
+
293
+ All feature components must be rendered inside `<PDFViewer>`. They read and write state via `usePDFViewer()` internally.
294
+
295
+ | Component | Icon props | Description |
296
+ | --------------------- | ---------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
297
+ | `PDFToolbar` | `overflowIcon` | Responsive toolbar — collapses items into `···` dropdown when space runs out |
298
+ | `PDFToolbarItem` | — | Wrapper for custom toolbar items to add a dropdown label |
299
+ | `PDFToolbarSeparator` | — | Vertical divider between toolbar groups — hidden automatically in the overflow dropdown |
300
+ | `PDFThumbnails` | — | Page thumbnail strip for quick navigation |
301
+ | `PDFOutline` | — | Document outline / bookmarks tree |
302
+ | `PDFAnnotations` | — | Display PDF annotations |
303
+ | `PDFAnnotationsIcon` | `icon` | Sidebar icon with annotation count badge |
304
+ | `PDFSignatures` | — | Display PDF digital signatures |
305
+ | `PDFSignaturesIcon` | `icon` | Sidebar icon with signature count badge |
306
+ | `PDFAttachments` | — | List embedded file attachments with download buttons |
307
+ | `PDFAttachmentsIcon` | `icon` | Sidebar icon with attachment count badge |
308
+ | `PDFSearch` | `searchIcon` `prevIcon` `nextIcon` `clearIcon` | Full-text search with match highlighting. Accepts optional `defaultSearch` to pre-fill and auto-navigate |
309
+ | `PDFZoomControls` | `zoomInIcon` `zoomOutIcon` | Zoom in / zoom out buttons with percentage display |
310
+ | `PDFPageFit` | `fitWidthIcon` `fitPageIcon` | Fit-to-width and fit-to-page buttons |
311
+ | `PDFRotationControl` | `icon` | Rotate page clockwise |
312
+ | `PDFNavigation` | `prevIcon` `nextIcon` | Previous / next page buttons with current page input |
313
+ | `PDFDownloadButton` | `icon` | Download the current PDF file |
314
+ | `PDFChangeFile` | `icon` | Button to open a new PDF file |
315
+ | `PDFThemeToggle` | `lightIcon` `darkIcon` | Toggle between dark and light mode |
316
+ | `PDFPrintButton` | `icon` | Print the PDF using the browser's native print dialog |
317
+ | `PDFSidebarToggle` | `collapseIcon` `expandIcon` | Toolbar button to expand the sidebar — visible only when sidebar is hidden (see Sidebar hide/show pattern) |
318
+
319
+ ### PDFSearch
320
+
321
+ | Prop | Type | Required | Description |
322
+ | --------------- | ----------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
323
+ | `searchIcon` | `ReactNode` | yes | Icon shown inside the search input |
324
+ | `prevIcon` | `ReactNode` | yes | Icon for the "previous match" button |
325
+ | `nextIcon` | `ReactNode` | yes | Icon for the "next match" button |
326
+ | `clearIcon` | `ReactNode` | yes | Icon for the "clear search" button |
327
+ | `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. |
328
+
329
+ ```tsx
330
+ <PDFSearch
331
+ initialSearch="invoice"
332
+ searchIcon={<LuSearch size={14} />}
333
+ prevIcon={<LuChevronUp size={14} />}
334
+ nextIcon={<LuChevronDown size={14} />}
335
+ clearIcon={<LuX size={14} />}
336
+ />
337
+ ```
338
+
339
+ ---
340
+
341
+ ### Count badges
342
+
343
+ `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.
344
+
345
+ ```tsx
346
+ import {
347
+ PDFSidebar,
348
+ PDFSignatures,
349
+ PDFSignaturesIcon,
350
+ PDFAnnotations,
351
+ PDFAnnotationsIcon,
352
+ PDFAttachments,
353
+ PDFAttachmentsIcon,
354
+ } from "pdf-tsx";
355
+ import { LuSignature, LuStickyNote, LuPaperclip } from "react-icons/lu";
356
+
357
+ <PDFSidebar
358
+ panels={[
359
+ {
360
+ icon: <PDFSignaturesIcon icon={<LuSignature size={17} />} />,
361
+ label: "Signatures",
362
+ content: <PDFSignatures />,
363
+ },
364
+ {
365
+ icon: <PDFAnnotationsIcon icon={<LuStickyNote size={17} />} />,
366
+ label: "Annotations",
367
+ content: <PDFAnnotations />,
368
+ },
369
+ {
370
+ icon: <PDFAttachmentsIcon icon={<LuPaperclip size={17} />} />,
371
+ label: "Attachments",
372
+ content: <PDFAttachments />,
373
+ },
374
+ ]}
375
+ />;
376
+ ```
377
+
378
+ The badge uses `var(--pdf-accent)` and respects `themeOverrides`.
379
+
380
+ ---
381
+
382
+ ## Mandatory reading
383
+
384
+ Use `requireRead` when the user must scroll through the entire document before proceeding (e.g. terms and conditions, consent forms).
385
+
386
+ ```tsx
387
+ const [canProceed, setCanProceed] = useState(false)
388
+
389
+ <PDFViewer
390
+ requireRead
391
+ onDocumentRead={() => setCanProceed(true)}
392
+ ...
393
+ />
394
+
395
+ <button disabled={!canProceed}>Accetto</button>
396
+ ```
397
+
398
+ `PDFViewer` renders a sticky banner at the bottom of the scroll area:
399
+
400
+ - **Pending** — accent color, arrow-down icon, label `requireReadPending` ("Scroll to the end to continue")
401
+ - **Confirmed** — green (`#16a34a`), checkmark icon, label `requireReadDone` ("Document read") — the banner stays visible after confirmation
402
+
403
+ `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.
404
+
405
+ ---
406
+
407
+ ## Localisation
408
+
409
+ `PDFViewer` ships with English (default) and Italian label sets. Switch via the `language` prop:
410
+
411
+ ```tsx
412
+ <PDFViewer language="it" ... />
413
+ ```
414
+
415
+ Both label objects are exported if you need to inspect them:
416
+
417
+ ```tsx
418
+ import { italianLabels, englishLabels } from "pdf-tsx";
419
+ ```
420
+
421
+ 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.
422
+
423
+ ---
424
+
425
+ ## Full example
426
+
427
+ ```tsx
428
+ import { useState, Suspense, lazy } from "react";
429
+ import {
430
+ PDFSidebar,
431
+ PDFSidebarToggle,
432
+ PDFToolbar,
433
+ PDFToolbarItem,
434
+ PDFToolbarSeparator,
435
+ PDFThumbnails,
436
+ PDFOutline,
437
+ PDFSearch,
438
+ PDFZoomControls,
439
+ PDFPageFit,
440
+ PDFRotationControl,
441
+ PDFNavigation,
442
+ PDFDownloadButton,
443
+ PDFChangeFile,
444
+ PDFThemeToggle,
445
+ PDFPrintButton,
446
+ PDFSignatures,
447
+ PDFSignaturesIcon,
448
+ PDFAnnotations,
449
+ PDFAnnotationsIcon,
450
+ PDFAttachments,
451
+ PDFAttachmentsIcon,
452
+ } from "pdf-tsx";
453
+ import {
454
+ LuLayoutGrid,
455
+ LuBookOpen,
456
+ LuSignature,
457
+ LuStickyNote,
458
+ LuPaperclip,
459
+ LuSearch,
460
+ LuChevronUp,
461
+ LuChevronDown,
462
+ LuX,
463
+ LuZoomIn,
464
+ LuZoomOut,
465
+ LuArrowLeftRight,
466
+ LuScan,
467
+ LuRotateCw,
468
+ LuChevronLeft,
469
+ LuChevronRight,
470
+ LuDownload,
471
+ LuFolderPlus,
472
+ LuPrinter,
473
+ LuSun,
474
+ LuMoon,
475
+ LuPanelLeftClose,
476
+ LuPanelLeftOpen,
477
+ LuEllipsis,
478
+ } from "react-icons/lu";
479
+ import workerUrl from "pdfjs-dist/build/pdf.worker.min.mjs?url";
480
+
481
+ const PDFViewer = lazy(() =>
482
+ import("pdf-tsx").then((m) => ({ default: m.PDFViewer })),
483
+ );
484
+
485
+ function App() {
486
+ const [file, setFile] = useState<File | null>(null);
487
+
488
+ if (!file) {
489
+ return (
490
+ <input
491
+ type="file"
492
+ accept=".pdf"
493
+ onChange={(e) => setFile(e.target.files![0])}
494
+ />
495
+ );
496
+ }
497
+
498
+ return (
499
+ <Suspense fallback={<div>Loading...</div>}>
500
+ <PDFViewer
501
+ file={file}
502
+ workerSrc={workerUrl}
503
+ defaultFit="width"
504
+ onChangeFile={setFile}
505
+ sidebar={
506
+ <PDFSidebar
507
+ panels={[
508
+ {
509
+ icon: <LuLayoutGrid size={18} />,
510
+ label: "Thumbnails",
511
+ content: <PDFThumbnails />,
512
+ },
513
+ {
514
+ icon: <LuBookOpen size={18} />,
515
+ label: "Outline",
516
+ content: <PDFOutline />,
517
+ },
518
+ {
519
+ icon: <PDFSignaturesIcon icon={<LuSignature size={17} />} />,
520
+ label: "Signatures",
521
+ content: <PDFSignatures />,
522
+ },
523
+ {
524
+ icon: <PDFAnnotationsIcon icon={<LuStickyNote size={17} />} />,
525
+ label: "Annotations",
526
+ content: <PDFAnnotations />,
527
+ },
528
+ {
529
+ icon: <PDFAttachmentsIcon icon={<LuPaperclip size={17} />} />,
530
+ label: "Attachments",
531
+ content: <PDFAttachments />,
532
+ },
533
+ ]}
534
+ />
535
+ }
536
+ toolbar={
537
+ <PDFToolbar overflowIcon={<LuEllipsis size={16} />}>
538
+ <PDFSidebarToggle
539
+ collapseIcon={<LuPanelLeftClose size={16} />}
540
+ expandIcon={<LuPanelLeftOpen size={16} />}
541
+ />
542
+ <PDFToolbarSeparator />
543
+ <PDFSearch
544
+ searchIcon={<LuSearch size={14} />}
545
+ prevIcon={<LuChevronUp size={14} />}
546
+ nextIcon={<LuChevronDown size={14} />}
547
+ clearIcon={<LuX size={14} />}
548
+ />
549
+ <PDFToolbarSeparator />
550
+ <PDFZoomControls
551
+ zoomOutIcon={<LuZoomOut size={16} />}
552
+ zoomInIcon={<LuZoomIn size={16} />}
553
+ />
554
+ <PDFRotationControl icon={<LuRotateCw size={16} />} />
555
+ <PDFPageFit
556
+ fitWidthIcon={<LuArrowLeftRight size={16} />}
557
+ fitPageIcon={<LuScan size={16} />}
558
+ />
559
+ <PDFToolbarSeparator />
560
+ <PDFNavigation
561
+ prevIcon={<LuChevronLeft size={16} />}
562
+ nextIcon={<LuChevronRight size={16} />}
563
+ />
564
+ <PDFToolbarSeparator />
565
+ <PDFThemeToggle
566
+ lightIcon={<LuSun size={16} />}
567
+ darkIcon={<LuMoon size={16} />}
568
+ />
569
+ <PDFPrintButton icon={<LuPrinter size={16} />} />
570
+ <PDFChangeFile icon={<LuFolderPlus size={16} />} />
571
+ <PDFDownloadButton icon={<LuDownload size={15} />} />
572
+ {/* custom button with overflow label */}
573
+ <PDFToolbarItem label="Share">
574
+ <button onClick={() => {}}>Share</button>
575
+ </PDFToolbarItem>
576
+ </PDFToolbar>
577
+ }
578
+ />
579
+ </Suspense>
580
+ );
581
+ }
582
+ ```
583
+
584
+ ---
585
+
586
+ ## Keyboard shortcuts
587
+
588
+ When focus is not on an input or textarea, the following keys are active:
589
+
590
+ | Key | Action |
591
+ | -------------------------- | ------------- |
592
+ | `ArrowLeft` / `ArrowUp` | Previous page |
593
+ | `ArrowRight` / `ArrowDown` | Next page |
594
+ | `r` / `R` | Rotate +90° |
595
+
596
+ ## Gesture & zoom controls
597
+
598
+ Zoom can be controlled via toolbar buttons (`PDFZoomControls`), keyboard/trackpad gestures, and touch gestures — no configuration required.
599
+
600
+ | Input | Action |
601
+ | ------------------------ | ------------------------------------- |
602
+ | `Ctrl + scroll` (mouse) | Zoom in / out |
603
+ | `Cmd + scroll` (macOS) | Zoom in / out |
604
+ | Trackpad pinch (desktop) | Zoom in / out |
605
+ | Two-finger pinch (touch) | Zoom in / out |
606
+ | Double tap (touch) | Toggle between 2× zoom and 1× (reset) |
607
+
608
+ Zoom is clamped to the range `[0.2, 3.0]`. Any manual gesture clears the active `fitMode` (`"width"` / `"page"`).
609
+
610
+ ---
611
+
612
+ ## Performance
613
+
614
+ `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.
615
+
616
+ This means:
617
+
618
+ - Initial load renders at most 5 pages, regardless of document length
619
+ - Memory and CPU usage stay flat as the user scrolls through a large document
620
+ - Pages that have already been rendered are kept in the DOM as the user scrolls, avoiding redundant re-renders
621
+
622
+ 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`.
623
+
624
+ No configuration is needed — this is the default behaviour.
625
+
626
+ ---
627
+
628
+ ## License
629
+
630
+ **PolyForm Noncommercial License 1.0.0** © Riccardo Grossano — see [LICENSE](./LICENSE) for full terms.
631
+
632
+ Free for noncommercial use (personal, research, education, non-profit). **Commercial use requires a separate commercial license** — _coming soon_.