Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.
Rust androiddocument-scannerdocximage-editorjetpack-composekotlinliquid-glassofflineopen-sourcepdfpdf-compressorpdf-editorpdf-mergerpdf-readerpdf-splitterpdf-toolspdf-viewerpptxspreadsheet-editorxlsx
feat: URL to PDF + calmer, tighter search highlight
Chethan616c82449892db4
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xmlindex 47b7fe2..f041d53 100644--- a/app/src/main/AndroidManifest.xml+++ b/app/src/main/AndroidManifest.xml@@ -4,6 +4,9 @@<!-- Camera permission for document scanning --><uses-permission android:name="android.permission.CAMERA" />+ <!-- Network access is used ONLY by the Web-to-PDF tool to fetch a user-entered URL. -->+ <uses-permission android:name="android.permission.INTERNET" />+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><!-- Legacy read access for older providers on Android 12L and below. --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" /><!-- Legacy write access for pre-scoped-storage devices only. -->diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/screen/HtmlToPdfScreen.kt b/app/src/main/java/com/chethan616/clearpdf/ui/screen/HtmlToPdfScreen.ktindex 7e84b59..58d1fcb 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/screen/HtmlToPdfScreen.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/screen/HtmlToPdfScreen.kt@@ -16,9 +16,11 @@ import androidx.compose.foundation.layout.sizeimport androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.foundation.text.BasicTextimport androidx.compose.foundation.text.BasicTextField+import androidx.compose.foundation.text.KeyboardOptionsimport androidx.compose.material.icons.Iconsimport androidx.compose.material.icons.rounded.Codeimport androidx.compose.material.icons.rounded.FileOpen+import androidx.compose.material.icons.rounded.Languageimport androidx.compose.material.icons.rounded.PictureAsPdfimport androidx.compose.material3.CircularProgressIndicatorimport androidx.compose.material3.Icon@@ -34,6 +36,7 @@ import androidx.compose.ui.platform.LocalContextimport androidx.compose.ui.res.stringResourceimport androidx.compose.ui.text.TextStyleimport androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.text.input.KeyboardTypeimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.sp@@ -46,6 +49,7 @@ import com.chethan616.clearpdf.ui.components.liquidGlassPanelimport com.chethan616.clearpdf.ui.theme.LocalIsDarkModeimport com.chethan616.clearpdf.ui.utils.rememberUISensorimport com.chethan616.clearpdf.ui.viewmodel.HtmlToPdfViewModel+import com.chethan616.clearpdf.ui.viewmodel.WebToPdfModeimport com.kyant.backdrop.backdrops.LayerBackdropimport kotlinx.coroutines.delay@@ -84,33 +88,60 @@ fun HtmlToPdfScreen(verticalArrangement = Arrangement.spacedBy(14.dp)) {GlassSectionHeader(title = stringResource(R.string.tool_html_to_pdf), icon = Icons.Rounded.Code, iconTint = HtmlAccent, titleColor = text)- BasicText(stringResource(R.string.html_to_pdf_desc), style = TextStyle(sub, 13.sp, textAlign = TextAlign.Start))- LiquidButton(onClick = { filePicker.launch("text/html") }, backdrop = backdrop, tint = HtmlAccent, modifier = Modifier.fillMaxWidth()) {- Row(horizontalArrangement = Arrangement.spacedBy(6.dp), verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(vertical = 4.dp)) {- Icon(Icons.Rounded.FileOpen, null, Modifier.size(18.dp), Color.White)- BasicText(stringResource(R.string.html_to_pdf_load_file), style = TextStyle(Color.White, 15.sp, fontWeight = FontWeight.Medium))- }- }- if (state.sourceName.isNotEmpty()) {- BasicText(state.sourceName, style = TextStyle(sub, 12.sp), maxLines = 1)+ // Mode: Web URL / HTML+ Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(10.dp)) {+ ModeOption(stringResource(R.string.html_to_pdf_mode_web), state.mode == WebToPdfMode.URL, backdrop, isLight, { viewModel.onModeChange(WebToPdfMode.URL) }, Modifier.weight(1f))+ ModeOption(stringResource(R.string.html_to_pdf_mode_html), state.mode == WebToPdfMode.HTML, backdrop, isLight, { viewModel.onModeChange(WebToPdfMode.HTML) }, Modifier.weight(1f))}- BasicTextField(- value = state.html,- onValueChange = viewModel::onHtmlChange,- textStyle = TextStyle(text, 13.sp),- modifier = Modifier- .fillMaxWidth()- .heightIn(min = 160.dp, max = 320.dp)- .clip(RoundedCornerShape(12.dp))- .background(if (isDark) Color.White.copy(0.08f) else Color.Black.copy(0.05f))- .padding(12.dp),- decorationBox = { inner ->- if (state.html.isEmpty()) BasicText(stringResource(R.string.html_to_pdf_hint), style = TextStyle(sub, 13.sp))- inner()+ if (state.mode == WebToPdfMode.URL) {+ BasicText(stringResource(R.string.html_to_pdf_url_desc), style = TextStyle(sub, 13.sp, textAlign = TextAlign.Start))+ Row(+ Modifier.fillMaxWidth().clip(RoundedCornerShape(12.dp))+ .background(if (isDark) Color.White.copy(0.08f) else Color.Black.copy(0.05f))+ .padding(horizontal = 12.dp, vertical = 12.dp),+ verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)+ ) {+ Icon(Icons.Rounded.Language, null, Modifier.size(18.dp), sub)+ BasicTextField(+ value = state.url,+ onValueChange = viewModel::onUrlChange,+ singleLine = true,+ textStyle = TextStyle(text, 15.sp),+ keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri),+ modifier = Modifier.weight(1f),+ decorationBox = { inner ->+ if (state.url.isEmpty()) BasicText(stringResource(R.string.html_to_pdf_url_hint), style = TextStyle(sub, 15.sp))+ inner()+ }+ )}- )+ } else {+ BasicText(stringResource(R.string.html_to_pdf_desc), style = TextStyle(sub, 13.sp, textAlign = TextAlign.Start))+ LiquidButton(onClick = { filePicker.launch("text/html") }, backdrop = backdrop, tint = HtmlAccent, modifier = Modifier.fillMaxWidth()) {+ Row(horizontalArrangement = Arrangement.spacedBy(6.dp), verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(vertical = 4.dp)) {+ Icon(Icons.Rounded.FileOpen, null, Modifier.size(18.dp), Color.White)+ BasicText(stringResource(R.string.html_to_pdf_load_file), style = TextStyle(Color.White, 15.sp, fontWeight = FontWeight.Medium))+ }+ }+ if (state.sourceName.isNotEmpty()) BasicText(state.sourceName, style = TextStyle(sub, 12.sp), maxLines = 1)+ BasicTextField(+ value = state.html,+ onValueChange = viewModel::onHtmlChange,+ textStyle = TextStyle(text, 13.sp),+ modifier = Modifier+ .fillMaxWidth()+ .heightIn(min = 160.dp, max = 320.dp)+ .clip(RoundedCornerShape(12.dp))+ .background(if (isDark) Color.White.copy(0.08f) else Color.Black.copy(0.05f))+ .padding(12.dp),+ decorationBox = { inner ->+ if (state.html.isEmpty()) BasicText(stringResource(R.string.html_to_pdf_hint), style = TextStyle(sub, 13.sp))+ inner()+ }+ )+ }}LiquidButton(@@ -140,3 +171,28 @@ fun HtmlToPdfScreen(Spacer(Modifier.height(40.dp))}}++@Composable+private fun ModeOption(+ label: String,+ selected: Boolean,+ backdrop: LayerBackdrop,+ isLight: Boolean,+ onClick: () -> Unit,+ modifier: Modifier = Modifier+) {+ val contentColor = if (selected) Color.White else (if (isLight) Color(0xFF2C2C2E) else Color(0xFFE0E0E0))+ LiquidButton(+ onClick = onClick,+ backdrop = backdrop,+ tint = if (selected) HtmlAccent else Color.Transparent,+ surfaceColor = if (selected) HtmlAccent.copy(0.18f) else (if (isLight) Color.White.copy(0.70f) else Color.White.copy(0.10f)),+ modifier = modifier+ ) {+ BasicText(+ label,+ style = TextStyle(contentColor, 14.sp, fontWeight = if (selected) FontWeight.Bold else FontWeight.Medium),+ modifier = Modifier.padding(vertical = 4.dp)+ )+ }+}diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfContinuousPage.kt b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfContinuousPage.ktindex 297d697..19d59eb 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfContinuousPage.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfContinuousPage.kt@@ -291,24 +291,24 @@ internal fun PdfContinuousPage(frame.left + match.right * frame.width,frame.top + match.bottom * frame.height)- // Hug the glyphs tightly: minimal horizontal bleed, corners scaled to the- // match height so the pill matches the text size (never a fixed slab).- val padX = 2f- val padY = 1.5f- val hl = Rect(r.left - padX, r.top - padY, r.right + padX, r.bottom + padY)- // Fully rounded "pill" — corner radius near half the height for a soft, modern look.- val cr = (hl.height * 0.42f).coerceIn(4f, 10f)+ // The match rect spans the whole LINE box (including ascender/descender+ // leading), so trim it vertically to hug the glyphs instead of towering over+ // them; only a hair of horizontal bleed.+ val insetY = r.height * 0.16f+ val padX = 1.5f+ val hl = Rect(r.left - padX, r.top + insetY, r.right + padX, r.bottom - insetY)+ // Gentle rounding scaled to the trimmed height — a soft tag, not a big pill.+ val cr = (hl.height * 0.26f).coerceIn(3f, 6f)if (match == activeMatch) {- // Current result: a warm outer glow, a rich translucent fill, and a crisp- // accent outline so it reads clearly as the focused match.- val glow = hl.inflate(5f)- drawRoundRect(Color(0xFFFF7A00).copy(0.16f), glow.topLeft, glow.size, CornerRadius(cr + 4f, cr + 4f))- drawRoundRect(Color(0xFFFF9500).copy(0.42f), hl.topLeft, hl.size, CornerRadius(cr, cr))- drawRoundRect(Color(0xFFFF7A00), hl.topLeft, hl.size, CornerRadius(cr, cr), style = Stroke(1.75f))+ // Focused result: a calm blue fill with a soft halo and a crisp thin outline.+ val base = Color(0xFF3B82F6)+ val glow = hl.inflate(3f)+ drawRoundRect(base.copy(0.12f), glow.topLeft, glow.size, CornerRadius(cr + 3f, cr + 3f))+ drawRoundRect(base.copy(0.30f), hl.topLeft, hl.size, CornerRadius(cr, cr))+ drawRoundRect(base.copy(0.95f), hl.topLeft, hl.size, CornerRadius(cr, cr), style = Stroke(1.5f))} else {- // Secondary results: a single soft translucent amber pill — discoverable,- // low-emphasis, no harsh border.- drawRoundRect(Color(0xFFFFC24D).copy(0.30f), hl.topLeft, hl.size, CornerRadius(cr, cr))+ // Secondary results: a soft translucent blue, low-emphasis, no border.+ drawRoundRect(Color(0xFF60A5FA).copy(0.22f), hl.topLeft, hl.size, CornerRadius(cr, cr))}}}diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/HtmlToPdfViewModel.kt b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/HtmlToPdfViewModel.ktindex c47d248..b623b02 100644--- a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/HtmlToPdfViewModel.kt+++ b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/HtmlToPdfViewModel.kt@@ -26,7 +26,11 @@ import java.util.Dateimport java.util.Localeimport kotlin.coroutines.resume+enum class WebToPdfMode { URL, HTML }+data class HtmlToPdfUiState(+ val mode: WebToPdfMode = WebToPdfMode.URL,+ val url: String = "",val html: String = "",val sourceName: String = "",val isProcessing: Boolean = false,@@ -40,6 +44,8 @@ class HtmlToPdfViewModel : ViewModel() {private val _uiState = MutableStateFlow(HtmlToPdfUiState())val uiState: StateFlow<HtmlToPdfUiState> = _uiState.asStateFlow()+ fun onModeChange(mode: WebToPdfMode) = _uiState.update { it.copy(mode = mode, lastOutputUri = null, resultMessage = null, errorMessage = null) }+ fun onUrlChange(value: String) = _uiState.update { it.copy(url = value) }fun onHtmlChange(value: String) = _uiState.update { it.copy(html = value) }fun onLoadFile(context: Context, uri: Uri) {@@ -58,24 +64,32 @@ class HtmlToPdfViewModel : ViewModel() {fun convert(context: Context) {if (_uiState.value.isProcessing) return- if (_uiState.value.html.isBlank()) {+ val mode = _uiState.value.mode+ val rawUrl = _uiState.value.url.trim()+ if (mode == WebToPdfMode.URL && rawUrl.isBlank()) {+ _uiState.update { it.copy(errorMessage = "Enter a web address") }+ return+ }+ if (mode == WebToPdfMode.HTML && _uiState.value.html.isBlank()) {_uiState.update { it.copy(errorMessage = "Enter or load some HTML") }return}+ // Default the scheme so "example.com" works.+ val url = if (rawUrl.startsWith("http://") || rawUrl.startsWith("https://")) rawUrl else "https://$rawUrl"_uiState.update { it.copy(isProcessing = true, errorMessage = null, resultMessage = null, lastOutputUri = null) }viewModelScope.launch {try {val ts = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())- val fileName = "ClearPDF_HTML_$ts.pdf"+ val fileName = "ClearPDF_Web_$ts.pdf"val saveLabel = SaveLocationManager.getSavePathDisplay(context)val outUri = withContext(Dispatchers.IO) { createOutputUri(context, fileName) }val html = _uiState.value.html// WebView work must run on the main thread.val ok = withContext(Dispatchers.Main) {suspendCancellableCoroutine<Boolean> { cont ->- HtmlToPdfConverter.convert(context, html, outUri) { success ->- if (cont.isActive) cont.resume(success)- }+ val cb: (Boolean) -> Unit = { success -> if (cont.isActive) cont.resume(success) }+ if (mode == WebToPdfMode.URL) HtmlToPdfConverter.convertUrl(context, url, outUri, cb)+ else HtmlToPdfConverter.convertHtml(context, html, outUri, cb)}}if (ok) {@@ -85,7 +99,7 @@ class HtmlToPdfViewModel : ViewModel() {))_uiState.update { it.copy(isProcessing = false, lastOutputUri = outUri, resultMessage = "PDF created · saved to $saveLabel") }} else {- _uiState.update { it.copy(isProcessing = false, errorMessage = "Couldn't render this HTML") }+ _uiState.update { it.copy(isProcessing = false, errorMessage = "Couldn't create the PDF") }}} catch (t: Throwable) {_uiState.update { it.copy(isProcessing = false, errorMessage = t.message ?: "Couldn't convert HTML") }diff --git a/app/src/main/java/com/chethan616/clearpdf/util/HtmlToPdfConverter.kt b/app/src/main/java/com/chethan616/clearpdf/util/HtmlToPdfConverter.ktindex 160a318..543dfbb 100644--- a/app/src/main/java/com/chethan616/clearpdf/util/HtmlToPdfConverter.kt+++ b/app/src/main/java/com/chethan616/clearpdf/util/HtmlToPdfConverter.kt@@ -6,13 +6,15 @@ import android.net.Uriimport android.view.Viewimport android.webkit.WebViewimport android.webkit.WebViewClient-import kotlin.math.min/**- * Converts a local HTML string to a paginated PDF entirely on-device: an offscreen [WebView]- * lays the HTML out at A4 width, then each page-height slice is drawn onto a [PdfDocument] page.- * No network is used (the app holds no INTERNET permission) — JavaScript is disabled and the- * base URL is null, so only self-contained HTML/CSS renders.+ * Converts a web page (URL) or a local HTML string to a paginated PDF on-device: an offscreen+ * [WebView] lays the content out at A4 width, then each page-height slice is drawn onto a+ * [PdfDocument] page.+ *+ * - [convertUrl] fetches a user-entered URL (JavaScript enabled) — the only place the app uses+ * the network.+ * - [convertHtml] renders self-contained HTML with JS disabled and no base URL (fully offline).** MUST be called on the main thread (WebView requirement). [onDone] is invoked on the main* thread with true on success.@@ -23,22 +25,40 @@ object HtmlToPdfConverter {private const val PAGE_W = 794private const val PAGE_H = 1123- fun convert(context: Context, html: String, outputUri: Uri, onDone: (Boolean) -> Unit) {+ fun convertHtml(context: Context, html: String, outputUri: Uri, onDone: (Boolean) -> Unit) {val webView = WebView(context)webView.settings.javaScriptEnabled = false- @Suppress("DEPRECATION")- webView.setInitialScale(100)+ finishOnLoad(webView, context, outputUri, settleMs = 250, onDone)+ webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null)+ }++ fun convertUrl(context: Context, url: String, outputUri: Uri, onDone: (Boolean) -> Unit) {+ val webView = WebView(context)+ webView.settings.apply {+ javaScriptEnabled = true+ domStorageEnabled = true+ loadWithOverviewMode = true+ useWideViewPort = true+ }+ // JS-heavy pages need a longer settle before the layout is stable enough to snapshot.+ finishOnLoad(webView, context, outputUri, settleMs = 900, onDone)+ webView.loadUrl(url)+ }++ private fun finishOnLoad(webView: WebView, context: Context, outputUri: Uri, settleMs: Long, onDone: (Boolean) -> Unit) {+ var handled = falsewebView.webViewClient = object : WebViewClient() {override fun onPageFinished(view: WebView, url: String?) {- // Give layout a beat to settle before measuring.+ if (handled) returnview.postDelayed({+ if (handled) return@postDelayed+ handled = trueval ok = runCatching { render(view, context, outputUri) }.getOrDefault(false)runCatching { view.destroy() }onDone(ok)- }, 250)+ }, settleMs)}}- webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null)}private fun render(view: WebView, context: Context, outputUri: Uri): Boolean {diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xmlindex b46ee59..853fc00 100644--- a/app/src/main/res/values-pt-rBR/strings.xml+++ b/app/src/main/res/values-pt-rBR/strings.xml@@ -413,9 +413,13 @@<string name="image_tools_processing">Processando…</string><string name="image_tools_save">Salvar</string><string name="image_tools_share">Compartilhar</string>- <string name="tool_html_to_pdf">HTML para PDF</string>- <string name="tool_html_to_pdf_sub">Converter HTML em PDF</string>- <string name="html_to_pdf_desc">Cole HTML ou carregue um arquivo .html — renderizado no dispositivo, sem internet.</string>+ <string name="tool_html_to_pdf">Web para PDF</string>+ <string name="tool_html_to_pdf_sub">URL ou HTML em PDF</string>+ <string name="html_to_pdf_mode_web">URL da web</string>+ <string name="html_to_pdf_mode_html">HTML</string>+ <string name="html_to_pdf_url_desc">Informe um endereço da web para capturar a página como PDF.</string>+ <string name="html_to_pdf_url_hint">https://exemplo.com</string>+ <string name="html_to_pdf_desc">Cole HTML ou carregue um arquivo .html — renderizado no dispositivo.</string><string name="html_to_pdf_load_file">Carregar arquivo .html</string><string name="html_to_pdf_hint"><h1>Olá</h1><p>Seu HTML aqui…</p></string><string name="html_to_pdf_action">Converter em PDF</string>diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xmlindex 2018e84..808cb9d 100644--- a/app/src/main/res/values-pt/strings.xml+++ b/app/src/main/res/values-pt/strings.xml@@ -413,9 +413,13 @@<string name="image_tools_processing">Processando…</string><string name="image_tools_save">Salvar</string><string name="image_tools_share">Compartilhar</string>- <string name="tool_html_to_pdf">HTML para PDF</string>- <string name="tool_html_to_pdf_sub">Converter HTML em PDF</string>- <string name="html_to_pdf_desc">Cole HTML ou carregue um arquivo .html — renderizado no dispositivo, sem internet.</string>+ <string name="tool_html_to_pdf">Web para PDF</string>+ <string name="tool_html_to_pdf_sub">URL ou HTML em PDF</string>+ <string name="html_to_pdf_mode_web">URL da web</string>+ <string name="html_to_pdf_mode_html">HTML</string>+ <string name="html_to_pdf_url_desc">Informe um endereço da web para capturar a página como PDF.</string>+ <string name="html_to_pdf_url_hint">https://exemplo.com</string>+ <string name="html_to_pdf_desc">Cole HTML ou carregue um arquivo .html — renderizado no dispositivo.</string><string name="html_to_pdf_load_file">Carregar arquivo .html</string><string name="html_to_pdf_hint"><h1>Olá</h1><p>Seu HTML aqui…</p></string><string name="html_to_pdf_action">Converter em PDF</string>diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xmlindex c897798..b8276bd 100644--- a/app/src/main/res/values/strings.xml+++ b/app/src/main/res/values/strings.xml@@ -104,9 +104,13 @@<string name="image_tools_processing">Processing…</string><string name="image_tools_save">Save</string><string name="image_tools_share">Share</string>- <string name="tool_html_to_pdf">HTML to PDF</string>- <string name="tool_html_to_pdf_sub">Render HTML to a PDF</string>- <string name="html_to_pdf_desc">Paste HTML or load an .html file — rendered on-device, no internet.</string>+ <string name="tool_html_to_pdf">Web to PDF</string>+ <string name="tool_html_to_pdf_sub">URL or HTML to PDF</string>+ <string name="html_to_pdf_mode_web">Web URL</string>+ <string name="html_to_pdf_mode_html">HTML</string>+ <string name="html_to_pdf_url_desc">Enter a web address to capture the page as a PDF.</string>+ <string name="html_to_pdf_url_hint">https://example.com</string>+ <string name="html_to_pdf_desc">Paste HTML or load an .html file — rendered on-device.</string><string name="html_to_pdf_load_file">Load .html file</string><string name="html_to_pdf_hint"><h1>Hello</h1><p>Your HTML here…</p></string><string name="html_to_pdf_action">Convert to PDF</string>diff --git a/image.png b/image.pngnew file mode 100644index 0000000..a4f3fe9Binary files /dev/null and b/image.png differ