Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# @rdlabo/capacitor-printer

printer plugin for capacitor
<!-- rdlabo-docs-omit -->
[![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)
<!-- /rdlabo-docs-omit -->

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.

<!-- rdlabo-docs-omit -->
**Full documentation:** [https://docs.rdlabo.dev/projects/capacitor-printer](https://docs.rdlabo.dev/projects/capacitor-printer)
<!-- /rdlabo-docs-omit -->

## Install

Expand All @@ -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.

<!-- rdlabo-docs-omit -->
### 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' });
};
```

<!-- /rdlabo-docs-omit -->

## 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

<docgen-index>
Expand Down Expand Up @@ -84,3 +142,9 @@ Present the printing user interface to print the web view content.
<code><a href="#printoptions">PrintOptions</a></code>

</docgen-api>

<!-- rdlabo-docs-omit -->
## License

This project is licensed under the [MIT License](./LICENSE).
<!-- /rdlabo-docs-omit -->
17 changes: 17 additions & 0 deletions docs/pdf.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 11 additions & 0 deletions docs/web.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"android/src/main/",
"android/build.gradle",
"dist/",
"docs/",
"ios/Sources",
"ios/Tests",
"Package.swift",
Expand Down