|
| 1 | +--- |
| 2 | +title: Loading & Documents |
| 3 | +description: Open PDFs from URLs, local files (buffers), and manage multi-document tabs programmatically. |
| 4 | +--- |
| 5 | + |
| 6 | +import DocumentLoadingExample from '../code-examples/document-loading-example'; |
| 7 | +import { ExampleWrapper } from '@/components/example-wrapper'; |
| 8 | + |
| 9 | +# Loading & Managing Documents |
| 10 | + |
| 11 | +The Snippet includes a robust `document-manager` plugin that handles file loading, error states, and tab management. While the viewer provides a built-in "Open File" button, you often need to trigger these actions from your own application logic—for example, loading a file when a user clicks a link in your dashboard. |
| 12 | + |
| 13 | +## Configuration |
| 14 | + |
| 15 | +You can configure initial documents and limits via the `documentManager` property. |
| 16 | + |
| 17 | +```javascript |
| 18 | +const viewer = EmbedPDF.init({ |
| 19 | + // ... |
| 20 | + documentManager: { |
| 21 | + // Load these files on startup |
| 22 | + initialDocuments: [ |
| 23 | + { |
| 24 | + url: 'https://snippet.embedpdf.com/ebook.pdf', |
| 25 | + // By default, autoActivate is true. |
| 26 | + // This document will open and become active. |
| 27 | + autoActivate: true, |
| 28 | + // OPTIONAL: Set a custom ID so you can easily reference |
| 29 | + // this document later (e.g. to scroll or close it). |
| 30 | + documentId: 'ebook-embedpdf', |
| 31 | + }, |
| 32 | + { |
| 33 | + url: 'https://snippet.embedpdf.com/ebook.pdf', |
| 34 | + // If we don't set this to false, this document would |
| 35 | + // steal focus immediately after loading! |
| 36 | + autoActivate: false |
| 37 | + } |
| 38 | + ], |
| 39 | + // Limit total open tabs (older ones may be closed) |
| 40 | + maxDocuments: 10 |
| 41 | + }, |
| 42 | + // Show the built-in tab bar if you want tabs inside the viewer |
| 43 | + tabBar: 'multiple' // 'always', 'multiple' (default), or 'never' |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +## Programmatic Loading |
| 48 | + |
| 49 | +You can control document loading using the `document-manager` plugin API. |
| 50 | + |
| 51 | +<CodeExample |
| 52 | + codePaths={[ |
| 53 | + "content/docs/snippet/code-examples/document-loading-example.tsx" |
| 54 | + ]} |
| 55 | +> |
| 56 | + <ExampleWrapper> |
| 57 | + <DocumentLoadingExample /> |
| 58 | + </ExampleWrapper> |
| 59 | +</CodeExample> |
| 60 | + |
| 61 | +### Loading from URL |
| 62 | + |
| 63 | +Use `openDocumentUrl` to load remote files. |
| 64 | + |
| 65 | +```javascript |
| 66 | +const registry = await viewer.registry; |
| 67 | +const docManager = registry.getPlugin('document-manager').provides(); |
| 68 | + |
| 69 | +docManager.openDocumentUrl({ |
| 70 | + url: 'https://snippet.embedpdf.com/ebook.pdf', |
| 71 | + documentId: 'invoice-123', // Optional: fixed ID for easy reference |
| 72 | + autoActivate: true // Switch tab to this document immediately |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +### Loading from Buffer (Local Files) |
| 77 | + |
| 78 | +To load a file selected by the user (via `<input type="file">`) or fetched via a custom API request, use `openDocumentBuffer`. This accepts a raw `ArrayBuffer`. |
| 79 | + |
| 80 | +```javascript |
| 81 | +// Example: Handling a file input change |
| 82 | +async function handleFileSelect(event) { |
| 83 | + const file = event.target.files[0]; |
| 84 | + const buffer = await file.arrayBuffer(); |
| 85 | + |
| 86 | + const registry = await viewer.registry; |
| 87 | + const docManager = registry.getPlugin('document-manager').provides(); |
| 88 | + |
| 89 | + docManager.openDocumentBuffer({ |
| 90 | + buffer: buffer, |
| 91 | + name: file.name, // Display name for the tab |
| 92 | + autoActivate: true |
| 93 | + }); |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Native File Picker |
| 98 | + |
| 99 | +You can also trigger the browser's native file picker dialog directly through the viewer API. |
| 100 | + |
| 101 | +```javascript |
| 102 | +docManager.openFileDialog(); |
| 103 | +``` |
| 104 | + |
| 105 | +## Managing Active Documents |
| 106 | + |
| 107 | +You can switch tabs or close documents programmatically. |
| 108 | + |
| 109 | +```javascript |
| 110 | +// Switch to a specific document |
| 111 | +docManager.setActiveDocument('invoice-123'); |
| 112 | + |
| 113 | +// Close a specific document |
| 114 | +docManager.closeDocument('invoice-123'); |
| 115 | + |
| 116 | +// Close all documents |
| 117 | +docManager.closeAllDocuments(); |
| 118 | +``` |
| 119 | + |
| 120 | +## Events |
| 121 | + |
| 122 | +You can listen for document lifecycle events to sync your external UI (like a sidebar list) with the viewer state. |
| 123 | + |
| 124 | +| Event | Payload | Description | |
| 125 | +| --- | --- | --- | |
| 126 | +| **`onDocumentOpened`** | `DocumentState` | Fired when a document successfully loads. | |
| 127 | +| **`onDocumentClosed`** | `documentId` | Fired when a document tab is closed. | |
| 128 | +| **`onActiveDocumentChanged`** | `{ previous, current }` | Fired when the user switches tabs. | |
| 129 | +| **`onDocumentError`** | `{ documentId, error }` | Fired if a document fails to load (e.g. 404, corrupt PDF). | |
| 130 | + |
| 131 | +```javascript |
| 132 | +docManager.onDocumentOpened((doc) => { |
| 133 | + console.log(`Opened: ${doc.name} (${doc.id})`); |
| 134 | +}); |
| 135 | +``` |
0 commit comments