Open playmans opened 6 years ago
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();
Where should I upload the PDF files to load them in my Android app from a URL?
@TuanSon The current thread is worker thread inside doInBackground you need to move "pdfView.fromStream(input).load()" code line to onPostExecute.
How can I preview the pdf url file in the firebase in to my android application?
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 can you show me how it worked for plz
@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();
}
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 notifications@github.com 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 .
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.
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 notifications@github.com 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 .
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()
}
}
}
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.
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.