diff --git a/README.md b/README.md index a3d522f..b7a8756 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,17 @@ # @rdlabo/capacitor-printer -printer plugin for capacitor + +[![npm version](https://badge.fury.io/js/@rdlabo%2Fcapacitor-printer.svg)](https://badge.fury.io/js/@rdlabo%2Fcapacitor-printer) +[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) + + +Print files or the current web view from a Capacitor app. + +This plugin wraps the native printing UI on iOS and Android. You can print a local file (for example, a PDF generated in your app) or the content of the current web view. + + +**Full documentation:** [https://docs.rdlabo.dev/projects/capacitor-printer](https://docs.rdlabo.dev/projects/capacitor-printer) + ## Install @@ -9,6 +20,53 @@ npm install @rdlabo/capacitor-printer npx cap sync ``` +## Usage + +See [PDF](./docs/pdf.md) to print a file and [Web](./docs/web.md) to print the current WebView. + + +### Print a file + +```ts +import { Printer } from '@rdlabo/capacitor-printer'; + +const printPdf = async (filePath: string) => { + try { + await Printer.printFile({ + path: filePath, + mimeType: 'application/pdf', + }); + } finally { + // The source file can be deleted once the promise settles. + } +}; +``` + +### Print the current web view + +```ts +import { Printer } from '@rdlabo/capacitor-printer'; + +const printPage = async () => { + await Printer.printWebView({ name: 'My Receipt' }); +}; +``` + + + +## When to use + +Use this plugin when your app needs to present the system print dialog, such as: + +- Printing a receipt or invoice as PDF. +- Printing a report generated in the app. +- Printing the contents of the current page. + +## Platform notes + +- **iOS and Android**: `printFile` and `printWebView` are both supported. +- **Web**: Not supported because browsers already provide `window.print()`. + ## API @@ -84,3 +142,9 @@ Present the printing user interface to print the web view content. PrintOptions + + +## License + +This project is licensed under the [MIT License](./LICENSE). + diff --git a/docs/pdf.md b/docs/pdf.md new file mode 100644 index 0000000..465678b --- /dev/null +++ b/docs/pdf.md @@ -0,0 +1,17 @@ +# PDF + +Present the system print UI for a PDF or other file. Only Android and iOS. Call this after [Installation](/docs/readme#installation). Print the current WebView with [Web](/docs/web). + +```ts +import { Printer } from '@rdlabo/capacitor-printer'; + +const filePath = '/path/to/document.pdf'; + +try { + await Printer.printFile({ path: filePath }); +} finally { + // The promise settles after the OS no longer needs the source file. +} +``` + +Android supports file paths, `file://` URLs, and `content://` URLs. iOS supports file paths and local `file://` URLs. `mimeType` is Android-only. Signatures are on the [API](/docs/api#printfile) page. diff --git a/docs/web.md b/docs/web.md new file mode 100644 index 0000000..c649279 --- /dev/null +++ b/docs/web.md @@ -0,0 +1,11 @@ +# Web + +Present the system print UI for the current WebView content. Only Android and iOS. Call this after [Installation](/docs/readme#installation). Print a PDF or other file with [PDF](/docs/pdf). + +```ts +import { Printer } from '@rdlabo/capacitor-printer'; + +await Printer.printWebView({ name: 'Document' }); +``` + +`name` is the print job name and defaults to `'Document'`. Signatures are on the [API](/docs/api#printwebview) page. diff --git a/package.json b/package.json index 345c5b3..8c29a12 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "android/src/main/", "android/build.gradle", "dist/", + "docs/", "ios/Sources", "ios/Tests", "Package.swift",