Skip to Content
OpenPreview

HTML, audio and video

Show HTML pages safely in a sandboxed frame, and play audio and video files, inside the OpenPreview React viewer.

HTML

HTML files show in a sandboxed <iframe srcdoc> with an opaque origin, and a Content Security Policy blocks network requests. The page cannot reach your app, its cookies or the network.

Two options loosen this when you trust the files:

<DocViewer source={file} html={{ allowScripts: true, allowExternalResources: true }} />
Live: page.html rendered by the library in this page.Open in the playground

Audio and video

Played with the browser's own <audio> and <video> elements, so the formats are whatever the browser supports: MP3, AAC, WAV, Ogg and FLAC for audio, and MP4 (H.264), WebM and often HEVC for video.

Live: tone.wav rendered by the library in this page.Open in the playground

Controller

StateFor HTMLFor audio and video
page, pageCountNo pages.No pages.
zoom, scaleNumber zooms scale the page inside its sandbox.Not supported.
searchNot searchable: the page runs in an isolated, sandboxed frame.Not searchable.
print()Prints the page in a script-free sandbox.Available, but prints only the player.
canzoom, print, download.print, download.
Live: useDocViewer on page.html. Press a command and watch the state change.

Headless

One wrapper for HTML and media, showing zoom only when it applies:

examples/headless/web-and-media.tsx
'use client';
// HTML pages and media. HTML runs in a sandbox, so its text is not searchable from outside
// (can.search is false); it can be zoomed and printed. Audio and video use the browser's own
// player controls, so there is nothing to zoom, search or page through.
import { DocumentSurface, useDocViewer, type DocSource } from '@openpreview/react';
import '@openpreview/react/styles.css';

export default function WebAndMediaViewer({ source, allowScripts = false }: { source: DocSource; allowScripts?: boolean }) {
  const viewer = useDocViewer({ source, html: { allowScripts } });

  return (
    <div className="flex h-full flex-col text-sm">
      <header className="flex items-center gap-3 border-b p-2">
        <strong>{viewer.fileName}</strong>
        {viewer.can.zoom && (
          <>
            <button onClick={viewer.zoomOut}>āˆ’</button>
            <span>{Math.round(viewer.scale * 100)}%</span>
            <button onClick={viewer.zoomIn}>+</button>
          </>
        )}
        {viewer.can.print && viewer.detection?.kind === 'html' && <button onClick={() => void viewer.print()}>Print page</button>}
        <button className="ml-auto" onClick={viewer.download}>Download</button>
      </header>
      <DocumentSurface viewer={viewer} className="min-h-0 flex-1" />
    </div>
  );
}
Live: the example above, built only with the public hooks, running on page.html.

On this page