Viewer
Embed the ready-made NexPDF reader and editor in a web page with Vanilla JS, React, Next.js, Vue, Nuxt, Angular, Svelte or SvelteKit, and drive it with commands, events and the shared UI configuration.
The viewer is the complete NexPDF reader and editor: annotations, forms, signatures, redaction, the
page organiser, the assistant and comments. It runs in an iframe served by the NexPDF WebViewer
server, and your page drives it through postMessage, so the server needs no CORS setup.
Viewer or engine
Use the viewer when you want a finished user interface. Use the engine when you build your own interface or process documents in code.
Start the server
Allow your page's origin to embed the viewer:
NEXPDF_FRAME_ANCESTORS="https://app.example.com" nexpdf-webviewer| Variable | Meaning |
|---|---|
NEXPDF_FRAME_ANCESTORS | Space-separated origins allowed to embed the viewer. Without it no page may frame it |
NEXPDF_ADDR | Listening address, default 127.0.0.1:7878 |
NEXPDF_LICENSE | License key or path of the XML file (see License) |
NEXPDF_UI_CONFIG | Path of a JSON file with the default UI configuration for every page, served at GET /api/ui-config |
NEXPDF_FILE | A document to open at start |
The component's serverUrl defaults to http://127.0.0.1:7878.
Mount the viewer
import { createNexPdfViewer } from '@nexdevit/nexpdf-web';
const viewer = createNexPdfViewer({
container: '#viewer', // element or CSS selector
serverUrl: 'http://127.0.0.1:7878',
document: '/contract.pdf', // URL, File or Uint8Array
initialMode: 'annotations',
theme: 'dark',
language: 'pt',
});
viewer.on('pageChanged', ({ page, pageCount }) => console.log(page, pageCount));
await viewer.ready();new NexPdfViewer(options) does the same as createNexPdfViewer(options).
Every component reloads the document when document changes and applies a new config in place.
Options
| Option | Meaning |
|---|---|
container | Element or CSS selector to mount into (Vanilla JS only) |
serverUrl | The WebViewer server, default http://127.0.0.1:7878 |
document | The first document: a URL, a File or a Uint8Array (PDF, or DOCX, XLSX, PPTX) |
config | The UI configuration, see below |
initialMode | Shortcut for config.modeConfig.initialMode |
theme | light, dark or system; shortcut for config.globalConfig.themeMode |
language | Shortcut for config.globalConfig.language |
licenseKey | A license key activated on the server |
Commands
Every command returns a promise.
| Command | What it does |
|---|---|
ready() | Resolves with the viewer state once the viewer is ready |
loadDocument(source, filename?) | Opens a URL, File or Uint8Array |
getDocument() | The current PDF, with every change applied, as a Blob |
exportAs(format) | The document as a Blob in docx, xlsx, pptx, html, csv, json, xml, rtf or md |
search(query, caseSensitive?, wholeWord?) | Matches with context snippets and PDF rectangles |
setConfig(config) | Replaces the whole UI configuration |
updateConfig(config) | Deep-merges a partial configuration into the current one |
getConfig() | The current configuration (synchronous) |
setMode(mode) | Switches mode |
setTheme(theme) | light, dark or system |
setLanguage(language) | Switches the interface language |
goToPage(page) | Goes to a page, 1-based |
setAnnotationTool(tool) | Picks an annotation tool, or select |
disableElements(ids) / enableElements(ids) | Hides or shows built-in items (the ids of toolbarConfig.hiddenItems) |
setCustomStyle(css) | Injects a stylesheet into the viewer, replacing the previous one |
undo() / redo() | Undoes or redoes the last change |
getState() | The viewer state |
on(event, fn) / off(event, fn) | Subscribes to an event; on returns an unsubscribe function |
destroy() | Removes the iframe and its listeners |
const pdf = await viewer.getDocument();
const docx = await viewer.exportAs('docx');
const link = Object.assign(document.createElement('a'), { href: URL.createObjectURL(pdf), download: 'contract.pdf' });
link.click();The viewer state has mode, modes, page, pageCount, filename, theme, language, canUndo
and canRedo.
Events
| Event | Data |
|---|---|
ready | The viewer state |
documentLoaded, documentChanged | filename, pageCount, canUndo, canRedo |
modeChanged | mode |
pageChanged | page (1-based), pageCount |
themeChanged | theme |
languageChanged | language |
customItemTapped | id, mode, page |
remoteChange | by, revision: another person changed the document |
UI configuration
One JSON document customises the viewer: modes, hidden items, your own buttons, tools, theme, accent colour and language. The same configuration works for the NexPDF readers on iOS, Android, React Native and Electron. Every key is optional, and unknown keys and ids are ignored.
await viewer.setConfig({
modeConfig: { initialMode: 'annotations', availableModes: ['viewer', 'annotations', 'forms'], readOnly: false },
toolbarConfig: {
hiddenItems: ['language', 'menu.ocr'],
customItems: [{ id: 'send', title: 'Send to CRM', icon: 'send', location: 'top' }],
},
annotationsConfig: {
availableTools: ['ink', 'highlight', 'note'],
defaultColor: '#0F766E',
colorPalette: ['#0F766E', '#DC2626', '#2563EB'],
stamps: ['APPROVED', 'REVIEWED'],
},
formsConfig: { availableTools: ['text', 'checkbox', 'signature'] },
readerConfig: { layout: 'continuous', zoom: 'fit-width', nightMode: false },
globalConfig: { themeMode: 'dark', accentColor: '#0F766E', language: 'pt', strings: { pt: { more: 'Opções' } } },
});
viewer.on('customItemTapped', ({ id }) => {
if (id === 'send') sendToCrm();
});| Key | What it does |
|---|---|
modeConfig.initialMode | The mode the viewer opens in: viewer, annotations, content, pages, forms, signatures, security, redaction, measurement or ai |
modeConfig.availableModes | The modes the user can switch to, in this order. With one mode the switcher is hidden |
modeConfig.readOnly | Viewing only: the viewer mode alone, without undo, redo, open, the page editor or editing rows |
toolbarConfig.hiddenItems | Built-in items to hide: brand, modeSwitcher, undo, redo, search, pages, more, open, download, share, theme, language, zoom, layout, rotate, navPanel, pageNav, inspector, license; More menu rows as menu.<id> |
toolbarConfig.order | Built-in ids to move to the front of their bar, in this order |
toolbarConfig.customItems | Your buttons: { id, title, icon?, location?, modes? }. location is top, toolbar (the current mode's tool row, limited to modes) or more (the default). A tap sends customItemTapped |
annotationsConfig.availableTools | note, ink, inkHighlight, eraser, highlight, underline, strikeout, squiggly, square, circle, arc, polygon, cloud, polyline, line, arrow, freetext, callout, stamp, signature, image, link |
annotationsConfig.defaultColor / defaultOpacity | The colour (#RRGGBB or #RRGGBBAA) and opacity (0.05 to 1) new annotations start with |
annotationsConfig.colorPalette | The swatches in the style picker |
annotationsConfig.presets | The style each tool starts with: color, opacity, width, fontSize |
annotationsConfig.rememberStyles | Each tool starts with the last style picked on this device (default true) |
annotationsConfig.stamps | The texts the stamp tool offers, at most 40 characters each |
annotationsConfig.defaultFontSize | The font size of free text |
formsConfig.availableTools | text, checkbox, radio, listbox, combobox, button, signature, barcode |
readerConfig.layout / zoom | single, continuous or double; fit-width, fit-page or a factor such as "1.25" |
readerConfig.nightMode | Shows pages with inverted colours |
collaboration.enabled | Everyone with the viewer open sees who else is there and gets live changes (default true) |
collaboration.userName | The name shown to others and on comments |
globalConfig.themeMode | system, light or dark |
globalConfig.accentColor | The brand colour of buttons, the active tool and selections |
globalConfig.language | en, pt, es, a code from languages, or system |
globalConfig.strings | Text overrides per language |
globalConfig.languages | Extra languages: { "fr": { "name": "Français", "strings": { ... } } }; missing keys fall back to English |
globalConfig.brand | The header title and logo: { title, logoText, logoUrl } |
globalConfig.cssVariables / customCss | CSS custom properties and extra stylesheet text |
The More menu ids on the web are open, download, share, theme, nightMode, outline,
comments, layers, attachments, info, properties, security, watermark, headerFooter,
bates, ocr, reflow, export, background, measure, formsData, signatures, pdfa,
compress, layout and language.
Server defaults: the JSON file named by NEXPDF_UI_CONFIG applies to every page, and the config
you pass on the page is applied over it.
Licensing
The viewer's license is activated on the server, with a key for platform server and app id
nexpdf-webviewer. See License.