动态 URL PDF 在 WebView 中显示为 "No Preview Available"

Dynamic URL PDF is displayed as "No Preview Available" in WebView

只有在访问了几乎所有 Stack Overflow 链接以获得相同答案后,我才不得不写这篇文章,

我正在尝试在 WebView 中显示 PDF 文件。我知道 WebView 不支持正常显示 PDF,所以我使用其他答案中建议的 G-Drive URL 扩展。此外,业务团队要求在我们的应用程序本身中打开 PDF,因此我无法向其他 PDF 查看器发送 Intent。我下载了 PdfViewer 库,但它增加了 APK 大小,远远超出了我的期望。

这个URL实际上是一个GET请求。每次我点击请求时,根据请求参数,服务器会在运行时生成 PDF 并立即 returns 它而不将其存储在服务器上。

PDF URL:

"http://server_ip_number/pata/retail/v4/reports_pdf?search=Test&min_amount=0.0&max_amount=0.0"(出于安全原因不能透露 IP 地址)

此请求还需要一些 headers 的身份验证,我将在下面 post 详细说明。

这就是我目前正在做的事情;

class AllUsersWebViewActivity : BaseActivity() {


    private val pdfActualUrl = "https://drive.google.com/viewerng/viewer?embedded=true&url=http://server_ip_number/pata/retail/v4/reports_pdf?search=Test&min_amount=0.0&max_amount=0.0"

    //This random dummy URL opens perfectly, it does not need any headers or GET params, just normal `loadUrl()` works.
    private val workingDummyUrl = "https://drive.google.com/viewerng/viewer?embedded=true&url=https://mindorks.s3.ap-south-1.amazonaws.com/courses/MindOrks_Android_Online_Professional_Course-Syllabus.pdf"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_all_users_report_web_view)
        wv_web.settings.javaScriptEnabled = true
        wv_web.clearCache(true)
        wv_web.loadUrl(URLEncoder.encode(pdfUrl, "ISO-8859-1"), mapOf( //map of necessary headers 
                "Authorization" to "Auth_TOKEN",
                "VersionId" to "1.0.0",
                "companyID" to "2",
                "DeviceId" to "my_device_id",
                "token" to "SECURE_TOKEN"
        ))
    }

注意:

  1. When I hit the above mentioned GET request with headers and params on PostMan it immediately downloads the PDF file.

  2. I tried the same GET request with DownloadManager with all the headers and params the PDF file is downloaded successfully

但是当我用 WebView 尝试时它只显示 No Preview Available.

我做错了什么?

您提供的 headers 最多会影响 Google 的 Web 服务器对 https://drive.google.com/viewerng/viewer 请求的行为。 Google 不会将它们传递给 http://server_ip_number/pata/retail/v4/reports_pdf 的其他服务器。