Library Overview
AndroidPdfViewer is based on PdfiumAndroid and provides:- Fast PDF rendering with native code
- Smooth scroll and zoom animations
- Page snap and fit-to-width modes
- Custom page spacing and scroll handles
AndroidPdfViewer version used in Kafka
Implementation
The PDF viewer is implemented as a multiplatform Compose component with Android-specific rendering:Android Implementation
ui/reader/pdf/src/main/java/com/kafka/reader/pdf/PdfViewer.android.kt
Maximum zoom level allowed for the PDF viewer
Spacing between pages in pixels
PDF State Management
ThePdfState class manages the viewer’s state and callbacks:
ui/reader/pdf/src/commonMain/kotlin/com/kafka/reader/pdf/PdfViewer.kt
The URI of the PDF file to display (can be local or remote)
The page number to display when the PDF is first loaded (0-indexed)
Callback invoked when the user navigates to a different page
Callback invoked when an error occurs during PDF loading or rendering
Reader Screen
TheReaderScreen component provides a complete PDF reading experience:
ui/reader/pdf/src/commonMain/kotlin/com/kafka/reader/ReaderScreen.kt
Download Progress
For remote PDFs, Kafka shows download progress while streaming:ui/reader/pdf/src/commonMain/kotlin/com/kafka/reader/DownloadProgress.kt
Reader ViewModel
TheReaderViewModel manages PDF loading, page tracking, and error handling:
ui/reader/pdf/src/commonMain/kotlin/com/kafka/reader/ReaderViewModel.kt
Features
Zoom Controls
- Pinch to Zoom: Natural gesture-based zooming
- Double Tap: Quick zoom in/out
- Max Zoom: Configurable maximum zoom level (3x by default)
Page Navigation
- Scroll: Smooth vertical scrolling between pages
- Scroll Handle: Visual scroll indicator showing current position
- Page Snap: Pages snap to position for better reading
- Page Spacing: 12px spacing between pages for clear separation
State Persistence
- Last Page: Remembers the last page read
- Reading Progress: Tracks reading progress for analytics
- Bookmarks: Can be integrated with bookmark system
Gradle Configuration
The PDF module is configured as a multiplatform library:ui/reader/pdf/build.gradle.kts
gradle/libs.versions.toml
Error Handling
The PDF viewer handles various error scenarios:- File not found: Shows error message with retry option
- Corrupt PDF: Detects and reports malformed PDF files
- Network errors: Handles streaming failures gracefully
- Memory issues: Efficiently manages memory for large PDFs
Performance Considerations
- Native Rendering: Uses PdfiumAndroid for fast native PDF rendering
- Page Caching: Automatically caches rendered pages for smooth scrolling
- Memory Management: Releases unused resources automatically
- Streaming Support: Can stream PDFs without full download