Pdf Not Open in Webview from Url

Can not open PDF file using WebView

There some issue while loading pdf with webView. You can use the following code to solve your problem. This method takes the help of drive to view pdf in webView.Finally let me know if your problem solved or not

webview.getSettings().setJavaScriptEnabled(true); 
String pdf = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf";
webview.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);

unable to open pdf in webview android app

I have faced the same problem and solved it : you have to override onStartDownload method in the web view .

There are to methods :

Force download and open it :

 webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String aUrl, String userAgent, String contentDisposition, String mimetype, long contentLength) {

fileName = URLUtil.guessFileName(aUrl, contentDisposition, mimetype); //returns a string of the name of the file THE IMPORTANT PART

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(aUrl));
request.allowScanningByMediaScanner();
CookieManager cookieManager = CookieManager.getInstance();
String cookie = cookieManager.getCookie(aUrl);
request.addRequestHeader("Cookie", cookie);
request.setMimeType("application/pdf");
request.addRequestHeader("User-Agent", userAgent);
Environment.getExternalStorageDirectory();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));





}


});

and don't forget to add those methods to open pdf after download :

BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
openFile(fileName); }
};

protected void openFile(String fileName) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator +
fileName);
Uri path = Uri.fromFile(file);
Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenintent.setDataAndType(path, "application/pdf");
try {
this.startActivity(pdfOpenintent);
} catch (ActivityNotFoundException e) {
}
}

Also don't forget declaring perrmission in the :

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

or you can only

open it in intent :

webView.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);
}
});

Both solutions are tested and works .

Webview is not loading a PDF from an URL

Hi try the below code,

String url = null;
try {
url = URLEncoder.encode("file url", "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String path = "http://docs.google.com/gview?embedded=true&url=" + url;
mWebView.loadUrl(path);

Unable to open PDF documents in WebView

To open pdf in Webview , it better to show pdf via google doc service

WebView webView = (WebView) context.findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://www.stagecoachbus.com/PdfUploads/Timetable_28768_5.pdf");

It help you to show pdf.

How to open pdf files in webview in pdfactivity?

BINGO!! My issue resolved can open pdf directly using BufferedInputStream

I created two activity, First Activity to load all pdfs in webview. When user clicks the link of pdf, the pdfurl fetches the url as strings, passes strings to next activity.
PdfActivity.java

   final String pdfurl = view.getHitTestResult().getExtra();
Intent intent = new Intent(PdfActivity.this,PdfViewer.class);
intent.putExtra("PDFURL",pdfurl);
startActivity(intent);

Second Activity is PdfViewer, bateksc pdfviewer to load pdf from url, edited as above
PdfViewer.java

package ak.wp.meto.activity;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
import com.github.barteksc.pdfviewer.PDFView;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import ak.wp.meto.R;

public class PdfViewer extends Activity {
private TextView txt; // You can remove if you don't want this
private PDFView pdf;
String value = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_pdf);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
value = bundle.getString("PDFURL");
}
System.out.println("PRINT PDFVIEWER DATA" +value);
pdf = (PDFView) findViewById(R.id.pdfView); //github.barteksc
txt = findViewById(R.id.txtPdf);
String pdfUrl = value;
try{
new RetrievePdfStream().execute(pdfUrl);
}
catch (Exception e){
Toast.makeText(this, "Failed to load Url :" + e.toString(), Toast.LENGTH_SHORT).show();
}
}

class RetrievePdfStream extends AsyncTask<String, Void, InputStream> {
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdf.fromStream(inputStream).load();
}
}

}


Related Topics



Leave a reply



Submit