

Even though there's very little code involved, this already works really well. This covers the basics but is pretty promising. If you're using coroutines, this would be as simple as turning what we have so far into a suspend function and wrapping it in withContext( Dispatchers.IO), but feel free to achieve that with your preferred tool. PdfRenderer's docs aren't explicit about this, so I'm relying on the platform's best practices - use your best judgement and benchmark your app. It is therefore important to manage threading appropriately when working with bitmaps. Loading bitmaps on the UI thread can degrade your app’s performance, causing slow responsiveness or even ANR messages. We've been neglecting threading so far, but whenever we're dealing with files and Bitmaps it's usually a good idea to offload the work to a background thread.
#Pdf printer for android pdf
Let's assume we have a PDF file available in a given filePath. TL DR:Ĭheck this gist with the final version of the most relevant snippets mentioned in the article. The documentation around it is good but brief, and there aren't many examples around, so the goal here is to walk through the process of rendering a PDF as a RecyclerView so you can scroll through the pages as you'd expect to whenever interacting with a PDF file in a mobile device.
#Pdf printer for android apk
Starting on API 21, we can use PdfRenderer as an abstraction on top of PDFium - same as AndroidPdfViewer but without the APK size hit or the need of any extra library at all.


There are also many paid options out there, but I'm here to talk about the great PdfRenderer. A few years ago, the popular AndroidPdfViewer took the stage and things got better, but we still had to pay an expensive price: an extra 15 MB in our APK size (which can be reduced, though, especially today with app bundles). Browsers (including WebViews) struggle to render PDFs there, and the task is usually delegated to a separate specialized app.įor a long time there was no easy way to render a PDF inside our apps and we had to rely on questionable solutions. Surprisingly, that's not exactly the case on Android. Developers or not, most of us are familiar with PDFs and are used to work with them all the time, so rendering PDFs may seem like an ordinary task that any platform should be able to perform with minimal effort.
