跳过主要内容
返回插件
@capgo/capacitor-打印机
教程
由 github.com/Cap-go

打印机

Capacitor 打印机,用于打印文档,HTML,PDF,图片和网页视图

指南

关于打印机的教程

使用 @capgo/capacitor-printer

Capacitor 打印插件,支持打印文档、HTML、PDF、图片和网页视图。

安装

bun add @capgo/capacitor-printer
bunx cap sync

此插件暴露的内容

  • printBase64 - 提供打印 UI 以打印 base64 编码的文件。
  • printFile - 提供打印 UI 以打印设备文件。
  • printHtml - 提供打印 UI 以打印 HTML 文档。
  • printPdf - 提供打印 UI 以打印 PDF 文档。

示例使用

printBase64

- 提供打印 UI 以打印 base64 编码的文件。

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

// Print a base64 encoded PDF
await Printer.printBase64({
  name: 'Invoice #12345',
  data: 'base64-encoded-pdf-data',
  mimeType: 'application/pdf',
});

// Print a base64 encoded image
await Printer.printBase64({
  name: 'Product Photo',
  data: '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDA...',
  mimeType: 'image/jpeg',
});

printFile

- 提供打印 UI 以打印设备文件。

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

// iOS: Print from app documents directory
await Printer.printFile({
  name: 'Contract',
  path: 'file:///var/mobile/Containers/Data/Application/.../Documents/contract.pdf',
});

// Android: Print from content URI
await Printer.printFile({
  name: 'Receipt',
  path: 'content://com.android.providers.downloads.documents/document/123',
  mimeType: 'application/pdf',
});

// Android: Print from file path
await Printer.printFile({
  name: 'Photo',
  path: 'file:///storage/emulated/0/Download/photo.jpg',
  mimeType: 'image/jpeg',
});

printHtml

- 提供打印 UI 以打印 HTML 文档。

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

// Simple HTML document
await Printer.printHtml({
name: 'Sales Report',
html: '<html><body><h1>Q4 Sales Report</h1><p>Revenue: $125,000</p></body></html>',
});

// HTML with print-specific CSS
await Printer.printHtml({
name: 'Styled Invoice',
html: `
<html>
<head>
<style>

printPdf

- 提供打印 UI 以打印 PDF 文档。

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

// Print PDF from app storage
await Printer.printPdf({
  name: 'Annual Report 2024',
  path: 'file:///var/mobile/Containers/Data/Application/.../Documents/report.pdf',
});

// Print PDF from Android downloads
await Printer.printPdf({
  name: 'Downloaded Document',
  path: 'content://com.android.providers.downloads.documents/document/123',
});

完整参考

继续使用@capgo/capacitor-printer

如果您正在使用 使用@capgo/capacitor-printer 来规划原生插件工作,连接它与 @capgo/capacitor-printer 了解@capgo/capacitor-printer的实现细节 开始使用 了解开始使用的实现细节 Capgo 插件目录 了解Capgo 插件目录中的产品工作流程, Capacitor 插件由 Capgo 提供 为 Capacitor 插件由 Capgo 提供的实现细节, 添加或更新插件 为添加或更新插件的实现细节,