@capgo/capacitor-pdf-viewer

中文文档
8.2.8发布于 2026/10/15MPL-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 4万

Capacitor plugin for opening PDFs inside the app.

原始使用文档
文档目录23 节
  1. ➡️ Get started for free
  2. Features
  3. Use cases
  4. Documentation
  5. Compatibility
  6. Install
  7. iOS setup
  8. Android setup
  9. Web
  10. Usage
  11. API
  12. open(...)
  13. close()
  14. goToPage(...)
  15. setZoom(...)
  16. getPluginVersion()
  17. addListener('load', ...)
  18. addListener('pageChange', ...)
  19. addListener('error', ...)
  20. addListener('close', ...)
  21. addListener('linkTap', ...)
  22. Interfaces
  23. Type Aliases

@capgo/capacitor-pdf-viewer

Capgo - Build Capacitor apps with AI, ship updates instantly

Capgo: push fixes to your Capacitor users in minutes, build signed iOS and Android apps without a Mac, and roll back in one click.

➡️ Get started for free

14-day unlimited free trial. No credit card required

🤖 Building with an AI coding agent? Install Capgo skills so Claude Code, Codex, Cursor, Copilot, or Gemini CLI know how to install and use @capgo/capacitor-pdf-viewer in your Capacitor app: npx skills add Cap-go/capgo-skills. Then ask your agent: "Add @capgo/capacitor-pdf-viewer to my Capacitor app and wire it up." Agent setup guides · Hosted MCP

@capgo/capacitor-pdf-viewer embeds native PDF viewing in Capacitor apps on iOS, Android, and web. Instead of fighting WebView limitations or shipping a heavy JavaScript PDF renderer, you open files from a path, HTTPS URL (with custom headers), or base64 and get system-grade rendering: page navigation, pinch zoom, continuous or single-page scrolling, password-protected documents, fullscreen or inline layout, and events for load, errors, page changes, link taps, and close. On iOS it uses PDFKit; on Android it uses Pdfium; on web it uses the browser PDF viewer with blob URLs.

Features

  • Open PDFs from a device path, file:// URL, HTTPS URL, or base64 (including data: URIs)
  • Optional headers when downloading remote URLs (auth tokens, cookies)
  • Password support for encrypted PDFs on iOS and Android
  • Fullscreen overlay or inline mode anchored to a DOM element (elementId)
  • Continuous vertical scrolling or single-page mode
  • Programmatic goToPage, setZoom, and close
  • Events: load, pageChange, error, close, linkTap

Use cases

  • Invoices, manuals, and tickets stored on device or returned from your API as base64
  • Authenticated document download from a signed URL with custom HTTP headers
  • Embedded PDF preview in a form or dashboard region (inline mode)
  • Guided reading flows that jump to a page and listen for link taps inside the PDF

Documentation

  • Plugin docs on capgo.app
  • Tutorial with install steps and examples
  • Capgo for live updates, native builds, and plugin support

Android emulator showing the example PDF open on two pages Example app with inline PDF and controls

Compatibility

Plugin versionCapacitor compatibilityMaintained
v8.*.*v8.*.*✅
v7.*.*v7.*.*On demand
v6.*.*v6.*.*On demand

Policy:

  • New plugins start at version 8.0.0 (Capacitor 8 baseline).
  • Backward compatibility for older Capacitor majors is supported on demand.

Install

npm i @capgo/capacitor-pdf-viewer
npx cap sync

With Bun:

bun add @capgo/capacitor-pdf-viewer
bunx cap sync

iOS setup

No extra Info.plist keys or entitlements are required by this plugin. It uses Apple PDFKit. Remote PDFs use URLSession with the headers you pass in open. For HTTPS URLs, follow your app's usual App Transport Security rules.

Android setup

The plugin library targets minSdk 24 (Android 7.0) and bundles Pdfium (io.legere:pdfiumandroid). The plugin AndroidManifest.xml is empty: it does not declare INTERNET, storage, or FileProvider entries. If you open https sources, add INTERNET to your app manifest:

<uses-permission android:name="android.permission.INTERNET" />

Local files and base64 do not need extra permissions beyond what your app already uses to read those paths.

Web

