Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork231
A Lightweight PDF Viewer Android library which only occupies around 80kb while most of the Pdf viewer occupies up to 16MB space.
License
afreakyelf/Pdf-Viewer
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A Simple PDF Viewer library which only occupies around80kb while most of the Pdf viewer occupies upto16MB space.
[New] Join our Discord Communityhere!
Hello Developers! We're thrilled to share some significant enhancements we've made to our PDF viewer library. We've fine-tuned several aspects to enhance your experience and ensure top-notch performance and security. Here's what's new:
- Step into the future with Jetpack Compose compatibility. Integrating our PDF viewer in Compose projects is now effortless, thanks to the PdfRendererViewCompose composable function.
- We've optimized performance to handle PDFs more efficiently, ensuring swift and smooth operations, even with large documents.
- We have made it better and smooth with how local files are handled now, with latest permission policies.
- Our library now smartly preserves your page position during orientation changes, ensuring uninterrupted reading sessions.
- Security just got stronger. We've revamped our file path handling to provide robust protection against directory traversal attacks, keeping your data safer than ever.
- Experience efficiency at its best! Our refined caching strategy smartly manages storage, retaining only the most recent PDF file to optimize performance and space usage.
- Privacy matters. Our new screenshot-blocking feature enhances data confidentiality in your app, keeping sensitive information secure from prying eyes.
- Your design, your rules. Enjoy complete freedom in customizing the PDF viewer's interface, ensuring a perfect match with your app's style and theme. Render the view directly in your screen now.
- Seamless aesthetics, no matter the theme. Our library now gracefully integrates with 'NoActionBar' themes, ensuring a cohesive and appealing user interface.
Stay tuned as we continue to innovate and improve. Happy coding, and let's keep creating amazing experiences together!
We have migrated our library to Maven Central for easier integration and better reliability. To use the Pdf Viewer library in your project, add the following dependency to yourbuild.gradle file:
dependencies {// Replace 'latest-version' with the actual latest version number implementation'io.github.afreakyelf:Pdf-Viewer:latest-version'}
dependencies {// Replace 'latest-version' with the actual latest version number implementation("io.github.afreakyelf:Pdf-Viewer:latest-version")}
- Minimum SDK version: 21
- Compile & Target SDK version: 35 (updated since version 2.2.0)
Now you have integrated the library in your project buthow do you use it? Well it's really easy. Just launch the intent with in following way: (Refer toMainActivity.kt for more details.)
Ensure the library is included in your project's dependencies.
To display a PDF from a URL, use the following code:
/* Parameters:- context: The context of your activity.- pdfUrl: URL of the PDF to be displayed.- pdfTitle: Title of the PDF document.- saveTo: Determines how to handle saving the PDF (e.g., ASK_EVERYTIME prompts the user each time).- enableDownload: Enables downloading of the PDF.*/PdfViewerActivity.launchPdfFromUrl( context=this, pdfUrl="your_pdf_url_here", pdfTitle="PDF Title", saveTo= saveTo.ASK_EVERYTIME, enableDownload=true)
To open a PDF stored in local storage:
/* Parameters:- path: File path or URI of the local PDF.- fromAssets: Set to false when loading from local storage. // FALSE by default*/PdfViewerActivity.launchPdfFromPath( context=this, path="your_file_path_or_uri_here", pdfTitle="Title", saveTo= saveTo.ASK_EVERYTIME, fromAssets=false)
To open a PDF from the app's assets folder:
/* Parameters:- path: File path or URI of the local PDF.- fromAssets: Set to true when loading from assets.*/PdfViewerActivity.launchPdfFromPath( context=this, path="file_name_in_assets", pdfTitle="Title", saveTo= saveTo.ASK_EVERYTIME, fromAssets=true)
Load a PDF directly into a view:
Add PDF render view in your layout file
<com.rajat.pdfviewer.PdfRendererViewandroid:id="@+id/pdfView"android:layout_width="match_parent"android:layout_height="match_parent"app:pdfView_divider="@drawable/pdf_viewer_divider"app:pdfView_showDivider="false" />
and in your kotlin file
binding.pdfView.initWithUrl( url="your_pdf_url_here", lifecycleCoroutineScope= lifecycleScope, lifecycle= lifecycle)
For Jetpack Compose, utilize PdfRendererViewCompose:
PdfRendererViewCompose( source=PdfSource.Remote("your_pdf_url_here"), lifecycleOwner=LocalLifecycleOwner.current, modifier=Modifier, headers=HeaderData(mapOf("Authorization" to"123456789")), statusCallBack=object:PdfRendererView.StatusCallBack {// Override functions here }, zoomListener=object:PdfRendererView.ZoomListener {// Override functions hereoverridefunonZoomChanged(isZoomedIn:Boolean,scale:Float) {TODO("Not yet implemented") } })
That's all you need to integrate PDF rendering in your Compose application.
You can monitor download progress, rendering success, page changes, and zoom state using the following callbacks:
Use thestatusListener to get callbacks on PDF lifecycle events:
binding.pdfView.statusListener=object:PdfRendererView.StatusCallBack {overridefunonPdfLoadStart() {Log.i("PDF Status","Loading started") }overridefunonPdfLoadProgress(progress:Int,downloadedBytes:Long,totalBytes:Long?) {Log.i("PDF Status","Download progress:$progress%") }overridefunonPdfLoadSuccess(absolutePath:String) {Log.i("PDF Status","Load successful:$absolutePath") }overridefunonError(error:Throwable) {Log.e("PDF Status","Error loading PDF:${error.message}") }overridefunonPageChanged(currentPage:Int,totalPage:Int) {Log.i("PDF Status","Page changed:$currentPage /$totalPage") }overridefunonPdfRenderStart() {Log.i("PDF Status","Render started") }overridefunonPdfRenderSuccess() {Log.i("PDF Status","Render successful") binding.pdfView.jumpToPage($number)// Recommend to use `jumpToPage` inside `onPdfRenderSuccess` }}
You can also monitor when the user zooms in or out usingzoomListener:
binding.pdfView.zoomListener=object:PdfRendererView.ZoomListener {overridefunonZoomChanged(isZoomedIn:Boolean,scale:Float) {Log.i("PDF Zoom","Zoomed in:$isZoomedIn, Scale:$scale") }}
You need to add the custom theme to styles.xml/themes.xml file and override the required attribute values.Parent theme can be eitherTheme.PdfView.Light orTheme.PdfView.Dark or the one with no actionbar from the application.Note: If parent is not one of the themes from this library, all of the pdfView attributes should be added to that theme.
<style name="Theme.PdfView.SelectedTheme" parent="@style/Theme.PdfView.Light"> <item name="pdfView_backIcon">@drawable/ic_arrow_back</item> <item name="pdfView_showToolbar">true</item> <item name="pdfView_disableScreenshots">true</item> ...</style>You need to add the custom layout to pdf_view_page_no.xml file and override the required attributevalues.
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pageNo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="18dp" android:background="#9C27B0" android:paddingStart="12dp" android:paddingTop="4dp" android:paddingEnd="12dp" android:paddingBottom="4dp" android:textColor="#ffffff" android:textSize="16sp" android:visibility="gone" />You need to add the custom string to strings.xml file and override the required strings.xml values.
Default:
<string name="pdfView_page_no">%1$s of %2$s</string>Custom:
<string name="pdfView_page_no" >%1$s / %2$s</string>| Attribute Name | Type | Expected changes |
|---|---|---|
| pdfView_backIcon | drawable | Navigation icon |
| pdfView_downloadIcon | drawable | Download icon |
| pdfView_downloadIconTint | color | Download icon tint |
| pdfView_toolbarColor | color | Actionbar background color |
| pdfView_titleTextStyle | style | Actionbar title text appearance |
| pdfView_progressBar | style | Progress bar style |
👉Check out who's using Pdf-Viewer
Any contributions you make aregreatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/NewFeature) - Commit your Changes (
git commit -m 'Add some NewFeature') - Push to the Branch (
git push origin feature/NewFeature) - Open a Pull Request
If this library helps you save time during development, you can buy me a cup of coffee :)
Maintained byRajat Mittal
About
A Lightweight PDF Viewer Android library which only occupies around 80kb while most of the Pdf viewer occupies up to 16MB space.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.





