AndroidPdfViewer icon indicating copy to clipboard operation
AndroidPdfViewer copied to clipboard

How to open pdf file form url

Open playmans opened this issue 6 years ago • 14 comments

playmans avatar Jul 13 '18 02:07 playmans

There are some pull requests to add this functionality to the project but as it stands they are not 100% robust.

The url has to be downloaded and saved to the device before opening.
However, in my opinion this is out of scope for this project. There are potentially many things to consider like connectivity status, activity lifecycle, managing the cache etc. which should be handled by application developer.

So have a look at downloading files on Android, possibly using DownloadManager, a Service, AsyncTask or some library and implement for your own project.

Flamedek avatar Jul 13 '18 10:07 Flamedek

You can use something like below: new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... voids) { try { InputStream input = new URL("url here").openStream(); pdfView.fromStream(input).load(); } catch (IOException e) { e.printStackTrace(); } return null; } }.execute();

TuanSon avatar Jul 27 '18 08:07 TuanSon

Where should I upload the PDF files to load them in my Android app from a URL?

arsalanengr avatar Jul 31 '18 22:07 arsalanengr

@TuanSon The current thread is worker thread inside doInBackground you need to move "pdfView.fromStream(input).load()" code line to onPostExecute.

skyberk avatar Aug 10 '18 13:08 skyberk

#548

imBalasankar avatar Nov 21 '18 13:11 imBalasankar

How can I preview the pdf url file in the firebase in to my android application?

Atlas789 avatar Aug 04 '19 10:08 Atlas789

You can use something like below: new AsyncTask<Void, Void, Void>() { @override protected Void doInBackground(Void... voids) { try { InputStream input = new URL("url here").openStream(); pdfView.fromStream(input).load(); } catch (IOException e) { e.printStackTrace(); } return null; } }.execute();

This is really works! Thank you!

beckkey avatar Oct 14 '19 18:10 beckkey

@beckkey can you show me how it worked for plz

mohamed00736 avatar Nov 21 '19 05:11 mohamed00736

@beckkey can you show me how it worked for plz

