Sam
Sam

Reputation: 6395

How to show PDF file from URL and show in WebView

I have a recyleView which show list of client bills when clicking on bill I need to display the detail bill (bill Url - when pate the URL on browser its download as a PDF - required to authenticate) I used the below code to download the PDF, I can thee PDF is downloading my questions

a) how can I show this in webView

b) is there any other way I can show this PDF to the user without downloading/writing to external director

 if(ContextCompat.checkSelfPermission(getActivity(),
                        Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
                    webView.setDownloadListener(new DownloadListener() {

                        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
                            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

                            request.setMimeType(mimeType);
                            //------------------------COOKIE!!------------------------
                            String cookies = CookieManager.getInstance().getCookie(url);
                            request.addRequestHeader("cookie", cookies);
                            //------------------------COOKIE!!------------------------
                            request.addRequestHeader("User-Agent", userAgent);
                            request.setDescription("Downloading file...");
                            request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
                            request.allowScanningByMediaScanner();
                            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
                            DownloadManager dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
                            dm.enqueue(request);
                            Toast.makeText(getActivity().getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
                        }
                    });

                }else{
                    if(shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
                        Toast.makeText(getActivity(),
                                "External storage permission required to save billing statements",
                                Toast.LENGTH_SHORT).show();
                    }
                    requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            REQUEST_EXTERNAL_STORAGE_RESULT);
                }

Upvotes: 0

Views: 877

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006724

how can I show this in webView

WebView has no native ability to render PDFs. You can use Mozilla's PDF.js to render a PDF in WebView, though this only works on Android 4.4+ and requires some minor tweaks to PDF.js itself.

is there any other way I can show this PDF to the user without downloading/writing to external director

You can download it to internal storage (e.g., getFilesDir(), getCacheDir()), serve the file via FileProvider, and use ACTION_VIEW to allow the user to view the PDF using their preferred PDF viewer.

For limited PDFs on Android 5.0+, there is PdfRenderer in the Android SDK. pdfium is available for Android, but it is rather large. See this blog post for more about PDF viewing options.

There are also commercial PDF rendering libraries that you can research.

Upvotes: 1

Related Questions