@capgo/capacitor-printer

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

Capacitor plugin for printing documents, HTML, PDFs, images and web views

原始使用文档
文档目录24 节
  1. ➡️ Get started for free
  2. Key features
  3. Why Capacitor Printer?
  4. Documentation
  5. Compatibility
  6. Install
  7. Usage
  8. API
  9. printBase64(...)
  10. printFile(...)
  11. printHtml(...)
  12. printPdf(...)
  13. printIframe(...)
  14. printWebView(...)
  15. getPluginVersion()
  16. Interfaces
  17. Android Configuration
  18. ProGuard
  19. Variables
  20. Platform Support
  21. Notes
  22. Large Files
  23. Web Platform
  24. Print Styles

@capgo/capacitor-printer

Print documents, HTML, PDFs, images and the current web view from your Capacitor app with the native print dialogs on iOS and Android.

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-printer in your Capacitor app: npx skills add Cap-go/capgo-skills. Then ask your agent: "Add @capgo/capacitor-printer to my Capacitor app and wire it up." Agent setup guides · Hosted MCP

@capgo/capacitor-printer for Capacitor apps

Key features

  • HTML: printHtml() prints an HTML string.
  • PDF and files: printPdf(), printFile() and printBase64() for device files and base64 data.
  • Web content: printWebView() prints the current page and printIframe() prints an iframe.
  • Native dialogs: UIPrintInteractionController on iOS and the Android print framework.
  • Platforms: iOS, Android and Web. Web uses the browser print dialog.

Why Capacitor Printer?

A free and open-source printing solution with comprehensive features:

  • Multiple formats - Print HTML, PDFs, images, and base64 data
  • Native APIs - Uses UIPrintInteractionController (iOS) and PrintManager (Android)
  • Web support - Works across all platforms including web browsers
  • Print customization - Support for CSS print styles and custom page names
  • Cross-platform - Consistent API across iOS, Android, and Web

Perfect for invoice generation, report printing, document sharing, and any app requiring native printing capabilities.

Documentation

The most complete doc is available here: https://capgo.app/docs/plugins/printer/

Compatibility

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

Note: The major version of this plugin follows the major version of Capacitor. Use the version that matches your Capacitor installation (e.g., plugin v8 for Capacitor 8). Only the latest major version is actively maintained.

Install

You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:

npx skills add https://github.com/cap-go/capacitor-skills --skill capacitor-plugins

Then use the following prompt:

Use the `capacitor-plugins` skill from `cap-go/capacitor-skills` to install the `@capgo/capacitor-printer` plugin in my project.

If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:

npm install @capgo/capacitor-printer
npx cap sync

Usage

import { Printer } from '@capgo/capacitor-printer';

// Print HTML content
await Printer.printHtml({
  name: 'My Document',
  html: '<html><body><h1>Hello World</h1><p>This is a test document.</p></body></html>',
});

// Print PDF file
await Printer.printPdf({
  name: 'Invoice',
  path: 'file:///path/to/document.pdf',
});

// Print image from base64
await Printer.printBase64({
  name: 'Photo',
  data: 'JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9UeXBlL...',
  mimeType: 'image/jpeg',
});

// Print current web view
await Printer.printWebView({
  name: 'Web Page',
});

API

printBase64(...)

printBase64(options: PrintBase64Options) => Promise<void>

Presents the printing UI to print files encoded as base64 strings.

Platform Behavior:

  • iOS: Uses UIPrintInteractionController with base64 decoded data
  • Android: Uses PrintManager with base64 decoded data
  • Web: Creates a blob from base64 data and opens print dialog

Performance Warning: Large files can lead to app crashes due to memory constraints when decoding base64. For files larger than 5MB, it's recommended to use printFile() instead.

ParamTypeDescription
optionsPrintBase64Options- The base64 data and configuration for printing

Since: 7.0.0


printFile(...)

printFile(options: PrintFileOptions) => Promise<void>

Presents the printing UI to print device files.

Platform Behavior:

  • iOS: Uses UIPrintInteractionController with file URL. Supports file:// paths or paths relative to app's documents directory.
  • Android: Uses PrintManager with file path. Supports both content:// URIs and file:// paths.
  • Web: Reads file and opens print dialog

Supported File Types:

  • PDF documents (application/pdf)
  • Images: JPEG, PNG, GIF, HEIC, HEIF
ParamTypeDescription
optionsPrintFileOptions- The file path and configuration for printing

Since: 7.0.0


printHtml(...)

printHtml(options: PrintHtmlOptions) => Promise<void>

Presents the printing UI to print HTML documents.

