I am trying to display a PDF document with PdfViewerFragment from the library androidx.pdf:pdf-viewer:1.0.0-alpha10 inside a Jetpack Compose UI.
I’m using AndroidView to host the fragment.
The problem: Although the content:// URI (provided by a FileProvider) looks correct and the PDF file itself is valid (can be opened with other viewers), and although the AndroidView container (with a test background color) is visible and occupies the correct space, the PDF itself is not displayed in the PdfViewerFragment.
There are no errors or exceptions in Logcat after the fragment is added.
What I’ve already checked/tried:
URI validity: The content:// URI is generated by a FileProvider for a file in the app’s cache directory. The file exists, is not empty, and can be downloaded/opened from the emulator.
FileProvider configuration: Declared properly in AndroidManifest.xml, authorities match.
Container visibility: The AndroidView hosting the FragmentContainerView for PdfViewerFragment has a background color. This background is visible and fills the expected area.
Logcat output:
PdfFragmentScreen D LaunchedEffect triggered. pdfUri:content://com.xxx.fileprovider/my_cache_files/xxx.pdf
PdfFragmentScreen D Attempting to copy PDF to cache. Temp file path: /data/user/0/com.xxx/cache/temp_pdf_viewer.pdf
PdfFragmentScreen D Deleting existing temp file.
PdfFragmentScreen D Successfully opened InputStream for pdfUri.
PdfFragmentScreen D Successfully copied data to temp file.
PdfFragmentScreen I Successfully created CONTENT URI: content://com.xxx.fileprovider/my_cache_files/temp_pdf_viewer.pdf
PdfFragmentScreen D Setting isLoading to false. fileUriForViewer: content://com.xxx.fileprovider/my_cache_files/temp_pdf_viewer.pdf
MyPdfViewer D Composing with URI: content://com.xxx.fileprovider/my_cache_files/temp_pdf_viewer.pdf, Modifier: androidx.compose.foundation.layout.FillElement@d444f175
MyPdfViewer D Factory called. Creating FragmentContainerView with id: 1
MyPdfViewer D Update called. URI: content://com.xxx/my_cache_files/temp_pdf_viewer.pdf, FragmentContainerView ID: 1
MyPdfViewer I Fragment with tag PdfViewerFragmentTag not found. Adding new PdfViewerFragment.`
Code:
@SuppressLint("ContextCastToActivity")
@Composable
fun MyPdfViewer(
uri: Uri?,
modifier: Modifier = Modifier
) {
val activity = LocalContext.current as FragmentActivity
val themeContext = ContextThemeWrapper(activity, R.style.PdfViewerTheme)
val fragmentTag = remember { "PdfViewerFragmentTag" }
Log.d("MyPdfViewer", "Composing with URI: $uri, Modifier: $modifier")
AndroidView(
modifier = modifier.then(Modifier.background(Color.Blue)),
factory = {
Log.d("MyPdfViewer", "Factory called. Creating FragmentContainerView with id: $containerId")
FragmentContainerView(themeContext).apply {
id = containerId
}
},
update = { fragmentContainerView ->
Log.d("MyPdfViewer", "Update called. URI: $uri, FragmentContainerView ID: ${fragmentContainerView.id}")
if (uri != null) {
if (activity.supportFragmentManager.findFragmentByTag(fragmentTag) == null) {
Log.i("MyPdfViewer", "Fragment with tag $fragmentTag not found. Adding new PdfViewerFragment.")
val newFragment = PdfViewerFragment()
newFragment.arguments = bundleOf("uri" to uri)
activity.supportFragmentManager.commit {
replace(
fragmentContainerView.id,
newFragment,
fragmentTag
)
}
} else {
Log.d("MyPdfViewer", "Fragment with tag $fragmentTag already exists.")
}
} else {
Log.w("MyPdfViewer", "Update called but URI is null. Not doing anything.")
}
}
)
}
dependencies:
def composeBom = platform('androidx.compose:compose-bom:2025.09.00')
// pdf viewer
implementation "androidx.pdf:pdf-viewer:1.0.0-alpha10"
implementation "androidx.pdf:pdf-compose:1.0.0-alpha10"
implementation "androidx.pdf:pdf-viewer-fragment:1.0.0-alpha10"
AndroidFragmentComposable, maybe it will work with that? stackoverflow.com/questions/79243049/…