androidgetandroid-webview

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


Only after visiting almost all Stack Overflow links for the same answer, I was compelled to write this,

I am trying to display a PDF file in the WebView. I am aware that WebView doesn't support displaying PDF normally so I am using the G-Drive URL extension suggested in other answers. Also, the Business team has demanded to open the PDF in our App itself so I can't fire an Intent to other PDF Viewer. I downloaded the PdfViewer library but it increased the APK size far beyond desire.

This URL is actually a GET request. Every time I hit the request, depending on the request parameters the server generates the PDF at runtime and returns it immediately without storing it on the server.

PDF URL:

"http://server_ip_number/pata/retail/v4/reports_pdf?search=Test&min_amount=0.0&max_amount=0.0" (can't disclose IP address for security reasons)

This request also demands some headers for auth, which I'll post below in detail.

This is what I am doing currently;

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

NOTE:

  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

But when I try it with WebView it just shows No Preview Available.

What am I doing wrong?


Solution

  • The headers that you are supplying, at most, will affect the behavior of Google's Web server for the https://drive.google.com/viewerng/viewer request. Google is not going to pass those along to some other server for http://server_ip_number/pata/retail/v4/reports_pdf.