Panduan Memulai
Salin prompt setup yang berisi langkah install dan panduan markdown lengkap untuk plugin ini.
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-printer`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/printer/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
Anda bisa menggunakan AI-Assisted Setup kami untuk menginstal plugin ini. Tambahkan skill Capgo ke tool AI Anda dengan command berikut:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-pluginsLalu gunakan prompt berikut:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-printer` plugin in my project.Jika Anda lebih memilih Manual Setup, instal plugin dengan menjalankan perintah berikut dan ikuti petunjuk khusus platform di bawah ini:
bun add @capgo/capacitor-printerbunx cap syncimport { Printer } from '@capgo/capacitor-printer';Mulai cepat
Bagian berjudul “Mulai cepat”Cetak PDF yang sudah ada di disk (disarankan untuk file besar).
import { Printer } from '@capgo/capacitor-printer';
await Printer.printFile({ path: 'file:///data/user/0/.../invoice.pdf', name: 'Invoice #1042',});Contoh: struk HTML
Bagian berjudul “Contoh: struk HTML”await Printer.printHtml({ html: '<h1>Thank you</h1><p>Total: $42.00</p>', name: 'Receipt',});Contoh: hindari payload base64 yang terlalu besar
Bagian berjudul “Contoh: hindari payload base64 yang terlalu besar”printBase64 mendekode seluruh file di memori. Untuk aset di atas ~5 MB, tulis ke file temp dan panggil printFile instead.
Kesalahan umum
Bagian berjudul “Kesalahan umum”- Memori: string base64 yang besar dapat membuat aplikasi crash di perangkat dengan RAM rendah.
- Web: pencetakan menggunakan dialog browser, tata letaknya berbeda dari kontroler cetak native.
- Paths: on Android, pass an accessible
file://URI, path filesystem absolut, ataucontent://URI. Path relatif terhadap aplikasi tidak di-resolve secara otomatis.
Ringkasan API
Bagian berjudul “Ikhtisar API”printBase64
Bagian berjudul “printBase64”Menampilkan UI pencetakan untuk mencetak file yang dienkode sebagai string base64.
Perilaku Platform:
- iOS: Menggunakan UIPrintInteractionController dengan data base64 yang didekode
- Android: Menggunakan PrintManager dengan data base64 yang didekode
- Web: Membuat blob dari data base64 dan membuka dialog cetak
Peringatan Performa: File besar dapat menyebabkan crash aplikasi karena keterbatasan memori saat mendekode base64. Untuk file lebih dari 5MB, disarankan menggunakan printFile() sebagai gantinya.
import { Printer } from '@capgo/capacitor-printer';
// Print a base64 encoded PDFawait Printer.printBase64({ name: 'Invoice #12345', data: 'base64-encoded-pdf-data', mimeType: 'application/pdf',});
// Print a base64 encoded imageawait Printer.printBase64({ name: 'Product Photo', data: '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDA...', mimeType: 'image/jpeg',});printFile
Bagian berjudul “printFile”Menampilkan UI pencetakan untuk mencetak file perangkat.
Perilaku Platform:
- iOS: Menggunakan UIPrintInteractionController dengan file URL. Mendukung path file:// atau path relatif terhadap direktori dokumen aplikasi.
- Android: Menggunakan PrintManager dengan file path. Mendukung URI content:// dan path file://.
- Web: Membaca file dan membuka dialog cetak
Jenis File yang Didukung:
- Dokumen PDF (application/pdf)
- Gambar: JPEG, PNG, GIF, HEIC, HEIF
import { Printer } from '@capgo/capacitor-printer';
// iOS: Print from app documents directoryawait Printer.printFile({ name: 'Contract', path: 'file:///var/mobile/Containers/Data/Application/.../Documents/contract.pdf',});
// Android: Print from content URIawait Printer.printFile({ name: 'Receipt', path: 'content://com.android.providers.downloads.documents/document/123', mimeType: 'application/pdf',});
// Android: Print from file pathawait Printer.printFile({ name: 'Photo', path: 'file:///storage/emulated/0/Download/photo.jpg', mimeType: 'image/jpeg',});printHtml
Bagian berjudul “printHtml”Menampilkan UI pencetakan untuk mencetak dokumen HTML.
Perilaku Platform:
- iOS: Merender HTML di WKWebView, lalu mencetak menggunakan UIPrintInteractionController
- Android: Merender HTML di WebView, lalu mencetak menggunakan PrintManager
- Web: Membuat iframe dengan konten HTML dan memicu dialog cetak
Persyaratan HTML:
- Harus berupa dokumen HTML lengkap dengan struktur yang benar
- Dapat menyertakan gaya CSS inline atau tag style
- Sumber daya eksternal (gambar, stylesheet) harus menggunakan URL absolut
- CSS khusus print bisa ditambahkan menggunakan
import { Printer } from '@capgo/capacitor-printer';
// Simple HTML documentawait Printer.printHtml({name: 'Sales Report',html: '<html><body><h1>Q4 Sales Report</h1><p>Revenue: $125,000</p></body></html>',});
// HTML with print-specific CSSawait Printer.printHtml({name: 'Styled Invoice',html: `<html><head><style>printPdf
Bagian berjudul “printPdf”Menampilkan UI pencetakan untuk mencetak dokumen PDF.
Perilaku Platform:
- iOS: Menggunakan UIPrintInteractionController dengan URL file PDF
- Android: Menggunakan PrintManager dengan PdfDocument
- Web: Membuat object URL dan membuka dialog cetak
Persyaratan Path File:
- iOS: Harus berupa path file:// atau relatif terhadap direktori dokumen aplikasi
- Android: Mendukung URI content:// (dari unduhan, media store) atau path file://
- Web: Harus berupa path file yang dapat diakses
import { Printer } from '@capgo/capacitor-printer';
// Print PDF from app storageawait Printer.printPdf({ name: 'Annual Report 2024', path: 'file:///var/mobile/Containers/Data/Application/.../Documents/report.pdf',});
// Print PDF from Android downloadsawait Printer.printPdf({ name: 'Downloaded Document', path: 'content://com.android.providers.downloads.documents/document/123',});printWebView
Bagian berjudul “printWebView”Menampilkan UI pencetakan untuk mencetak konten web view.
Mencetak konten saat ini yang ditampilkan di web view aplikasi Anda.
Perilaku Platform:
- iOS: Menggunakan UIPrintInteractionController dengan view printable dari WKWebView
- Android: Menggunakan WebView.createPrintDocumentAdapter() dengan PrintManager
- Web: Memicu window.print() untuk halaman saat ini
Styling Cetak: Mendukung CSS print media queries untuk kustomisasi. Gaya saat ini dari web view akan diterapkan, termasuk semua
import { Printer } from '@capgo/capacitor-printer';
// Print current web view with default nameawait Printer.printWebView();
// Print with custom job nameawait Printer.printWebView({name: 'Dashboard Screenshot',});
// Use with print-specific CSS in your HTML// Add this to your app's CSS://Referensi Tipe
Bagian berjudul “Referensi Tipe”PrintBase64Options
Bagian berjudul “PrintBase64Options”Opsi untuk mencetak data yang dikodekan base64.
export interface PrintBase64Options extends PrintOptions { /** * Valid 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 printing * * @since 7.0.0 * @example 'base64-encoded-pdf-data' */ data: string;
/** * MIME 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 formats * * @since 7.0.0 * @example 'application/pdf' * @example 'image/jpeg' */ mimeType: string;}PrintFileOptions
Bagian berjudul “PrintFileOptions”Opsi untuk mencetak file dari penyimpanan perangkat.
export interface PrintFileOptions extends PrintOptions { /** * Path 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) * * @since 7.0.0 * @platform ios Supports file:// paths and relative paths * @platform android Supports content:// URIs and file:// paths * @example 'content://com.android.providers.downloads.documents/document/123' * @example 'file:///var/mobile/Containers/Data/Application/.../Documents/document.pdf' * @example 'file:///storage/emulated/0/Download/receipt.pdf' */ path: string;
/** * MIME 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 images * * @since 7.0.0 * @default Undefined (auto-detected from file extension) * @platform android Used for content:// URIs and file type detection * @platform ios Ignored (auto-detected) * @example 'application/pdf' * @example 'image/jpeg' */ mimeType?: string;}PrintHtmlOptions
Bagian berjudul “PrintHtmlOptions”Opsi untuk mencetak konten HTML.
export interface PrintHtmlOptions extends PrintOptions { /** * HTML content to print. * * **Content Requirements:** * - Should be a complete HTML document with `<html>`, `<head>`, and `<body>` tags * - Can include inline CSS styles or `<style>` 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: `<meta charset="UTF-8">` * * @since 7.0.0 * @example '<html><body><h1>Hello World</h1><p>This is a test document.</p></body></html>' * @example * ```typescript * const htmlWithStyles = ` * <html> * <head> * <meta charset="UTF-8"> * <style> * @media print { * body { font-family: Arial, sans-serif; font-size: 12pt; } * .no-print { display: none; } * h1 { color: #333; page-break-before: always; } * } * </style> * </head> * <body> * <h1>Invoice #12345</h1> * <p>Amount: $299.99</p> * </body> * </html> * `; * ``` */ html: string;}Invoice #12345
*Amount: $299.99
* *