8000 GitHub - Bhuvaneshw/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.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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.

License

Notifications You must be signed in to change notification settings

Bhuvaneshw/PdfViewer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Screenshots

ScreenShot1 ScreenShot2 ScreenShot3 ScreenShot4 ScreenShot5

Demo

You can download apk from here

Contents

  1. Setup
    1.1. Setup - Kotlin DSL
    1.2. Setup - Groovy DSL
  2. Usage
    2.1. Jetpack Compose PdfViewer
    2.2. XML PdfViewer
    2.3. More Examples
  3. See also
  4. Public Members
  5. License
  6. External Libraries used
  7. Contributions

1. Setup

1.1. Kotlin DSL

View Kotlin DSL Setup

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 $module with compose, compose-ui, core or ui Replace $version with latest version
Latest version:

View Module Usage Options

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")

Note

If you are upgrading to v1.1.0 see Migration


1.2. Groovy DSL

View Groovy DSL setup

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'
}

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

  1. Asset Path, like "asset://sample.pdf" or "file:///android_asset/sample.pdf"
  2. Android Uri, like uri starting with "content://" from Document Picker
  3. Network url, like "https://example.com/sample.pdf"
  4. Direct file path like "/data/data/<YourPackageName>/files/sample.pdf" or "file:///sdcard/Downloads/sample.pdf" (Not Recommended for accessing file from Internal 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!

  1. PdfViewer.load()
  2. PdfViewer.loadFromAsset()
  3. PdfViewer.loadFromFileUri()
  4. PdfViewer.loadFromUrl()
  5. PdfViewer.ui
  6. PdfViewer.findController
  7. PdfViewer.pageScrollMode
  8. PdfViewer.pageSpreadMode
  9. PdfViewer.cursorToolMode
  10. PdfViewer.pageRotation
  11. PdfViewer.doubleClickThreshold
  12. PdfViewer.longClickThreshold
  13. PdfViewer.snapPage
  14. PdfViewer.pageAlignMode
  15. PdfViewer.singlePageArrangement
  16. PdfViewer.scrollSpeedLimit

2.3 More Examples

  1. For Jetpack Compose examples see Jetpack Compose Examples
  2. For XML examples see XML Examples

3. See also

4. Public Members

View Public Members

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 7F74 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

Also see PDF.js 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, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

6. External Libraries used for demo app

  1. MohammedAlaaMorsi/RangeSeekBar
  2. jaredrummler/ColorPicker
  3. mhssn95/compose-color-picker

7. Contributions

Contributions are welcome! If you have ideas for improvements, bug fixes, or new features, please feel free to:

  1. Fork the repository.
  2. Create a new branch for your changes.
  3. Make your changes and commit them.
  4. Push to the branch.
  5. Open a pull request.

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published
0