I do something like this:

    @BindView(R.id.receipt_view)
    PDFView receiptView;

    private void getPdf() {
        new AsyncTask<Void, Void, Void>() {
            @SuppressLint("WrongThread")
            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    InputStream input = new URL("http://www.africau.edu/images/default/sample.pdf").openStream();
                    receiptView.fromStream(input).load();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute();
    }

beckkey avatar Nov 21 '19 12:11 beckkey

Is there any way to save on device the pdf after it is loaded in pdfview as stream? Or it needs to download the pdf before and then load as a file?

Any method to save on device the loaded pdf as stream directly ?

Thanks in advance for the answer.

Il Gio 21 Nov 2019, 13:18 beckkey [email protected] ha scritto:

@beckkey https://github.com/beckkey can you show me how it worked for plz

I do something like this:

@BindView(R.id.receipt_view)
PDFView receiptView;

private void getPdf() {
    new AsyncTask<Void, Void, Void>() {
        @SuppressLint("WrongThread")
        @Override
        protected Void doInBackground(Void... voids) {
            try {
                InputStream input = new URL("http://www.africau.edu/images/default/sample.pdf").openStream();
                receiptView.fromStream(input).load();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }.execute();
}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/barteksc/AndroidPdfViewer/issues/634?email_source=notifications&email_token=AHQ634EC7XFQDOQPLO6HZ73QUZ4BVA5CNFSM4FJY2EQ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEE2A6GY#issuecomment-557059867, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHQ634DETNVQIUMK6C3WNQ3QUZ4BVANCNFSM4FJY2EQQ .

simonbellu avatar Nov 21 '19 12:11 simonbellu

Is there any way to save on device the pdf after it is loaded in pdfview as stream? Or it needs to download the pdf before and then load as a file? Any method to save on device the loaded pdf as stream directly ? Thanks in advance for the answer.

@simonbellu You can modify the above function:

new AsyncTask<Void, Void, Void>() {
                @SuppressLint("WrongThread")
                @Override
                protected Void doInBackground(Void... voids) {
                    try {
                        File file = new File(activity.getCacheDir(), "receipt.pdf");
                        try (OutputStream output = new FileOutputStream(file)) {
                            byte[] buffer = new byte[4 * 1024]; // or other buffer size
                            int read;
                            InputStream test = new URL(receiptGetUrl).openStream();
                            while ((read = test.read(buffer)) != -1) {
                                output.write(buffer, 0, read);
                            }
                            output.flush();
                            PrintDocumentAdapter printAdapter = new PdfDocumentAdapter(file);
                            printManager.print("Document", printAdapter,new PrintAttributes.Builder().build());
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } finally {
                        try {
                            input.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    return null;
                }
            }.execute();

As you can see, pdf downloaded and saved as a file. You can do anything with saved pdf file (by default file saved in temp dir). In this example file printed with default android print manager.

beckkey avatar Nov 22 '19 07:11 beckkey

Thank you so much for your quick reply and for your code! I wanted to open and view the pdf first in the pdfview from an url as stream and then have the chance to save it on device... I guessed it was not possible and it needs the file to be stored first on device... But you give me a good idea for storing first in temp dir and then printing it. I will try your code!! Thanks again.

Il Ven 22 Nov 2019, 08:57 beckkey [email protected] ha scritto:

Is there any way to save on device the pdf after it is loaded in pdfview as stream? Or it needs to download the pdf before and then load as a file? Any method to save on device the loaded pdf as stream directly ? Thanks in advance for the answer.

You can modify the above function:

new AsyncTask<Void, Void, Void>() { @SuppressLint("WrongThread") @Override protected Void doInBackground(Void... voids) { try { File file = new File(activity.getCacheDir(), "receipt.pdf"); try (OutputStream output = new FileOutputStream(file)) { byte[] buffer = new byte[4 * 1024]; // or other buffer size int read; InputStream test = new URL(receiptGetUrl).openStream(); while ((read = test.read(buffer)) != -1) { output.write(buffer, 0, read); } output.flush(); PrintDocumentAdapter printAdapter = new PdfDocumentAdapter(file); printManager.print("Document", printAdapter,new PrintAttributes.Builder().build()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } finally { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }.execute();

As you can see, pdf downloaded and saved as a file. You can do anything with saved pdf file (by default file saved in temp dir). In this example file printed with default android print manager.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/barteksc/AndroidPdfViewer/issues/634?email_source=notifications&email_token=AHQ634HCBLLPOEFI5PIF4GDQU6GGNA5CNFSM4FJY2EQ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEE43OXI#issuecomment-557430621, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHQ634E7QJ4ACXI3UKAQDILQU6GGNANCNFSM4FJY2EQQ .

simonbellu avatar Nov 22 '19 08:11 simonbellu

KOTLIN CODE WITH PROGRESS BAR

private fun loadPdf() { MyAsyncTask(this).execute() }

companion object{
    class MyAsyncTask internal constructor(context: MainActivity) : AsyncTask<Unit, Unit, InputStream>() {
        private val activityReference: WeakReference<MainActivity> = WeakReference(context)
        override fun onPreExecute() {
            val activity = activityReference.get()
            if (activity == null || activity.isFinishing) return
            activity.progressBar.visibility = View.VISIBLE
        }

        override fun doInBackground(vararg params: Unit): InputStream {
            return URL("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf").openStream()
        }

        override fun onPostExecute(result: InputStream) {
            val activity = activityReference.get()
            if (activity == null || activity.isFinishing) return
            activity.progressBar.visibility = View.GONE
            activity.pdfView.fromStream(result).load()
        }

    }
}

srikanthPrakash avatar Oct 24 '20 17:10 srikanthPrakash

You can load pdf from stream using Executors from java.util.concurrent package like this on kotlin lang:

val executor = Executors.newSingleThreadExecutor()
val handler = Handler(Looper.getMainLooper())
val url = "https://document_url.pdf"

executor.execute {
    val input = URL(url).openStream()
    handler.post {
        pdfView.fromStream(input).load()
    }
}

Source: my answer on stackoverflow.

nusagates avatar Nov 23 '23 04:11 nusagates