跳过主要内容

指南

打印机教程

使用@capgo/capacitor-打印机

Capacitor打印插件.

安装

bun add @capgo/capacitor-printer
bunx cap sync

此插件暴露的内容

  • printBase64 - 以 base64 编码的字符串形式打印文件的 UI。
  • printFile - 打印设备文件的 UI。
  • printHtml - 打印 HTML 文档的 UI。
  • printPdf - 打印 PDF 文档的 UI。

示例使用

printBase64

以 base64 编码的字符串形式打印文件的 UI。

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

打印 HTML 文档的 UI。

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

打印 PDF 文档的 UI。

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-打印机

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