Giter Club home page Giter Club logo

composepdfviewer's Introduction

ComposePdfViewer

License: GPL v3

A simple Jetpack Compose PDF viewer

Demo

Usage

PdfViewer(
    pdfResId = R.raw.demo,
    modifier = Modifier.fillMaxSize(),
    backgroundColor = Color(0xFF909090),
    pageColor = Color.White,
    listDirection = PdfListDirection.VERTICAL,
    arrangement = Arrangement.spacedBy(16.dp),
    fallbackWidget = {
        Icon(
            Icons.Rounded.Info,
            contentDescription = "Error Component"
        )
    },
    loadingListener = { isLoading, currentPage, maxPage ->
         // Observe loading changes
    }
)

// Or if you already have an InputStream
PdfViewer(
    pdfStream = inputStream,
    modifier = Modifier.fillMaxSize(),
    backgroundColor = Color(0xFF909090),
    pageColor = Color.White,
    listDirection = PdfListDirection.VERTICAL,
    arrangement = Arrangement.spacedBy(16.dp),
    fallbackWidget = {
        // Display Custom Widget if an error occurred while rendering the pdf file
    },
    loadingListener = { isLoading, currentPage, maxPage ->
         // Observe loading changes
    }
)

// Or if you want to implement your own page layout
PdfViewer(
   pdfStream = inputStream,
   modifier = Modifier.fillMaxSize(),
   backgroundColor = Color(0xFF909090),
   listDirection = PdfListDirection.VERTICAL,
   arrangement = Arrangement.spacedBy(16.dp),
    fallbackWidget = {
        // Display Custom Widget if an error occurred while rendering the pdf file
    },
   loadingListener = { isLoading, currentPage, maxPage ->
        // Observe loading changes
   },
   page = { lazyListState, imageBitmap ->
        // Implement your own custom page
   }
 )

Known problems

  • Zoom pan is not restricted (Any fix to that should go to ComposeZoomableImagePlus)
  • Load performance is slow (on average, the PDF Viewer loads about 4 pages a second on a Pixel 2 emulator)

Future changes (other than fixing the known problems)

  • Add ram caching for the pdf pages in case the PDF is not as heavy and doesn't need disk caching
  • Publish to Jitpack

composepdfviewer's People

Contributors

joaopegoraro avatar s-ka-alpha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

composepdfviewer's Issues

Corrupted Pdf File Exception

when trying to render a corrupted pdf file it throws an exception and crashes the app,
it would be a good to have a fallback composable to view instead, or another way to display an error instead of an app crashing exception
the thrown exception :
java.io.IOException: file not in PDF format or corrupted
at android.graphics.pdf.PdfRenderer.nativeCreate(Native Method)
at android.graphics.pdf.PdfRenderer.(PdfRenderer.java:171)
at xyz.joaophp.pdfviewer.PdfViewerKt$loadPdf$3.invokeSuspend(PdfViewer.kt:209)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [androidx.compose.ui.platform.MotionDurationScaleImpl@29e3fb3, androidx.compose.runtime.BroadcastFrameClock@c3bd970, StandaloneCoroutine{Cancelling}@e5be4e9, AndroidUiDispatcher@215a56e]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.