Platform Behavior:

  • iOS: Renders HTML in WKWebView, then prints using UIPrintInteractionController
  • Android: Renders HTML in WebView, then prints using PrintManager
  • Web: Creates iframe with HTML content and triggers print dialog

HTML Requirements:

  • Should be a complete HTML document with proper structure
  • Can include inline CSS styles or style tags
  • External resources (images, stylesheets) should use absolute URLs
  • Print-specific CSS can be added using @media print rules

CSS Print Styling: Use CSS media queries to customize print output:

  • Control page breaks: page-break-before, page-break-after, page-break-inside
  • Hide elements: display: none for no-print classes
  • Adjust font sizes and colors for print
ParamTypeDescription
optionsPrintHtmlOptions- The HTML content and configuration for printing

Since: 7.0.0


printPdf(...)

printPdf(options: PrintPdfOptions) => Promise<void>

Presents the printing UI to print PDF documents.

Platform Behavior:

  • iOS: Uses UIPrintInteractionController with PDF file URL
  • Android: Uses PrintManager with PdfDocument
  • Web: Creates object URL and opens print dialog

File Path Requirements:

  • iOS: Must be file:// path or relative to app's documents directory
  • Android: Supports content:// URIs (from downloads, media store) or file:// paths
  • Web: Must be accessible file path
ParamTypeDescription
optionsPrintPdfOptions- The PDF file path and configuration for printing

Since: 7.0.0


printIframe(...)

printIframe(options: PrintIframeOptions) => Promise<void>

Presents the printing UI for content inside an iframe element.

Useful when the full HTML document is too large to pass to printHtml().

Platform Behavior:

  • iOS: Evaluates JavaScript in the WebView to print the iframe content
  • Android: Evaluates JavaScript in the WebView to print the iframe content
  • Web: Finds the iframe and triggers its print dialog
ParamTypeDescription
optionsPrintIframeOptions- CSS selector and optional print job name

Since: 8.0.19


printWebView(...)

printWebView(options?: PrintOptions | undefined) => Promise<void>
ParamType
optionsPrintOptions

getPluginVersion()

getPluginVersion() => Promise<{ version: string; }>

Get the native Capacitor plugin version.

Returns the hardcoded plugin version from native code (iOS/Android) or package version (Web). This is useful for debugging and ensuring plugin compatibility.

Returns: Promise<{ version: string; }>

Since: 7.0.0


Interfaces

PrintBase64Options

Options for printing base64 encoded data.

PropTypeDescriptionSince
datastringValid base64 encoded string representing the file content. The base64 string should NOT include the data URL prefix (e.g., "data:application/pdf;base64,"). Only provide the raw base64 encoded content. Performance Considerations: - Base64 encoding increases data size by approximately 33% - Large files (>5MB) may cause memory issues when decoding - Consider using printFile() for large documents Platform Notes: - iOS: Decoded to NSData and passed to UIPrintInteractionController - Android: Decoded to byte array and written to temporary file - Web: Converted to Blob for printing7.0.0
mimeTypestringMIME type of the base64 encoded data. Supported types: - application/pdf - PDF documents - image/jpeg - JPEG images - image/png - PNG images - image/gif - GIF images (iOS/Android only) - image/heic - HEIC images (iOS only) - image/heif - HEIF images (iOS only) Platform Support: - All platforms support PDF, JPEG, and PNG - GIF support varies by platform - HEIC/HEIF are iOS-specific formats7.0.0

PrintFileOptions

Options for printing files from device storage.

PropTypeDescriptionDefaultSince
pathstringPath to the file to print. iOS Path Formats: - file:// URL: Full file URL path - Relative path: Path relative to app's documents directory - Must be within app's accessible directories (documents, temporary, cache) Android Path Formats: - content:// URI: Content provider URI (recommended for external files) - file:// path: Direct file system path - Must have read permission for the file Common Use Cases: - App documents: Files saved in app's document directory - Downloads: Files from system downloads folder (use content:// on Android) - Temporary files: Files in app's temporary/cache directory - Shared storage: Files from external storage (Android, requires permissions)7.0.0
mimeTypestringMIME type of the file. Platform Behavior: - Android: REQUIRED for content:// URIs. Helps the system determine how to handle the file. Optional for file:// paths (auto-detected from extension). - iOS: Ignored. File type is auto-detected from file extension. - Web: Optional. Auto-detected if not provided. Common MIME Types: - application/pdf - PDF documents - image/jpeg - JPEG images - image/png - PNG images - image/gif - GIF imagesUndefined (auto-detected from file extension)7.0.0

