@build-qube/papyra

中文文档
0.8.0发布于 2026/10/27MITTypeScriptES Module构建来源已验证近 30 天下载 4632

Fast PDF rendering for Node and the browser, powered by Rust

原始使用文档
文档目录10 节
  1. Install
  2. Render a page
  3. Thumbnails
  4. Say what is urgent
  5. Export, text, and outlines
  6. Links, labels, and what the file says about itself
  7. Rotate the view
  8. Three things that will bite you
  9. Engines
  10. More

papyra

Fast PDF rendering for Node and the browser, from one Rust core. The engine (hayro) is compiled in — there are no pdfium binaries to ship and nothing to install alongside it.

Install

npm install @build-qube/papyra

The native addon and the browser wasm build ship as optional dependencies; your package manager picks the right one.

Render a page

import { open, paintToCanvas } from '@build-qube/papyra';

const doc = await open(file); // Uint8Array | ArrayBuffer | Blob | File
const page = await doc.renderPage(0, { fitWidth: 1600 });

paintToCanvas(page, canvas);

renderPage never blocks the event loop. page is tightly packed RGBA8, so paintToCanvas is a convenience rather than the only route — toImageData, the encoders, or your own buffer handling all work from the same bytes.

Thumbnails

stream yields pages as they finish, with concurrency bounded for you:

for await (const { page, bitmap } of doc.stream({ fitWidth: 160 })) {
  paint(page, bitmap);
}

Say what is urgent

papyra does not guess which page matters — a viewer knows what is on screen and the library does not. Attach a priority and the queue honours it. Lower runs first, and the default is the most urgent tier, so callers who do not care can ignore this entirely.

const doc = await open(file, { concurrency: 4 }); // viewers want a narrow pool

// Thumbnails yield to whatever is on screen.
for await (const t of doc.stream({ fitWidth: 160, priority: 2 })) { /* … */ }

// Reprioritise on scroll instead of cancelling and resubmitting.
const job = doc.render(12, { fitWidth: 1600, priority: 2 });
onScroll(() => job.setPriority(isVisible(12) ? 0 : 3));

Requests for the same page at the same size coalesce into one render. Pending work reorders freely; work already running is never interrupted.

Export, text, and outlines

const image = await doc.renderImage(0, { fitWidth: 2000 });
await writeFile('page-0.webp', (await image.toWebp()).bytes);

for await (const hit of doc.search('site plan')) {
  highlight(hit.page, hit.rects);
}

for (const node of await doc.outline()) {
  if (node.page !== null) goTo(node.page);
}

renderImage keeps the pixels in Rust and encodes on demand, so nothing but the finished file crosses into JS. For output that has to survive being scaled there is doc.renderSvg(0), which returns vector markup instead of pixels and so takes no size and no quality.

const scale = rendered.width / doc.pageSize(index).width;
for (const link of await doc.links(index)) {
  const box = scaleRect(link.rect, scale);
  if (link.target.kind === 'uri') openExternally(link.target.uri);
  else goTo(link.target.dest.page);
}

const labels = await doc.pageLabels();
const shown = labels[index] || String(index + 1);   // "iv", not "4"

doc.metadata.title;   // null when the document never said
doc.fingerprint;      // stable key for per-document state

Link rectangles are in the same space as extracted text — 72 DPI from the top-left, rotation applied — so one multiply places them over any render. pageLabels() is empty when the document defines no labels, which is your signal to number by index.

Encrypted files throw PasswordRequiredError or IncorrectPasswordError, both extending PasswordError with a retry flag, so one dialog handles the cold ask and the wrong answer.

Rotate the view

const vp = viewport(doc.pageSize(index), { fitWidth: 1600, rotation: 90 });

paintToCanvas(await doc.renderPage(index, { dpi: vp.dpi }), canvas, {
  rotation: vp.rotation,
});
const box = viewportRect(link.rect, vp);   // the link layer, turned to match

Pages render unrotated and the canvas does the turning, so a rotate button costs no render. Size through the viewport rather than with fitWidth directly: turned, the width on screen is the page's height, and vp.dpi carries the answer back.

Renders draw annotations by default. Pass { annotations: false } when you are drawing your own link or highlight layer, or they appear twice.

Three things that will bite you

Size with fitWidth, not dpi. Page areas vary by two orders of magnitude. At a fixed 36 DPI, US Letter is a 216x279 thumbnail while a 42x30in drawing is a 1512x1080, 6.5 MB image. Sizing by output width keeps cost proportional to what you actually display; renders above 100 MP are refused outright.

The browser build needs cross-origin isolation. It uses shared wasm memory, so SharedArrayBuffer must be available. Serve your page with:

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

A panic in the browser build is fatal. It aborts that wasm instance, and papyra fails until the page reloads; natively it is a rejected promise like any other error.

Engines

Other engines — pdf.js for pages that cannot be isolated, Tauri, and the experimental Word and PowerPoint @build-qube/papyra/office — are covered in the Engines guide. Code that opens documents with another engine imports from @build-qube/papyra/core, which never loads papyra's wasm.

More

The full API reference is generated from these sources, so it never drifts from them. The project README covers architecture, benchmarks against pdf.js, and the measurements behind these defaults.

MIT.

版本与发布记录

@build-qube/papyra 共发布 11 个版本,首次发布于 2026/08/27;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.8.0latest2026/10/2713 天前
0.7.12026/10/2415 天前
0.7.02026/10/2416 天前
0.6.02026/10/2316 天前
0.5.02026/10/2317 天前
0.4.02026/10/081 个月前

关于 @build-qube/papyra 的常见问题

@build-qube/papyra 是什么?

Fast PDF rendering for Node and the browser, powered by Rust

如何安装 @build-qube/papyra?

在项目目录运行 npm install @build-qube/papyra,或使用 pnpm add / yarn add / bun add。

@build-qube/papyra 支持 TypeScript 吗?

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

@build-qube/papyra 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@build-qube/papyra 可以商用吗?

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

@build-qube/papyra 的下载量有多少?

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

@build-qube/papyra 还在维护吗?

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