Lessons from handling 1 GB PDFs on an Android phone, fully offline
I'm MGA, a full-stack developer. For the last few weeks I've been building BotoPDF, an Android app where every PDF tool runs on the phone: compress, merge, split, sign, scan, convert, and a reader. Nothing gets uploaded.
The hard part wasn't the tools. It was making them behave with big files. I set a rule early that every tool had to work on PDFs up to 1 GB, and that rule broke a lot of my first ideas. Here are the ones that changed the design the most.
1. Picking a file must not open the file
My first version parsed the whole document as soon as you picked it, to show the page count and a thumbnail. With a 1 GB file the user stared at a spinner for minutes before the tool even started.
Now the picker reads only what the next screen needs: the name, the size, the page count and the first page. The heavy work starts when the user taps the tool's main button, inside a foreground service with a progress notification, so it keeps running when the screen turns off.
2. Don't compress the file twice
The compress screen shows three levels with the real resulting size of each. For files up to 100 MB the app actually compresses all three in the background, and when you tap Compress it saves the run you picked instead of doing it again.
Above 100 MB I first used an estimate that re-encoded sample images. On a 1 GB file that was a second heavy compression before the real one. So above 100 MB the cards now show no size at all, just one line saying the final size appears after compressing.
On the phone, a 1.1 GB file goes down to 138 MB in about a minute.
3. Cancel needs a confirmation
During testing, an accidental tap on Cancel at 100% (while the result was being saved) threw away a 4.7 GB merge. Now Cancel always asks first, and it disappears once a job passes 70%, because at that point stopping costs more than finishing.
4. The file has to be on disk before you show it
A saved result used to appear in My Files before its bytes were forced to storage. If the phone lost power a few seconds later, the file could be cut short. The fix is boring: flush and fsync before the file becomes visible.
5. A zoom bug that only happened on the first page
The reader is one long vertical list of pages. Zoom scales the whole list around the center of the screen, and moving up and down is the list's own scroll.
That worked everywhere except at the two ends of the file: when zoomed in at the top, the list was already at scroll 0, so the part of page 1 above the center of the screen could never be reached. At 2.5x that was about 43% of the first page, and at 5x about 58%.
The fix was a vertical shift that is only used at the ends, the same idea as the horizontal pan the viewer already had. It stays 0 at normal zoom and in the middle of the file, so search, links and the page scrubber didn't need to change. I wrote the failing tests first, which made it easy to prove the fix and not just believe it.
The stack
Kotlin and Jetpack Compose. PdfBox-Android does the reading and writing, and Android's own PdfRenderer (Pdfium) draws the pages. Every result is saved as a new file, so the original is never touched.
Try it
BotoPDF is on Google Play: https://play.google.com/store/apps/details?id=com.botopdf.app
To be upfront: it's a subscription with a 7-day free trial, and there are no ads. If you have a PDF that it handles badly, I'd like to hear about it in the comments.
Comments
No comments yet. Start the discussion.