PrintHtmlOptions

Options for printing HTML content.

PropTypeDescriptionSince
htmlstringHTML content to print. Content Requirements: - Should be a complete HTML document with &lt;html&gt;, &lt;head&gt;, and &lt;body&gt; tags - Can include inline CSS styles or &lt;style&gt; tags - External resources (images, fonts) should use absolute URLs - JavaScript is not executed during print rendering Print Optimization Tips: - Use @media print CSS rules for print-specific styling - Control page breaks with page-break-before, page-break-after, page-break-inside - Hide UI elements using .no-print { display: none; } class - Adjust font sizes for readability (12pt is standard for print) - Use print-friendly colors (avoid dark backgrounds) Platform Rendering: - iOS: Rendered in WKWebView before printing - Android: Rendered in WebView before printing - Web: Rendered in hidden iframe before printing Character Encoding: - UTF-8 is recommended and default - Include charset in HTML: &lt;meta charset="UTF-8"&gt;7.0.0

PrintPdfOptions

Options for printing PDF documents.

PropTypeDescriptionSince
pathstringPath to the PDF document. iOS Path Formats: - file:// URL: Full file URL path to PDF document - Relative path: Path relative to app's documents directory - Must be within app's accessible directories (documents, temporary, cache) - PDF must be valid and not password-protected Android Path Formats: - content:// URI: Content provider URI (recommended for external PDFs) - file:// path: Direct file system path to PDF - Must have read permission for the file - Supports both single-page and multi-page PDFs Web Path Formats: - Relative or absolute path accessible from web context - Must be a valid PDF file Validation: - File must exist at the specified path - File must be a valid PDF (checked by magic number/header) - File must be readable by the app Common Sources: - App documents: PDFs saved in app's document directory - Downloads: PDFs from system downloads (use content:// on Android) - Generated PDFs: Temporary PDFs created by the app - Network downloads: PDFs downloaded and saved locally7.0.0

PrintIframeOptions

Options for printing iframe content.

PropTypeDescriptionSince
selectorstringCSS selector for the iframe element to print. The selector must match a single iframe in the current document.8.0.19

PrintOptions

Base options for all print operations.

PropTypeDescriptionDefaultSince
namestringName of the print job. Usage: - Displayed in the system print queue - Shown in print history/logs - May appear in printer status displays - Used as default filename for "Save as PDF" option Platform Behavior: - iOS: Shown in print preview header and activity view - Android: Displayed in print job notification and print queue - Web: Used as document title in print dialog Best Practices: - Use descriptive names (e.g., "Invoice #12345", "Q4 Report") - Keep under 50 characters for better display - Avoid special characters that may cause issues in filenames - Include relevant identifiers (order numbers, dates, etc.) Examples: - "Invoice #12345" - "Sales Report - 2024 Q4" - "Customer Receipt - John Doe" - "Product Photo - SKU-ABC123"'Document'7.0.0

Android Configuration

ProGuard

If you are using ProGuard, add the following rules to your proguard-rules.pro file:

-keep class com.capgo.printer.** { *; }

Variables

You can configure the following project variables in your variables.gradle file:

ext {
  androidxDocumentFileVersion = '1.0.1'
  androidxPrintVersion = '1.0.0'
}

Platform Support

PlatformSupported
iOS✅
Android✅
Web✅

Notes

Large Files

When printing base64 encoded data, large files can lead to app crashes due to memory constraints. For large files, it's recommended to save them to the device first and use printFile() instead.

Web Platform

On the web platform:

  • printWebView() triggers the browser's native print dialog
  • Other methods create an iframe and print its content
  • Some browsers may have security restrictions on printing certain file types

Print Styles

When printing web content, you can use CSS media queries to customize the print output:

@media print {
  /* Your print styles here */
  body {
    font-size: 12pt;
  }
  .no-print {
    display: none;
  }
}

版本与发布记录

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

版本发布日期距今
8.1.8latest2026/10/1525 天前
8.1.72026/10/1327 天前
8.1.62026/10/091 个月前
8.1.52026/10/081 个月前
8.1.42026/09/161 个月前
8.1.32026/08/292 个月前

其他发布通道:v7 → 7.0.1,lts-v7 → 7.0.14。

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

@capgo/capacitor-printer 是什么?

Capacitor plugin for printing documents, HTML, PDFs, images and web views

如何安装 @capgo/capacitor-printer?

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

@capgo/capacitor-printer 支持 TypeScript 吗?

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

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

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

@capgo/capacitor-printer 可以商用吗?

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

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

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

@capgo/capacitor-printer 还在维护吗?

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