【问题标题】:Show blank page in showing pdf via Googler drive in Android webview在 Android webview 中通过 Google 驱动器显示 pdf 时显示空白页
【发布时间】:2020-05-02 05:16:49
【问题描述】:

我通过https://docs.google.com/viewer?embedded=true&url={my pdf link} 在 WebView 中显示我的 pdf。有时我的 WebView 在“onReceivedError”方法中显示没有任何错误的空白页面。为什么会出现这个空白页?

【问题讨论】:

    标签: android android-webview


    【解决方案1】:

    以此为例。

    class PdfViewActivity : AppCompatActivity() {
        private lateinit var activityPdfViewBinding: ActivityPdfViewBinding
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            activityPdfViewBinding = DataBindingUtil.setContentView(this, R.layout.activity_pdf_view)
            val path =  "https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf" // Add Your URL here
            loadPdfFromURL(path)
        }
    
        @SuppressLint("SetJavaScriptEnabled")
        private fun loadPdfFromURL(path: String?) {
            activityPdfViewBinding.webview.settings.loadWithOverviewMode = true
            activityPdfViewBinding.webview.settings.javaScriptEnabled = true
            val url = "https://docs.google.com/gview?embedded=true&url=$path"
            activityPdfViewBinding.webview.loadUrl(url)
        }
    }
    

    另外,将这些行添加到 onPageFinished 方法中

    if (view.getTitle().equals("")) { 
       view.reload();
    }
    

    【讨论】:

      【解决方案2】:

      解决空白 gview 问题。您可以使用 HTML 和 JQuery 来完成。

      HTML Code:
      <iframe  id="assetDivIdMob" src="https://docs.google.com/gview?embedded=true&amp;url=YOUR-PDF-URL.pdf" width="100%" height="100%" frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe> 
      
      
      JQuery code:
      
      //Wait for some time & then call loadPDF
      setTimeout(function(){ 
          loadPDF(); 
      },1050);
      
      function loadPDF() {
          //This statement will raise DOMException if gview PDF already loaded in Iframe & Code excution will stop.
          document.getElementById('assetDivIdMob').contentWindow.document;
      
          //If iframe is blank then reload Iframe.
          $('#assetDivIdMob').attr('src', $('#assetDivIdMob').attr('src'));
      
          console.log("reloading iframe...");  
      
          //Call loadPDF() after sometime & load PDF in Iframe till it is loaded in Iframe.
          setTimeout(loadPDF, 2000);
       }
      

      【讨论】:

        猜你喜欢
        • 2012-12-27
        • 1970-01-01
        • 1970-01-01
        • 2014-07-19
        • 2019-09-13
        • 2020-07-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多