ApplePdfRasterizer

Headless rendering on Apple platforms (iOS, macOS, tvOS). Produces a PNG-encoded NSData sized by PdfPage.rasterGeometry (rotation, crop box and /UserUnit all included), at scale device pixels per point. Pure CoreGraphics + ImageIO, no UIKit/AppKit, no Compose dependency.

Typical uses:

  • Pre-rendering thumbnails on disk (writeToFile:atomically:)

  • Generating sharing previews / extension thumbnails

  • CI snapshots of PDF rendering

For drawing into a UIView's drawRect: (or an NSView) directly, instantiate CoreGraphicsCanvas over the current CGContext instead. The rasterizer is for off-screen output.

Functions

Link copied to clipboard
fun renderToPngData(page: PdfPage, scale: Double = 1.0, backgroundR: Double = 1.0, backgroundG: Double = 1.0, backgroundB: Double = 1.0, backgroundA: Double = 1.0): NSData?

Render a page into a PNG-encoded NSData. Returns null if the underlying CGBitmapContext or PNG encoder can't be created (very rare; would indicate a system-level resource failure).

fun renderToPngData(page: PdfPage, scale: Double = 1.0, backgroundR: Double = 1.0, backgroundG: Double = 1.0, backgroundB: Double = 1.0, backgroundA: Double = 1.0, maxPixels: Long): NSData?

renderToPngData with an explicit allocation ceiling.