简体   繁体   English

在 Android webview 中通过 Googler 驱动器显示 pdf 时显示空白页

[英]Show blank page in showing pdf via Googler drive in Android webview

I show my pdfs via https://docs.google.com/viewer?embedded=true&url= {my pdf link} in WebView.我通过https://docs.google.com/viewer?embedded=true&url= {my pdf 链接} 在 WebView 中显示我的 pdf。 Sometimes my WebView shows a blank page without any error in "onReceivedError" method.有时我的 WebView 在“onReceivedError”方法中显示空白页,没有任何错误。 Why this blank page show?为什么这个空白页显示?

Take this as an example.以此为例。

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)
    }
}

also, add these lines into onPageFinished method另外,将这些行添加到onPageFinished方法中

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

To resolve blank gview issue.解决空白 gview 问题。 You can do it by using HTML & JQuery.您可以通过使用 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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM