Improve PDF rendering in Android app using Pagination

Vibhanshu Sharma
Powerplay
Published in
3 min readOct 12, 2021

Recently at Powerplay, many users gave us feedback that their progress report was loading very slowly.

To give you all context, in the Powerplay app, a progress report is a pdf file that contains information about the user’s project.

So we decided to accelerate the rendering speed of PDF files in our app and we were able to decrease the rendering time of these files by almost 70 -80%.

In this blog, I will tell you how we were able to achieve this.

First I will tell you about the mistakes that we were doing earlier and then I will tell you how did we rectify them, which made PDF rendering in our app super fast.

What we were doing initially?

We got the s3 link(storage link of our pdf) from the server and then we downloaded the pdf(using Download Manager API) and after the download was finished, we used to convert each page of the downloaded pdf into images(Using Pdf Renderer library) and then we were showing the pdf to the user as a list of images by using recycler view.

Earlier we used to convert all the pages of pdf into images at one time and then populate the pages in the list.

The problem with this approach was that the function that returned us the bitmap of the page was a heavy function and it took some time to complete( around 5 seconds for each page to be precise), so for large pdfs (having multiple pages), the process of converting all the pages to images first and then loading the list of images into recycler view was time taking and it lead to very slow loading of the report and hence hampered user experience.

What we did to improve Pdf rendering speed?

We came to a conclusion that there is no need of loading all the pages of the pdf at a time because usually, 2 pages of the pdf are in focus on the mobile screen at a time, so keeping all the pages of the user preloaded made no sense.

Now the challenge came how to develop our own custom pagination logic, for that we used the Paging library of android, and made our custom datasource class.

This is the custom datasource class we made for developing our own custom pagination logic.

Quantitative Analysis

To quantitatively define by how much we were able to make pdf rendering fast, let’s assume time taken by getBitmap(page:Page) function(it converted pdf page into image) to be T(we are considering the time of this function as this is the most time-consuming function and it will decide the rendering time of pdf) and let your pdf be of P pages.

So in the previous case pdf loading time was (P*T)(all the pages are loaded at a time, and in case of pagination, it takes 2*T(as only two pages are loaded a time) time so our time is reduced by (P-2)*T.

In terms of percentage, the percentage of pdf rendering was reduced by

(P-2)*T/P*T = (P-2)/P %

where:
P =number of pages in pdf
T = time taken by getBitmap(page) function

After debugging I found that on average the getBitmap() function takes 5 seconds to execute.

Therefore T =5 seconds.

So we can see that for a pdf of P = 10 pages it would take 50 seconds( P*T) and in the case of our new (lazy loading) approach, it takes only 10 seconds.

So we can see that we have reduced the loading time by 80%.

That’s the magic of pagination.

If you want to learn more about pagination you can refer to android documentation -

https://developer.android.com/topic/libraries/architecture/paging/v3-overview

Have any doubts or suggestions regarding the blog? You can write them in the comments section.

--

--