No Capacitor sync steps are required for web. URL sources are fetched with your headers; setZoom is a no-op because zoom is controlled by the browser viewer. Passwords cannot be injected on web; the browser may prompt on its own.

Usage

import { PdfViewer } from '@capgo/capacitor-pdf-viewer';

await PdfViewer.addListener('pageChange', ({ page, pageCount }) => {
  console.log(`Page ${page} of ${pageCount}`);
});

const { pageCount, page } = await PdfViewer.open({
  source: 'https://example.com/manual.pdf',
  headers: { Authorization: 'Bearer YOUR_TOKEN' },
  mode: 'fullscreen',
  scrollMode: 'continuous',
  page: 1,
});

await PdfViewer.goToPage({ page: 2 });
await PdfViewer.setZoom({ scale: 1.25 });
await PdfViewer.close();

Inline preview in a div:

await PdfViewer.open({
  source: base64FromYourApi,
  sourceType: 'base64',
  mode: 'inline',
  elementId: 'pdf-host',
});

Password-protected file (iOS and Android):

await PdfViewer.open({
  source: '/path/on/device/encrypted.pdf',
  sourceType: 'path',
  password: 'secret',
});

On native shells, loading a PDF from the WebView origin URL often fails because that origin is not a real file server. Fetch the bytes in JavaScript and pass sourceType: 'base64' instead (see example-app/).

API

Native PDF viewer for Capacitor (PDFKit on iOS, Pdfium on Android, browser viewer on web).

open(...)

open(options: OpenPdfOptions) => Promise<OpenPdfResult>

Open a PDF from a file, path, URL, or base64 source.

ParamType
optionsOpenPdfOptions

Returns: Promise<OpenPdfResult>

Since: 8.0.0


close()

close() => Promise<void>

Close the active viewer and remove any overlay.

Since: 8.0.0


goToPage(...)

goToPage(options: GoToPageOptions) => Promise<void>

Jump to a 1-based page number in the open document.

ParamType
optionsGoToPageOptions

Since: 8.0.0


setZoom(...)

setZoom(options: SetZoomOptions) => Promise<void>

Set the zoom scale multiplier (1 is the default fit scale on native). On web this resolves without changing the browser viewer zoom.

ParamType
optionsSetZoomOptions

Since: 8.0.0


getPluginVersion()

getPluginVersion() => Promise<PluginVersionResult>

Get the native or web implementation version marker.

Returns: Promise<PluginVersionResult>

Since: 8.0.0


addListener('load', ...)

addListener(eventName: 'load', listenerFunc: (event: PdfLoadEvent) => void) => Promise<PluginListenerHandle>

Listen for successful PDF load.

ParamType
eventName'load'
listenerFunc(event: PdfLoadEvent) => void

Returns: Promise<PluginListenerHandle>

Since: 8.0.0


addListener('pageChange', ...)

addListener(eventName: 'pageChange', listenerFunc: (event: PdfPageChangeEvent) => void) => Promise<PluginListenerHandle>

Listen for page changes while the user scrolls or when {@link PdfViewerPlugin.goToPage} runs.

ParamType
eventName'pageChange'
listenerFunc(event: PdfPageChangeEvent) => void

Returns: Promise<PluginListenerHandle>

Since: 8.0.0


addListener('error', ...)

addListener(eventName: 'error', listenerFunc: (event: PdfErrorEvent) => void) => Promise<PluginListenerHandle>

Listen for errors while opening or displaying a PDF.

ParamType
eventName'error'
listenerFunc(event: PdfErrorEvent) => void

Returns: Promise<PluginListenerHandle>

Since: 8.0.0


addListener('close', ...)

addListener(eventName: 'close', listenerFunc: (event: PdfCloseEvent) => void) => Promise<PluginListenerHandle>

Listen for viewer close (user gesture or {@link PdfViewerPlugin.close}).

ParamType
eventName'close'
listenerFunc(event: PdfCloseEvent) => void

Returns: Promise<PluginListenerHandle>

Since: 8.0.0


addListener('linkTap', ...)

addListener(eventName: 'linkTap', listenerFunc: (event: PdfLinkTapEvent) => void) => Promise<PluginListenerHandle>

Listen for taps on links inside the PDF.

