0

I am trying to display pdf file which is self-signed with SSL certificate. Here is my webview setup.

mWebView.clearCache(true);
mWebView.setInitialScale(1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
            if (handler != null){
                handler.proceed();
            } else {
                super.onReceivedSslError(view, null, error);
            }
        }
    });
mWebView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url="+url);

Since it is pdf file, I am adding "http://drive.google.com/viewerng/viewer?embedded=true&url=" as prefix to my actual https url which is basically leading to below error. enter image description here

Now if I just do following line then it is just showing blank page.

mWebView.loadUrl(url);

I have checked my url in Google Chrome on laptop and it is showing pdf file perfectly.

Thank you in advanced.

Pooja
  • 2,417
  • 20
  • 39
  • A webview cannot display a pdf file to begin with. – greenapps Jul 31 '18 at 19:11
  • @greenapps It used to display pdf before we made changes with SSL certificate. You need to add google drive prefix to your url. Please see in my question. – Pooja Jul 31 '18 at 19:15
  • @greenapps It's a pdf file on AWS so I have url of that pdf file which starts with https. – Pooja Jul 31 '18 at 19:15
  • you can check here for ssl https://stackoverflow.com/questions/36553190/check-in-the-onreceivedsslerror-method-of-a-webviewclient-if-a-certificate-is – Neha Chauhan Jul 31 '18 at 20:24
  • This has nothing to do with displaying a pdf. You will receive html from that https url. A webview can display html. Not pdf. – greenapps Aug 01 '18 at 04:30
  • @greenapps My app is able to display other links starting with https but not the pdf file which is on AWS. – Pooja Aug 01 '18 at 16:22

2 Answers2

0
'WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true); 
String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);'
Neha Chauhan
  • 622
  • 4
  • 12
  • Thank you for your answer but it won't work in my case. My pdf file is secured with SSL. It starts with https and not http. – Pooja Jul 31 '18 at 20:13
  • https://stackoverflow.com/questions/36553190/check-in-the-onreceivedsslerror-method-of-a-webviewclient-if-a-certificate-is Please check this link , in that there is a setup of ssl in webview – Neha Chauhan Jul 31 '18 at 20:25
0

First you use web view you create web activity like this

xml layout:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

java code:

/** Crate WebView Activity open web window in app*/
public class WebViewActivity extends AppCompatActivity {

@BindView(R.id.webView1)
WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);

    Intent intent2 = getIntent();
    Bundle bundle = intent2.getExtras();
    String link = bundle.getString("Agreement_URL");
    Log.e("link---",""+link);
    String file_type=bundle.getString("file_type");
    if(file_type.equals("PDF"))
    {
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https://docs.google.com/gview?embedded=true&url="+link);
        setContentView(webView);
    }
    else
    {
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(link);
    }

}

/**  Method on BackPressed Button click*/
public void onBackPressed(){
    super.onBackPressed();

    /** Activity finish*/
    finish();
}
}

sending the data web view using intent from previous activity like this

 upload_doc_tv.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {

             String filename = finalUploadDoc;

             String filenameArray[] = filename.split("\\.");
             String extension = filenameArray[filenameArray.length-1];
             if(extension.equals("pdf"))
             {
                 Intent intent =new Intent(context, WebViewActivity.class);
                 intent.putExtra("Agreement_URL","https://54.183.245.32/uploads/"+ finalUploadDoc);
                 intent.putExtra("file_type","PDF");
                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 Log.e("ggg",""+ finalUploadDoc);
                 context.startActivity(intent);
             }
             else
             {
                 Intent intent =new Intent(context, WebViewActivity.class);
                 intent.putExtra("Agreement_URL","https://54.183.245.32/uploads/"+ finalUploadDoc);
                 intent.putExtra("file_type","image");
                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 Log.e("ggg",""+ finalUploadDoc);
                 context.startActivity(intent);
             }
         }
     });
Android Geek
  • 8,956
  • 2
  • 21
  • 35
  • Maybe my question was not clear enough. I do not have access to pdf file. It's not locally available to me. It's on AWS and all I get is https Link. I do not understand what are you trying to achieve here by using this line intent.putExtra("Agreement_URL","https://54.183.245.32/uploads/"+ finalUploadDoc); – Pooja Aug 01 '18 at 14:03
  • i am using this link open pdf file – Android Geek Aug 02 '18 at 04:01