This library wraps libpdfium (the Chromium PDF engine). The pre-compiled pdfium files are taken from here and bundled into this library.
- Android
- JVM Desktop
- Linux x64, ARM64
- Windows X64
- MacOS X64, ARM64
- iOS
- x64 (simulator)
- arm64
- arm64Simulator
implementation("com.dshatz.pdfmp:pdfmp-compose:1.0.9")pdfmp = { module = "com.dshatz.pdfmp:pdfmp-compose", version = "1.0.9" }val pdf =PdfSource.PdfBytes(byteArray)val pdf =PdfSource.PdfPath(Path("~/Download/sample.pdf")You can use this helper.
val bytes:PdfBytes? by asyncPdfResource { Res.readBytes("files/doc.pdf")
}To create a PdfState, you need only an instance of PdfSource.
Additionally, you can pass some optional parameters:
pageRange: IntRange- range of pages to include. Zero-indexed, inclusive range.pageSpacing: Dp- spacing between pages, in DP.
@Composable
funShowDoc(state:PdfState) {
val state = rememberPdfState(pdf, pageSpacing =100.dp)
PdfView(state, Modifier.fillMaxSize())
}Use val displayState by state.displayState to get the document state.
sealedclassDisplayState {
data objectInitializing: DisplayState()
data objectActive: DisplayState()
data classError(valerror:Throwable): DisplayState()
}You can use this to display a loading indicator or an error dialog.
You can read and change various view parameters.
val layoutInfo by state.layoutInfo()
// It will be null if the document did not load yet.
layoutInfo?.apply {
// Below is some of the available stateval visiblePages:State<List<VisiblePageInfo>>
val mostVisiblePage:State<VisiblePageInfo?>
val totalPages:State<Int>
val pageRange:State<IntRange>
val documentHeight:State<Float>
// Scroll relatedvar offsetY:FloatfunscrollTo(pageIdx:Int)
suspendfunanimateScrollTo(pageIdx:Int)
// Zoom relatedval zoom:State<Float>
funsetZoom(newZoom:Float)
suspendfunanimateSetZoom(newZoom:Float)
}| Feature | Supported | Notes |
|---|---|---|
| Loading from bytes | ✅ | |
| Loading from path | ✅ | May not work on Android, depending on permissions and path. Please report. |
| Loading from compose resources | ❌ | Does not work directly because native code does not have access to it. See above for how to do it. |
| Zooming and scrolling | ✅ | Loading the high-res image is debounced by 100ms. On desktop, horizontal scrolling can only be done with Shift+Vertical scroll. |
| Filtering pages | ✅ | Pass page range to rememberPdfState |