【发布时间】:2020-05-02 05:16:49
【问题描述】:
我通过https://docs.google.com/viewer?embedded=true&url={my pdf link} 在 WebView 中显示我的 pdf。有时我的 WebView 在“onReceivedError”方法中显示没有任何错误的空白页面。为什么会出现这个空白页?
【问题讨论】:
我通过https://docs.google.com/viewer?embedded=true&url={my pdf link} 在 WebView 中显示我的 pdf。有时我的 WebView 在“onReceivedError”方法中显示没有任何错误的空白页面。为什么会出现这个空白页?
【问题讨论】:
以此为例。
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();
}
【讨论】:
解决空白 gview 问题。您可以使用 HTML 和 JQuery 来完成。
HTML Code:
<iframe id="assetDivIdMob" src="https://docs.google.com/gview?embedded=true&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);
}
【讨论】: