安卓webview在点击按钮时不能生成pdf报告

0 人关注

我在Android studio中使用WebView创建了一个网络应用。我在Android WebView中加载了一个WordPress网站。当我在chrome浏览器上加载它时,WordPress网站在点击按钮时输出一个pdf报告。

该网站在chrome中加载

When i click on PDF button shows the window

和 finally it starts downloading the pdf file

和 the generated pdf report looks like:

But the problem occurs when i use the Android App created in WebView. When i click on PDF button it does nothing 和 i get a Debug log

D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
[InputMethodManagerWrapper.java:68] hideSoftInputFromWindow

我将清单权限设置为

 uses-permission android:name="android.permission.INTERNET"

这里的代码 private static final String WEB_URL = "http://workmanager.midzonetechnologies.com/";

webView = findViewById(R.id.webView);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setAllowFileAccessFromFileURLs(true);
    webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webView.getSettings().setBlockNetworkLoads(false);
    webView.getSettings().setAppCacheMaxSize( 10 * 1024 * 1024 ); // 10MB
    webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath() );
    webView.getSettings().setAllowFileAccess( true );
    webView.getSettings().setAppCacheEnabled( true );
    webView.getSettings().setJavaScriptEnabled( true );
    webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
    webView.setWebViewClient(new WebViewClient());
    webView.setWebChromeClient(new WebChromeClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(WEB_URL);

请告诉我这个问题是与清单文件权限还是运行时间权限有关,请给个解决方案。

我在WebView中创建的Android应用程序。

1 个评论
你是在安卓11上测试的吗?
android
android-studio
pdf
webview
Ranjit Vamadevan
Ranjit Vamadevan
发布于 2021-07-24
2 个回答
Florin T.
Florin T.
发布于 2021-07-30
0 人赞同

你在Chrome浏览器中看到的情况是。

  • noticing the downloadable file based on path: mywebsite.com/randomPath/randomFile**.pdf**
  • request download permissions
  • download the file to it's local storage
  • open the PDF file using Google Drive
  • 如果你想要这种行为,你必须在你的应用程序中编写所有这些步骤。

    一个快速的解决方案是拦截网址,检查它是否是一个pdf文件,然后用浏览器打开它。

        webView.webviewClient = object : WebViewClient() {
            @TargetApi(Build.VERSION_CODES.N)
            fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest): Boolean {
                val url = request.url.toString()
                if (url.takeLast(4) == ".pdf") {
                    startActivity(
                        Intent(
                            Intent.ACTION_VIEW,
                            "https://docs.google.com/gview?embedded=true&url=${request.url}"
                    return true
                return false
        
    在chrome浏览器中发生的事情也应该在WebView中发生。我认为数据是从数据库中获取的,而pdf文件是动态创建的。所以我们不能使用网址,而让WebView来做其他的事情。当我点击PDF按钮时,InputMethodManagerWrapper禁用了软输入模式。请告诉我如何解决这个问题。
    Webview不能显示pdf文件。
    为什么它不能像在chrome浏览器中那样工作呢?请阅读我的全部问题并理解它。
    TIMBLOCKER
    TIMBLOCKER
    发布于 2021-07-30
    0 人赞同

    你可能必须在这里有不同的步骤。正如@Florin T.所说,你首先必须获得你的应用程序所需的许可。

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    在下一步,你将检测系统是否可以下载任何东西。

    This coud be done like so:

    mWebView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);