PdfViewer
A lightweight Android PDF viewer library powered by Mozilla's PDF.js, offering seamless PDF rendering and interactive features. Supports both Jetpack Compose and Xml.
Install / Use
/learn @Bhuvaneshw/PdfViewerREADME
PdfViewer
A lightweight Android PDF Viewer Library powered by Mozilla's PDF.js, offering seamless PDF rendering and interactive features. Supports both Jetpack Compose and XML.
📢 Any iOS dev interested in CMP? — check this.
Screenshots
<img src="images/screenshot-1.png" width="190" alt="ScreenShot1"/> <img src="images/screenshot-2.png" width="190" alt="ScreenShot2"/> <img src="images/screenshot-3.png" width="190" alt="ScreenShot3"/> <img src="images/screenshot-4.png" width="190" alt="ScreenShot4"/> <img src="images/screenshot-5.png" width="190" alt="ScreenShot5"/>
PdfVision
Check out the complete application developed based on PdfViewer library.
<a href="https://play.google.com/store/apps/details?id=com.jetbotlabs.pdfvision"> <img src="images/GetItOnGooglePlay_Badge_Web_color_English.svg" alt="GetItOnGooglePlay_Badge_Web_color_English.svg" height="48px"/> </a>Demo
You can download apk from here
Docs
Contents
- Setup<br> 1.1. Setup - Kotlin DSL<br> 1.2. Setup - Groovy DSL<br>
- Usage<br> 2.1. Jetpack Compose PdfViewer<br> 2.2. XML PdfViewer<br> 2.3. More Examples<br>
- See also<br>
- Public Members<br>
- License
- External Libraries used
- Contributions
1. Setup
1.1. Kotlin DSL
<details open> <summary>View Kotlin DSL Setup</summary>Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle.kts or settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven("https://jitpack.io")
}
}
Step 2. Add the dependency
dependencies {
implementation("com.github.bhuvaneshw.pdfviewer:$module:$version")
}
Replace <b>$module</b> with <b>compose</b>, <b>compose-ui</b>, <b>core</b> or <b>ui</b>
Replace <b>$version</b> with latest version<br/>
Latest version:
Compose PDF Viewer (Core only)
Minimal setup for rendering PDFs using Jetpack Compose.
implementation("com.github.bhuvaneshw.pdfviewer:compose:1.0.0")
Compose PDF Viewer with UI Components
Enhanced Compose viewer setup including PdfViewerContainer, PdfToolBar, and PdfScrollBar.
implementation("com.github.bhuvaneshw.pdfviewer:compose:1.0.0")
implementation("com.github.bhuvaneshw.pdfviewer:compose-ui:1.0.0")
XML PDF Viewer (Core only)
Use the minimal setup for rendering PDFs.
implementation("com.github.bhuvaneshw.pdfviewer:core:1.0.0")
XML PDF Viewer with UI Components
Includes PdfViewerContainer, PdfToolBar, and PdfScrollBar for a complete viewing experience.
implementation("com.github.bhuvaneshw.pdfviewer:core:1.0.0")
implementation("com.github.bhuvaneshw.pdfviewer:ui:1.0.0")
</details>
</details>
<br/>
<br/>[!NOTE]
If you are upgrading to v1.1.0 see Migration
1.2. Groovy DSL
<details> <summary>View Groovy DSL setup</summary>Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle or settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.bhuvaneshw.pdfviewer:$module:$version'
}
</details>
2. Usage
2.1 Jetpack Compose PdfViewer
Include compose dependency
val pdfState = rememberPdfState(source = "source")
PdfViewer(
pdfState = pdfState,
modifier = Modifier,
containerColor = Color.Transparent,
onReady = {
// Optional work
}
)
source (string) can be
- Asset Path, like "asset://sample.pdf" or "file:///android_asset/sample.pdf"
- Android Uri, like uri starting with "content://" from Document Picker
- Network url, like "https://example.com/sample.pdf"
- Direct file path like "/data/data/<YourPackageName>/files/sample.pdf" or ~~"file:///sdcard/Downloads/sample.pdf"~~ (Not Recommended for accessing file from External Storage)
2.2 XML PdfViewer
Include PdfViewer in your xml
<com.bhuvaneshw.pdf.PdfViewer
android:id="@+id/pdf_viewer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/md_theme_primaryContainer"
app:containerBackgroundColor="@android:color/transparent" />
Then call load function
// Kotlin
pdfViewer.onReady {
load("source")
}
// Java
PdfUtil.onReady(pdfViewer, () -> {
pdfViewer.load("source");
});
[!WARNING] You should not access below members before the PdfViewer is initialized!
- PdfViewer.load()
- PdfViewer.loadFromAsset()
- PdfViewer.loadFromFileUri()
- PdfViewer.loadFromUrl()
- PdfViewer.ui
- PdfViewer.findController
- PdfViewer.pageScrollMode
- PdfViewer.pageSpreadMode
- PdfViewer.cursorToolMode
- PdfViewer.pageRotation
- PdfViewer.doubleClickThreshold
- PdfViewer.longClickThreshold
- PdfViewer.snapPage
- PdfViewer.pageAlignMode
- PdfViewer.singlePageArrangement
- PdfViewer.scrollSpeedLimit
- PdfEditor.undo()
- PdfEditor.redo()
- All setters in PdfEditor
2.3 More Examples
- For Jetpack Compose examples see Jetpack Compose Examples
- For XML examples see XML Examples
3. See also
[!NOTE] ComposePdfViewerActivity.kt<br> PdfViewerActivity.kt<br> ExtendedToolBar.kt<br>
4. Public Members
<details> <summary>View Public Members</summary>isInitialized: Boolean
Indicates whether the PDF viewer has been initialized.
currentUrl: String?
The current URL of the loaded PDF document.
currentPage: Int
The current page number of the PDF document.
pagesCount: Int
The total number of pages in the currently loaded PDF document.
currentPageScale: Float
The scale factor of the current page (zoom level).
currentPageScaleValue: String
The current scale value of the PDF page (e.g., page-fit, auto).
properties: PdfDocumentProperties?
The properties of the currently loaded PDF document, such as title, author, etc.
ui: UiSettings
Returns the UiSettings for the PDF viewer. Provides settings related to the UI provided by Mozilla's PDF.js.
findController: FindController
Returns the FindController for the PDF viewer. Provides functionality for finding text in the PDF.
pageScrollMode: PageScrollMode
Defines the page scroll mode (e.g., vertical, horizontal, wrapped).
pageSpreadMode: PageSpreadMode
Defines the page spread mode (e.g., none, odd, even).
cursorToolMode: CursorToolMode
Defines the cursor tool mode (e.g., text select, hand tool).
load(url: String, originalUrl: String = url)
Loads a PDF file from the specified url. The originalUrl parameter is optional and defaults to the url.
onReady(onReady: PdfOnReadyListener)
Registers a listener that gets called when the PDF viewer is initialized and ready.
addListener(listener: PdfListener)
Adds a listener to be notified of PDF events (e.g., page load).
removeListener(listener: PdfListener) and removeListener(listener: PdfOnReadyListener)
Removes a previously added listener.
goToPage(pageNumber: Int)
Navigates to the specified page number in the PDF.
scrollToRatio(ratio: Float)
Scrolls the viewer to a specific ratio (0f - 1f) (calculated to offset).
scrollTo(offset: Int)
Scrolls the viewer to the specified offset.
goToNextPage()
Navigates to the next page in the PDF.
goToPreviousPage()
Navigates to the previous page in the PDF.
goToFirstPage()
Navigates to the first page in the PDF.
goToLastPage()
Navigates to the last page in the PDF.
scalePageTo(scale: Float)
Zooms the current page to the specified scale factor.
zoomIn()
Zooms in on the current page.
zoomOut()
Zooms out on the current page.
zoomTo(zoom: Zoom)
Zooms to a specified zoom mode (e.g., PAGE_FIT, PAGE_WIDTH).
downloadFile()
Initiates the download of the currently viewed PDF file.
printFile() - unstable
Prints the currently viewed PDF file.
startPresentationMode() - unstable
Starts presentation mode, which is typically used for viewing PDFs in full-screen mode.
rotateClockWise()
Rotates the PDF clockwise by 90 degrees.
rotateCounterClockWise()
Rotates the PDF counter-clockwise by 90 degrees.
showDocumentProperties()
Displays the properties of the current PDF document (e.g., title, author).
reInitialize()
Re-initializes the PDF viewer, reloading the webview.
setContainerBackgroundColor(color: Int)
Sets the background color of the PDF viewer container.
5. License
Copyright 2025 Bhuvaneshwaran
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
