I am trying to load a simple pdf in a `WebView` (c...
# compose
t
I am trying to load a simple pdf in a
WebView
(code in 🧵) but it doesn’t seem to work, is there a potential issue with
WebView
and compose?
Copy code
@Composable
fun WebPagePreview() {
  AndroidView(factory = {
    WebView(it).apply {
      layoutParams = ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT
      )
      webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
          return false
        }
      }
      loadUrl("<https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf>")
    }
  })
}
c
Android WebView cannot display pdf documents on its own
try copy-pasting this url into Chrome, it will open your default pdf viewer
you should do the same (open an external app that is capable of displaying pdf) Or render the pdf yourself Or use a pdf viewer library like https://github.com/barteksc/AndroidPdfViewer
t
I see, that explains a lot, thank you @Csaba Szugyiczki
👍 1
t
Or use this small hack to show it on the webview https://stackoverflow.com/a/5296125/6022725 😄
c
read the comments below this… I would not use it in production. If it is a hobby project then it might be fine
t
doesn’t look like something stable, I think that the external library pdf viewer is a better option in this case
✔️ 1
c
I've used the "hack" in production apps with a million+ installs and it works fine for us. /shruggie
e