Skip to Content
NexPDF

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.

English

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
VariableMeaning
NEXPDF_FRAME_ANCESTORSSpace-separated origins allowed to embed the viewer. Without it no page may frame it
NEXPDF_ADDRListening address, default 127.0.0.1:7878
NEXPDF_LICENSELicense key or path of the XML file (see License)
NEXPDF_UI_CONFIGPath of a JSON file with the default UI configuration for every page, served at GET /api/ui-config
NEXPDF_FILEA 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

OptionMeaning
containerElement or CSS selector to mount into (Vanilla JS only)
serverUrlThe WebViewer server, default http://127.0.0.1:7878
documentThe first document: a URL, a File or a Uint8Array (PDF, or DOCX, XLSX, PPTX)
configThe UI configuration, see below
initialModeShortcut for config.modeConfig.initialMode
themelight, dark or system; shortcut for config.globalConfig.themeMode
languageShortcut for config.globalConfig.language
licenseKeyA license key activated on the server

Commands

Every command returns a promise.

CommandWhat 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

EventData
readyThe viewer state
documentLoaded, documentChangedfilename, pageCount, canUndo, canRedo
modeChangedmode
pageChangedpage (1-based), pageCount
themeChangedtheme
languageChangedlanguage
customItemTappedid, mode, page
remoteChangeby, 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();
});
KeyWhat it does
modeConfig.initialModeThe mode the viewer opens in: viewer, annotations, content, pages, forms, signatures, security, redaction, measurement or ai
modeConfig.availableModesThe modes the user can switch to, in this order. With one mode the switcher is hidden
modeConfig.readOnlyViewing only: the viewer mode alone, without undo, redo, open, the page editor or editing rows
toolbarConfig.hiddenItemsBuilt-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.orderBuilt-in ids to move to the front of their bar, in this order
toolbarConfig.customItemsYour 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.availableToolsnote, ink, inkHighlight, eraser, highlight, underline, strikeout, squiggly, square, circle, arc, polygon, cloud, polyline, line, arrow, freetext, callout, stamp, signature, image, link
annotationsConfig.defaultColor / defaultOpacityThe colour (#RRGGBB or #RRGGBBAA) and opacity (0.05 to 1) new annotations start with
annotationsConfig.colorPaletteThe swatches in the style picker
annotationsConfig.presetsThe style each tool starts with: color, opacity, width, fontSize
annotationsConfig.rememberStylesEach tool starts with the last style picked on this device (default true)
annotationsConfig.stampsThe texts the stamp tool offers, at most 40 characters each
annotationsConfig.defaultFontSizeThe font size of free text
formsConfig.availableToolstext, checkbox, radio, listbox, combobox, button, signature, barcode
readerConfig.layout / zoomsingle, continuous or double; fit-width, fit-page or a factor such as "1.25"
readerConfig.nightModeShows pages with inverted colours
collaboration.enabledEveryone with the viewer open sees who else is there and gets live changes (default true)
collaboration.userNameThe name shown to others and on comments
globalConfig.themeModesystem, light or dark
globalConfig.accentColorThe brand colour of buttons, the active tool and selections
globalConfig.languageen, pt, es, a code from languages, or system
globalConfig.stringsText overrides per language
globalConfig.languagesExtra languages: { "fr": { "name": "Français", "strings": { ... } } }; missing keys fall back to English
globalConfig.brandThe header title and logo: { title, logoText, logoUrl }
globalConfig.cssVariables / customCssCSS 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.

On this page