One Logo Pool Ball
wwwwwwwwwwwwwwwwwww

Print

Present the system print sheet for a local PDF

One.Print presents the system print sheet for a PDF already stored in the app’s readable local files. It prints; it does not generate PDF content or download remote URLs. Write a PDF with the file system or copy one in with the document picker first.

On iOS it presents UIPrintInteractionController on the main thread. On Android each page rasterizes through the platform PDF renderer at the job’s resolution into the system print sheet. On web availability reads false and printing rejects: window.print prints the current document, never this method’s PDF URI with a completion report.

Usage

import { One } from 'one'
async function printInvoice(fileUri: string) {
if (await One.Print.isAvailable()) {
const result = await One.Print.printPdf(fileUri, 'Invoice')
if (result.completed) {
console.log('print request completed')
}
}
}

The job name defaults to the PDF filename without its extension. No manifest setting is required on either platform.

How it works

printPdf needs a local file:// URI pointing at an existing, readable PDF with at least one page. Bad argument types and empty strings throw before reaching native code; native failures carry one code each for nonlocal URIs, missing files, invalid or empty PDFs, unavailable printing, missing presenters, and system print errors. One sheet runs at a time; a second request while one is open rejects as busy.

Tapping the system Cancel button resolves { completed: false }; a completed system request resolves { completed: true }. Completion reports the sheet’s request, not delivered paper. isAvailable reports whether the system can present printing at all: on iOS even with no printer selected, on Android false when no print service handles the job.

API reference

MethodsTypeDescription
isAvailable() => Promise<boolean>Whether the system can present printing.
() => Promise<boolean>Whether the system can present printing.
printPdf(fileUri: string, jobName?: string) => Promise<PrintResult>Print a local PDF. Job name defaults to the filename.
(fileUri: string, jobName?: string) => Promise<PrintResult>Print a local PDF. Job name defaults to the filename.
ResultTypeDescription
completedbooleanTrue when the system completed its request.
booleanTrue when the system completed its request.

Platform differences

iOSAndroidWeb
SheetSystem printSystem printNone
PagesNative PDFRasterizedRejects
AvailableIf printableIf servicedNever

Limits and gotchas

Examples

import { One } from 'one'
async function printFromUrl(url: string, jobName: string) {
const bytes = await (await fetch(url)).arrayBuffer()
const uri = `${One.FileSystem.getDirectories().cache}/invoice.pdf`
await One.FileSystem.writeFile(uri, toBase64(bytes), 'base64')
try {
if (!(await One.Print.isAvailable())) return false
const result = await One.Print.printPdf(uri, jobName)
return result.completed
} finally {
await One.FileSystem.delete(uri)
}
}
function toBase64(bytes: ArrayBuffer) {
return btoa(String.fromCharCode(...new Uint8Array(bytes)))
}

The download lands in cache as a real file, which is what the printer needs. The temp file is deleted after the sheet closes either way.

Offer printing only where it works

import { useEffect, useState } from 'react'
import { Button } from 'react-native'
import { One } from 'one'
function PrintButton({ fileUri }: { fileUri: string }) {
const [available, setAvailable] = useState(false)
useEffect(() => {
One.Print.isAvailable().then(setAvailable)
}, [])
if (!available) return null
async function print() {
const result = await One.Print.printPdf(fileUri)
console.log('completed:', result.completed)
}
return <Button title="Print" onPress={print} />
}

Availability gates the button, so web and printer-less devices never offer a dead action. The default job name comes from the filename.

Edit this page on GitHub.