Printing
Printing is the system's print dialog and an HTML-to-PDF renderer, bound to expo-print.
import { Printing } from '@ng-native/expo';
inject(Printing)Platforms. Available on macOS, Linux (no HTML), iPadOS, iPhone and Android. Absent on Web host, where available() is false.
Install
npx expo install expo-printThe smallest useful example
import { Component, inject } from '@angular/core';
import { Printing } from '@ng-native/expo/printing';
@Component({ selector: 'app-invoice', template: '' })
export class Invoice {
private readonly printing = inject(Printing);
protected async print(html: string): Promise<void> {
await this.printing.print({ html });
}
protected async exportPdf(html: string): Promise<string | null> {
return this.printing.printToPdf(html);
}
}What it does
print(options)- shows the print dialog forhtml, or for the file aturi, such as a PDF, and resolves once it closes, printed or not. On iOS,printer(fromselectPrinter()) prints to that printer without the dialog.orientationisportraitorlandscape.printToPdf(html, page?)- renders the HTML to a PDF file in the cache and resolves to its uri, to share or save where the user chooses.pagetakes thewidthandheightin points.selectPrinter()- iOS's printer picker. Resolves to the printer chosen, with itsnameandurl, ornullwhen the picker is dismissed and on Android and macOS, which have none.
On macOS the desktop host prints with AppKit: the print dialog opens as a sheet on the key window,
and a printer passed to print() is ignored. printToPdf() breaks pages where the HTML's CSS
asks (break-before: page, break-after: page).
On Linux the GTK host prints a window or an image through GTK's print dialog, which is the
desktop's Print portal inside a Flatpak, and a PDF through the Print portal, which takes the file as
it is; with no portal, GTK's own dialog sends the PDF to a printer that takes PDF. HTML has no
engine to lay it out on Linux, so print({ html }) and printToPdf() reject there.
Printing a window on macOS and Linux
print({ window }) prints what a window shows, drawn as it is on screen and scaled down to the
paper, with the print dialog as a sheet on that window. A web view prints its page as WebKit's
snapshot of it. A video, the camera preview and anything drawn with Metal print blank, since they
draw outside the window's own drawing and capturing them would need the Screen Recording
permission. On Linux the window is drawn onto one page, scaled down to fit it. It takes an
AppWindow, from AppWindows.open() or CURRENT_WINDOW:
import { Component, inject } from '@angular/core';
import { CURRENT_WINDOW } from '@ng-native/device';
import { Printing } from '@ng-native/expo/printing';
@Component({ selector: 'app-report', template: '' })
export class Report {
private readonly window = inject(CURRENT_WINDOW);
private readonly printing = inject(Printing);
protected async printWindow(): Promise<void> {
if (this.window) await this.printing.print({ window: this.window });
}
}On iOS, Android and the web a window prints nothing and the call resolves.
The default menu bar has File > Print... (Command-P, and Ctrl+P on Linux), which prints the key
window this way. An app
that prints something else, such as a document, gives its own item the print role and a handler
in its <menu-bar>; that item takes Print...'s place and Command-P, and the host leaves printing
to it.
Without the module
On iOS, Android and macOS, a missing expo-print - never installed, or installed without the app
being rebuilt since - leaves the service made, so injecting it does not throw. A call throws a
MissingModuleError; see
Using a module.
On the web, and in a test that provides no fake,
available() is false, print() does nothing, and printToPdf() and selectPrinter() resolve
to null.
Reference
Printingservice@ng-native/expoimport { Printing } from '@ng-native/expo';Methods
print(options: PrintOptions): Promise<void>Shows the print dialog for HTML, a file, or on macOS and Linux a window's content. Resolves once it closes, printed or not. A window prints nothing on a phone or the web.
printToPdf(html: string, page: { width?: number; height?: number } = {}): Promise<string | null> Renders HTML to a PDF file in the cache and answers its uri, to share or save. width and height are the page size in points, US Letter by default. Null where the service is absent, and where the module made no file.
selectPrinter(): Promise<Printer | null>iOS's printer picker: answers the printer chosen, or null when it is dismissed and on an OS with no picker. Any other failure is rethrown.