mob_print
System PDF printing for Mob apps — hand a PDF file on disk to the platform's native print dialog.
- Android:
PrintManager.print/3with a passthroughPrintDocumentAdapterthat streams the PDF unchanged. - iOS:
UIPrintInteractionControllerwith the PDF file URL asprintingItem.
Usage
# Job name defaults to the filename without extension ("invoice"):
MobPrint.pdf("/path/to/invoice.pdf")
# Or set an explicit job title for the system print UI / spooler:
MobPrint.pdf("/path/to/invoice.pdf", title: "Invoice #1042")
:ok means the system print dialog was requested — not that the user tapped
Print or that pages came out of a printer. No completion message is sent.
Options
:title(t:String.t/0) — job name shown in the system print UI and spooler. Defaults toPath.rootname(Path.basename(path)). RaisesArgumentErrorif not a binary; unknown options raiseArgumentError.
Return values
| Return value | Meaning |
|---|---|
:ok |
System print UI was requested. Does not mean the job finished printing. |
{:error, :not_found} |
path does not exist, is not a regular file, or cannot be read. |
{:error, :no_activity} |
App is not foregrounded, or no active Activity / foreground window is available. |
{:error, :no_app} |
Android: no print spooler / handler activity could be started for the print action. |
{:error, :failed} |
Platform refused to print the file (e.g. unreadable/invalid PDF or UI error). |
{:error, :unsupported} |
Printing is unavailable on this platform/device (host build, no print service, etc.). |
Install
# mix.exs
{:mob_print, "~> 0.1"}
# mob.exs
config :mob, :plugins, [:mob_print]
config :mob, :trusted_plugins, %{mob_print: "ed25519:<fingerprint>"}
mix mob.plugin.trust mob_print records the fingerprint, then
mix mob.deploy --native.
Platform notes
- Android (
PrintManager+ passthroughPrintDocumentAdapter): streams the PDF file byte-for-byte in 16 KiB chunks to the print framework and declaresPrintDocumentInfo.PAGE_COUNT_UNKNOWNso the system print preview and spooler inspect the PDF directly. NoFileProvideror extra manifest entries are needed. - iOS (
UIPrintInteractionController): passes the PDF as afile://NSURLviaprintingItem(UIPrintInfoOutputGeneral). On iPad (UIUserInterfaceIdiomPad), presents as a popover viapresentFromRect:inView:animated:completionHandler:centred on the top presented view controller's view; on iPhone, presents viapresentAnimated:completionHandler:. - No permissions: neither Android nor iOS requires any runtime or manifest permissions to open the system print dialog.
Limitations
- No completion callback: the call returns
:okonce the dialog has been requested; it does not report whether the user printed or cancelled. - PDF only: passes an existing PDF file through byte-for-byte. Generate or
render the PDF before calling
MobPrint.pdf/2. - Foreground only: must be called while the app is in the foreground (the call blocks a dirty IO scheduler for up to ~2 seconds while hand-off runs on the UI thread).
License
MIT.