@embedpdf/react

中文文档
3.2.0发布于 2026/11/12Apache-2.0TypeScriptESM + CJS近 30 天下载 6.6万

React components and hooks for building your own PDF viewer with EmbedPDF.

原始使用文档
文档目录6 节
  1. Quick start
  2. 1. Install
  3. 2. Show a PDF
  4. Every plugin
  5. Documentation
  6. License

@embedpdf/react

React components and hooks for building your own PDF viewer with EmbedPDF.

Quick start

This guide builds a small PDF viewer: the pages, text people can select, zoom buttons, a highlighter and a save button. Each step adds one plugin or one control, so you can stop at any step and have a working viewer.

1. Install

You need two packages: the one for your framework, and an engine.

npm install @embedpdf/react @embedpdf/engine

Every feature is a separate import from the framework package, such as @embedpdf/react/stage, so what you don't use never reaches your bundle.

2. Show a PDF

Create the engine once, then give it to <Viewer> with the plugins you want and the document to open. <Stage> shows the pages, and <RenderLayer> draws each one:

basic.tsx

import { Viewer, DocumentGate } from '@embedpdf/react/runtime';
import type { OpenInput } from '@embedpdf/react/runtime';
import { Stage, stagePlugin } from '@embedpdf/react/stage';
import { RenderLayer, renderPlugin } from '@embedpdf/react/render';
import { localEngine } from '@embedpdf/engine';

// The engine is created synchronously and costs nothing until first use, so
// a module-scope `const engine = …` is safe, even under SSR. Only opening a
// document does real work: the UI renders at t≈0.
const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin()];

// The local engine opens bytes: fetch lazily, under the loading tab.
const welcome = async (): Promise<OpenInput> => {
  const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf');
  return { kind: 'bytes', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) };
};

export default function App() {
  return (
    <Viewer engine={engine} plugins={plugins} initialDocuments={[{ source: welcome }]}>
      <div style={{ height: 500 }}>
        {/* Document UI is defined over a document: gate it on having one. */}
        <DocumentGate fallback={<p>Loading…</p>}>
          <Stage style={{ height: '100%' }}>{() => <RenderLayer />}</Stage>
        </DocumentGate>
      </div>
    </Viewer>
  );
}

localEngine() costs nothing until it's used, so creating it at the top of a module is safe, even with server-side rendering. The viewer starts it when it mounts: PDFium boots in a Web Worker in the background, and the first page shows as soon as the document is open. The engine's WebAssembly is part of your build and loads from your own site; see Setup.

<DocumentGate> shows its fallback until the document is open, so the Stage never renders without one.

Every plugin

Each plugin is one import line. Registering a plugin and importing its components come from the same place:

PluginWhat it gives youImport from
StageScrolling, zoom, layouts, page navigation@embedpdf/react/stage
RenderSharp pages at every zoom, deep-zoom tiles@embedpdf/react/render
LinksClickable links to pages and websites@embedpdf/react/link
SelectionSelecting and copying text@embedpdf/react/selection
SearchFinding text, and showing every match@embedpdf/react/search
AnnotationsHighlights, notes, shapes, free text, and your own tools@embedpdf/react/annotation
StampsLibraries of stamps, placed by click or by code@embedpdf/react/stamp
MeasurementsDistances, perimeters and areas at a real scale@embedpdf/react/measurement
RedactionRemoving content for good@embedpdf/react/redaction
FormsFilling and building PDF forms@embedpdf/react/form
SignaturesSigning documents and checking signatures@embedpdf/react/signature
Page editRotating, reordering, inserting and deleting pages@embedpdf/react/page-edit
MetadataA document's title, author and your own fields@embedpdf/react/metadata
FontsThe fonts a document can be written in, drawn as the engine writes them@embedpdf/react/fonts
OutlineA document's outline (its bookmarks), to show, follow and edit@embedpdf/react/outline
AttachmentsThe files a PDF carries: listing, saving, attaching and removing them@embedpdf/react/attachment
ActionsWhat a PDF does by itself: buttons, scripts, printing@embedpdf/react/actions
View managerSeveral documents side by side@embedpdf/react/view-manager
InteractionThe active tool, cursors and touch@embedpdf/react/interaction
CommandsNamed actions with labels, icons and shortcuts@embedpdf/react/commands
ShellWhich panels, dialogs and menus are open@embedpdf/react/shell
TranslationsYour UI in the user's language@embedpdf/react/i18n

Documentation

  • Quick start: Build a PDF viewer step by step, with text selection, zoom buttons, a highlighter and a save button.
  • Setup: Server-side rendering, bundlers, fonts, and hosting the engine yourself.
  • How it works: Plugins, how your UI talks to them, layers, and which document a component talks to.
  • Scroll, zoom & layout: Layouts, zoom modes, page navigation and touch.

License

Apache-2.0

版本与发布记录

@embedpdf/react 共发布 27 个版本,首次发布于 2026/08/06;过去 12 个月发布了 27 个版本。

版本发布日期距今
3.2.0latest2026/11/121 天前
3.1.02026/11/094 天前
3.0.02026/11/076 天前
3.0.0-next.23next2026/11/076 天前
3.0.0-next.222026/11/049 天前
3.0.0-next.212026/11/0310 天前

关于 @embedpdf/react 的常见问题

@embedpdf/react 是什么?

React components and hooks for building your own PDF viewer with EmbedPDF.

如何安装 @embedpdf/react?

在项目目录运行 npm install @embedpdf/react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18)、react-dom(>=18)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@embedpdf/react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@embedpdf/react 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@embedpdf/react 可以商用吗?

@embedpdf/react 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@embedpdf/react 的下载量有多少?

根据 npm 官方下载统计,@embedpdf/react 在 2026/10/14 – 2026/11/12 这 30 天内被下载 66,116 次,日均约 2,204 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@embedpdf/react 还在维护吗?

最新版本 3.2.0 发布于 2026/11/12(1 天前);至今共发布 27 个版本,其中过去 12 个月发布了 27 个;首次发布于 2026/08/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。