@anaralabs/lector

中文文档
3.17.0发布于 2026/11/06MITTypeScriptESM + CJS构建来源已验证近 30 天下载 27.8万

A headless PDF viewer for React

原始使用文档
文档目录6 节
  1. Get a PDF on screen
  2. Build the reader your app needs
  3. Use with a coding assistant
  4. Work on Lector
  5. Acknowledgements
  6. License

Lector

A headless PDF viewer for React. Compose pages, text selection, search, and annotations into your own reading experience. Lector handles PDF.js rendering and page virtualization; you control the layout and UI.

Documentation · Live demo · npm · Contributing

Get a PDF on screen

Use React 19+. The current source expects pdfjs-dist ^5.5.207. These docs track main; check the releases against your installed package version.

npm install @anaralabs/lector pdfjs-dist@^5.5.207

Copy the PDF.js worker from your app's installed dependency to its public assets. Recopy it whenever you update PDF.js so the runtime and worker versions match.

mkdir -p public/pdfjs
cp node_modules/pdfjs-dist/legacy/build/pdf.worker.min.mjs public/pdfjs/

Put a PDF at public/sample.pdf, then add this browser-rendered component:

import { CanvasLayer, Page, Pages, Root, TextLayer } from "@anaralabs/lector";
import { GlobalWorkerOptions } from "pdfjs-dist/legacy/build/pdf.mjs";
import "pdfjs-dist/web/pdf_viewer.css";

GlobalWorkerOptions.workerSrc = "/pdfjs/pdf.worker.min.mjs";

export default function PDFViewer() {
  return (
    <Root
      source="/sample.pdf"
      style={{ height: 600 }}
      loader={<p role="status">Loading PDF…</p>}
    >
      <Pages>
        <Page>
          <CanvasLayer />
          <TextLayer />
        </Page>
      </Pages>
    </Root>
  );
}

For Next.js, load the viewer through a Client Component using dynamic(..., { ssr: false }); keep PDF.js setup inside the dynamically loaded module. See installation for the full wrapper, Vite worker setup, and deployments below a path prefix.

Root loads the document and provides its state. Pages owns scrolling and clones one Page template for visible pages. CanvasLayer paints the PDF; TextLayer adds selectable text. Give the viewer a definite height and import the PDF.js stylesheet so its layers align.

Build the reader your app needs

FeatureStart here
Toolbar, page input, and layoutYour first viewer
Authenticated URLs, local files, errors, and self-hosted assetsLoading documents
Page navigation and fit widthNavigation, zoom
Page previewsThumbnails
Text search and highlighted resultsSearch
Selection and citation regionsSelection, highlights
PDF links and editable form fieldsLinks, forms
Dark page renderingDark mode
Props, hooks, and defaultsAPI reference

Lector is a toolkit rather than a finished toolbar or a PDF editor. Your app supplies accessible controls, error UI, and storage for user annotations. Search requires embedded text; it does not perform OCR. Custom highlight overlays do not automatically modify the PDF file. See troubleshooting for worker errors, blank pages, and layout issues.

Use with a coding assistant

Connect an MCP client to https://anara.com/lector/mcp using Streamable HTTP, with no API key. It can search and read every guide through tools and resources. See AI agents and MCP for setup and example requests.

For direct fetching, start with llms.txt, read individual Markdown guides, or use the complete documentation. These exports are generated from the same source as the website.

Work on Lector

From the repository root, with Node.js 22.13+ and pnpm 9.5.0:

pnpm install --frozen-lockfile
pnpm --filter @anaralabs/lector build
pnpm --filter docs dev

Open localhost:3000/docs. The docs app uses the local workspace package. For watch mode, validation commands, and testing in another application, read CONTRIBUTING.md.

Acknowledgements

Inspired by react-pdf-headless and pdfreader. Built on PDF.js and React.

License

MIT © Anara

版本与发布记录

@anaralabs/lector 共发布 77 个版本,首次发布于 2025/03/20;过去 12 个月发布了 53 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.17.0latest2026/11/061 天前
3.16.42026/10/2810 天前
3.16.32026/10/2711 天前
3.16.22026/10/1919 天前
3.16.12026/10/1523 天前
3.16.02026/10/1424 天前

关于 @anaralabs/lector 的常见问题

@anaralabs/lector 是什么?

A headless PDF viewer for React

如何安装 @anaralabs/lector?

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

@anaralabs/lector 支持 TypeScript 吗?

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

@anaralabs/lector 支持 ESM 还是 CommonJS?

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

@anaralabs/lector 可以商用吗?

@anaralabs/lector 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@anaralabs/lector 的下载量有多少?

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

@anaralabs/lector 还在维护吗?

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