ParamType
eventName'linkTap'
listenerFunc(event: PdfLinkTapEvent) => void

Returns: Promise<PluginListenerHandle>

Since: 8.0.0


Interfaces

OpenPdfResult

Result returned after a PDF successfully opens.

PropTypeDescription
pageCountnumberTotal number of pages in the document.
pagenumberCurrent page, 1-based.

OpenPdfOptions

Options for {@link PdfViewerPlugin.open}.

PropTypeDescriptionDefaultSince
sourcestringPDF location: file URL, device path, https URL, or base64 (raw or data: URI).
sourceTypePdfSourceTypeExplicit source kind. When omitted: - data: or raw base64 without a scheme => base64 - http/https => url - otherwise => file (treated like a path or file URL; path is an alias on native)8.0.0
headersRecord<string, string>HTTP headers used when downloading an https url source (for example auth tokens).8.0.0
passwordstringPassword for encrypted PDFs on iOS and Android. On web the password cannot be injected into the browser viewer; the browser may still prompt.8.0.0
modePdfDisplayModeHow to present the viewer.'fullscreen'8.0.0
elementIdstringDOM element id used when mode is inline. Native code measures this element and places the viewer over it.8.0.0
pagenumberInitial page, 1-based.8.0.0
scrollModePdfScrollModeScroll behavior inside the viewer.'continuous'8.0.0

GoToPageOptions

Options for {@link PdfViewerPlugin.goToPage}.

PropTypeDescription
pagenumberTarget page, 1-based.

SetZoomOptions

Options for {@link PdfViewerPlugin.setZoom}.

PropTypeDescription
scalenumberZoom multiplier. 1 is the default fit scale on native.

PluginVersionResult

Plugin version payload from {@link PdfViewerPlugin.getPluginVersion}.

PropTypeDescription
versionstringVersion identifier returned by the platform implementation (native on iOS/Android, web on web).

PluginListenerHandle

PropType
remove() => Promise<void>

PdfLoadEvent

Payload for the load event.

PropTypeDescription
pageCountnumberTotal number of pages.
pagenumberCurrent page, 1-based.

PdfPageChangeEvent

Payload for the pageChange event.

PropTypeDescription
pagenumberCurrent page, 1-based.
pageCountnumberTotal number of pages.

PdfErrorEvent

Payload for the error event.

PropTypeDescription
messagestringHuman-readable error message.

PdfLinkTapEvent

Payload for the linkTap event.

PropTypeDescription
urlstringDestination URL of the tapped link.

Type Aliases

PdfSourceType

How the source string should be interpreted. When omitted, the plugin infers the type from the string shape.

'file' | 'path' | 'url' | 'base64'

Record

Construct a type with a set of properties K of type T

{ [P in K]: T; }

PdfDisplayMode

Presentation mode for the viewer.

  • fullscreen: covers the app with a native or browser overlay.
  • inline: places the viewer over a DOM element identified by elementId.

'fullscreen' | 'inline'

PdfScrollMode

Page scrolling behavior.

  • continuous: pages flow vertically as one scrollable document.
  • single: one page at a time.

'continuous' | 'single'

PdfCloseEvent

Payload for the close event (empty object).

Record<string, never>

版本与发布记录

@capgo/capacitor-pdf-viewer 共发布 8 个版本,首次发布于 2026/10/01;过去 12 个月发布了 8 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.2.8latest2026/10/1525 天前
8.2.72026/10/1327 天前
8.2.62026/10/091 个月前
8.2.52026/10/081 个月前
8.2.42026/10/081 个月前
8.2.32026/10/011 个月前

关于 @capgo/capacitor-pdf-viewer 的常见问题

@capgo/capacitor-pdf-viewer 是什么?

Capacitor plugin for opening PDFs inside the app.

如何安装 @capgo/capacitor-pdf-viewer?

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

@capgo/capacitor-pdf-viewer 支持 TypeScript 吗?

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

@capgo/capacitor-pdf-viewer 支持 ESM 还是 CommonJS?

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

@capgo/capacitor-pdf-viewer 可以商用吗?

@capgo/capacitor-pdf-viewer 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@capgo/capacitor-pdf-viewer 的下载量有多少?

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

@capgo/capacitor-pdf-viewer 还在维护吗?

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