HTML, audio and video
Show HTML pages safely in a sandboxed frame, and play audio and video files, inside the AnyPreview 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 }} />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.
Controller
| State | For HTML | For audio and video |
|---|---|---|
page, pageCount | No pages. | No pages. |
zoom, scale | Number zooms scale the page inside its sandbox. | Not supported. |
search | Not 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. |
can | zoom, print, download. | print, download. |
Headless
One wrapper for HTML and media, showing zoom only when it applies:
'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 '@anypreview/react';
import '@anypreview/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>
);
}