Todor Grudev

    Todor Grudev

    1 year ago
    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?
    @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>")
        }
      })
    }
    Csaba Szugyiczki

    Csaba Szugyiczki

    1 year ago
    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
    Todor Grudev

    Todor Grudev

    1 year ago
    I see, that explains a lot, thank you @Csaba Szugyiczki
    Tgo1014

    Tgo1014

    1 year ago
    Or use this small hack to show it on the webview https://stackoverflow.com/a/5296125/6022725 😄
    Csaba Szugyiczki

    Csaba Szugyiczki

    1 year ago
    read the comments below this… I would not use it in production. If it is a hobby project then it might be fine
    Todor Grudev

    Todor Grudev

    1 year ago
    doesn’t look like something stable, I think that the external library pdf viewer is a better option in this case
    Colton Idle

    Colton Idle

    1 year ago
    I've used the "hack" in production apps with a million+ installs and it works fine for us. /shruggie
    e

    ephemient

    1 year ago