@gingersnapsoftware/capacitor-plugin-printer
v0.0.15
Published
Capacitor plugin for printing HTML, PDF, image, base64, and file-path content from iOS/Android apps.
Maintainers
Readme
Printer.print({ content: '<b>Lorem ipsum...</b>' })Versions
| Capacitor Version | Plugin Version | | ----------------- | -------------- | | v5.x | v0.0.3 | | v6.x | v0.0.4 – 0.0.11 | | v7.x | v0.0.13+ |
Supported Platforms
- Android 5.1+
- iOS 13+
Supported Content
- HTML (with inline CSS)
- Plain text
- Images (passed as HTML
<img>or as Base64 / file path) - Base64-encoded PDFs and images (auto-detected from
base64:prefix ordata:URI) - Local PDFs and images via a filesystem path (
contentType: 'path')
Installation
npm install @gingersnapsoftware/capacitor-plugin-printer
npx cap syncUsage
import { Printer } from '@gingersnapsoftware/capacitor-plugin-printer';
await Printer.print({
content: 'Lorem ipsum...',
name: 'lorem-filename',
orientation: 'landscape',
});Examples
Plain text
Printer.print({ content: 'Lorem ipsum...' });HTML
Printer.print({ content: '<h1>Lorem</h1>' });HTML with multiple elements
let body = '';
body += '<li style="color:green">Tea</li>';
body += '<li style="font-size:50px">Coffee</li>';
body += '<img src="https://picsum.photos/200">';
Printer.print({ content: body });Inline CSS
Printer.print({ content: '<b style="color:red">Lorem ipsum</b>' });Base64 PDF or image (auto-detected, no contentType required)
// "base64:" prefix
Printer.print({ content: 'base64:JVBERi0xLjQK...' });
// or a full data URI
Printer.print({ content: 'data:application/pdf;base64,JVBERi0xLjQK...' });Local file path (PDF or image) — useful when the file was just downloaded or generated via @capacitor/filesystem:
import { Filesystem, Directory } from '@capacitor/filesystem';
const writeResult = await Filesystem.writeFile({
path: 'ticket.pdf',
data: base64Pdf, // base64-encoded PDF bytes
directory: Directory.Cache,
});
// writeResult.uri is a "file://" URI; the plugin strips the prefix automatically
await Printer.print({
content: writeResult.uri,
name: 'ticket',
orientation: 'portrait',
contentType: 'path',
});Notes on contentType: 'path':
- Accepts both
file:///absolute/path/to/file.pdfand/absolute/path/to/file.pdf. - PDFs are printed directly. Images are wrapped into a single-page PDF on the fly.
- Unsupported file types reject the promise with a descriptive error.
Why prefer contentType: 'path' over Base64
Base64 embeds the entire document in the JavaScript string passed across the Capacitor bridge, which means the same bytes can briefly live in memory three times at once (the JS string, the decoded native buffer, and the print-system copy). On large or image-heavy PDFs this can spike RSS by tens of megabytes per print.
This matters more on recent iOS releases: the OS has become more aggressive about killing background or memory-pressured apps mid-print. A print that works in development can be silently terminated on a real device once total memory crosses the per-app limit. Symptoms are usually one of:
- The print sheet opens, then the app crashes or is forcibly backgrounded.
- The print job appears to send but never reaches the printer.
- An OOM jetsam log entry in
Console.appreferencing your app right afterPrinter.printwas called.
Using contentType: 'path' avoids this entirely: only the file path is sent across the bridge, the file is streamed by the OS printing service directly from disk, and your app's JS heap stays flat. For anything larger than a small text receipt — and for all PDFs produced by @capacitor/filesystem, downloads, or PDF generators — write the bytes to a file once and pass the path.
API
print(...)
print(printOptions: PrintOptions) => Promise<void>| Param | Type |
| ------------------ | ----------------------------------------------------- |
| printOptions | PrintOptions |
Interfaces
PrintOptions
| Prop | Type | Description | Default | Since |
| ----------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------- | ----- |
| content | string | Content to print. Interpreted according to {@link PrintOptions.contentType}. | | 0.0.1 |
| name | string | Name of the print job / document. | iOS=YourAppName/Android=Document+CurrentTimestamp | 0.0.1 |
| orientation | string | Orientation of the printing page. "portrait" or "landscape". | "portrait" | 0.0.1 |
| contentType | string | How to interpret {@link PrintOptions.content}. - "html" (default): treat content as raw HTML. - "path": treat content as a filesystem path (with or without a file:// prefix) pointing to a PDF or image to print. Note: base64-encoded content (base64:... or a data: URI) is detected automatically and does not require setting this. | "html" | 0.0.1 |
