Androidpdfviewer: How can pdfViewer render a pdf from a web url?

Created on 22 Dec 2019  Â·  4Comments  Â·  Source: barteksc/AndroidPdfViewer

Is it posible to render and show pdf taken from a url like https://mslc.osu.edu/sites/default/files/Integral%20Calculus%20Formula%20Sheet_0.pdf? If it's, how? Thanks.

Most helpful comment

private void getPdf(String url) {

        pdfView.setVisibility(View.VISIBLE);
        final InputStream[] input = new InputStream[1];
        new AsyncTask<Void, Void, Void>() {
            @SuppressLint({"WrongThread", "StaticFieldLeak"})
            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    input[0] = new URL(url).openStream();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                pdfView.fromStream(input[0])
                        .pageFitPolicy(FitPolicy.WIDTH)
                        .load();
            }
        }.execute();
    }

All 4 comments

@titosobabas unfortunately you need to download pdf, fo example using retrofit https://futurestud.io/tutorials/retrofit-2-how-to-download-files-from-server and then call pdfView.fromBytes(byteArray)

itosobabas unfortunately you need to download pdf, fo example using retrofit https://futurestud.io/tutorials/retrofit-2-how-to-download-files-from-server and then call pdfView.fromBytes(byteArray)

can u give me example how i can call this after downloading ?

hey is this only for pdfs ??
can i view ppts using this??

private void getPdf(String url) {

        pdfView.setVisibility(View.VISIBLE);
        final InputStream[] input = new InputStream[1];
        new AsyncTask<Void, Void, Void>() {
            @SuppressLint({"WrongThread", "StaticFieldLeak"})
            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    input[0] = new URL(url).openStream();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                pdfView.fromStream(input[0])
                        .pageFitPolicy(FitPolicy.WIDTH)
                        .load();
            }
        }.execute();
    }
Was this page helpful?
0 / 5 - 0 ratings