130

Is there a way to get the stock Android browser to auto-open a PDF, Word or other typical file without having to go through the process of downloading the file and then getting the user to open the file from the Downloads app or the Notification bar?

We have a web application that has a lot of documents that we'd like to include and not have to convert to HTML, but making the user download the file and manually open it is not easy to train users on. Android PDF Rendering

On iOS, these files all display inline in the browser. I'd like a way to get the browser to auto-launch the files into Acrobat Reader or QuickOffice or whatever program the user has to display them.

Does anyone know a way to do that? I know that Google Docs has some PDF viewing support, but people using our web app may not have public Internet access in all cases, and may be hitting on a local web server.

3
  • 1
    I've never seen it work that way. That being said, I would imagine you could create your own browser that is capable of decoding and properly displaying pdf files. I just don't believe any of the popular browsers support this. Use your Android device to read your PDFs wherever you are and whenever you want. Learn how to open PDF files on an Android with a PDF reader.
    – FoamyGuy
    Sep 15, 2011 at 23:30
  • 1
    Are your PDF files optimized for "Fast Web View"? If not they can not be displayed while download is still in progress - hence they can only be downloaded and then displayed. how to integrate offline dictionary with pdf reader in android
    – Robert
    Mar 11, 2012 at 15:59
  • If the browser is not handling Content-Disposition:inline and similar headers properly, try serving it through viewerjs or similar javascript PDF renderer. (instead of serving the pdf, serve an iframe that loads a viewer that fetches the pdf).
    – MattBianco
    Mar 10, 2023 at 12:29

12 Answers 12

85

You can open a file PDF in Google Docs Viewer by appending the URL to:

https://docs.google.com/gview?embedded=true&url=<URL of a supported doc>

This would open a PDF file in the default browser or a WebView.

A list of supported formats is given here.

15
  • 4
    This was very helpful, thank you. I'm not sure it's 100% necessary, but it is probably worth mentioning that the <url of a supported doc> should be urlencoded. PDF reader: The original PDF solution | Adobe Acrobat Reader
    – mason81
    May 17, 2012 at 18:59
  • 2
    This doesn't work for me... It just display a page "Oops! There was a problem previewing this document" with a download button. There is nothing happens when you click on the download button. Nov 17, 2014 at 4:41
  • 35
    The above link failed to work for Chrome on Android, I switched to https://drive.google.com/viewerng/viewer?embedded=true&url=#### and that did the job!
    – QFDev
    Jan 12, 2015 at 15:31
  • 2
    Hi, it seems the url in the answer no longer works . I get an infinite loop with it now in webView listener. The url posted in comment from QFDev is working.
    – Gene Bo
    Jun 10, 2016 at 20:36
  • 9
    This is a WORKAROUND that requires google obtaining a copy of the document (and making the document publicly accessible over the Internet). Not really an answer to the question, and absolutely devastating from a security/privacy point of view. r/Calibre on Reddit: A Good Free Text To Speech Solution For eBooks
    – MattBianco
    Nov 10, 2021 at 10:30
31

You can use this format as of 2017-04-06.

https://docs.google.com/viewerng/viewer?url=http://yourfile.pdf

Just replace http://yourfile.pdf with the link you use.

3
  • 1
    The perfect way to get the preview without full download. Sep 21, 2020 at 5:19
  • perfect answer .
    – Reza
    May 3, 2021 at 11:05
  • 22
    This is a WORKAROUND that requires google obtaining a copy of the document (and making the document publicly accessible over the Internet). Not really an answer to the question, and absolutely devastating from a security/privacy point of view.
    – MattBianco
    Nov 10, 2021 at 10:30
6
String format = "https://drive.google.com/viewerng/viewer?embedded=true&url=%s";
String fullPath = String.format(Locale.ENGLISH, format, "PDF_URL_HERE");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
startActivity(browserIntent);
2
  • 9
    This is a WORKAROUND that requires google obtaining a copy of the document (and making the document publicly accessible over the Internet). Not really an answer to the question, and absolutely devastating from a security/privacy point of view.
    – MattBianco
    Nov 10, 2021 at 10:30
  • Could you expand on what you are doing here? Are you automating opening the pdf with the google drive link? Where is this script meant to go?
    – Kvothe
    Apr 4, 2022 at 11:44
5

Specifically, to install the pdf.js plugin for Firefox, you do not use the app store. Instead, go to addons.mozilla.org from inside Mozilla and install it from there.

Also, to see if it's installed properly, go to the menu ToolsAdd-ons (not the "about:plugins" URL as you might think from the desktop version).

1
  • 1
    Are you saying this is possible on android?As far as I know there are only a few Firefox plugins that can be installed on android and this one is not one of them.
    – Kvothe
    Apr 4, 2022 at 11:45
4

Try this code This worked for me.

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;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_pdf);
   
        pdf = (PDFView) findViewById(R.id.pdfView); //github.barteksc
        txt = findViewById(R.id.txtPdf);
        String pdfUrl = "https://www.rkiinstruments.com/pdf/71-0136RK.pdf";
        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();
        }
    }

}

add library in build.gradle

 implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'
 implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

create activity_custom_pdf.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        tools:context=".activity.PdfViewer">
    
        <com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdfView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
        <TextView
            android:id="@+id/txtPdf"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"/>
    </RelativeLayout>
2
  • 1
    It uses android-pdf-viewer library and AsyncTask. :)
    – CoolMind
    Apr 22, 2022 at 8:09
  • It would work (much better than the other answers with Google Drive??), but it also increase APK size by 16 MB Apr 5 at 10:14
3

Unfortunately the native browser present on Android devices not support this type of file. Let's see if in the 4.0 we will be able to do that.

3

I needed this too, and the links above stopped working so this is what I found to work with the New Google Drive:

Google has a service that creates the link for PDF's Not in GDrive: https://docs.google.com/viewer Just add your URL and it creates a link, and IFrame code (Look closely and you will see the pattern and create links without this web service)

Also, there is a way to do it for PDF's stored in Google Drive: https://docs.google.com/viewer?srcid=YOUR_GDRIVE_PDF_DOC_ID_HERE&pid=explorer&efh=false&a=v&chrome=false&embedded=true (this can be a link or the src URL of an iframe)

I've tested on Android and it brings up the PDF viewer nicely.

5
  • For me, if I am not currently signed in with a google account, then it takes me to a log in page. Is there a way to bypass this and just view the pdf?
    – h_k
    Sep 18, 2014 at 16:35
  • Try using viewerjs.org viewer.js file looks promising because it is browser independent and relies on your server to do the heavy lifting. Dec 29, 2016 at 6:12
  • 8
    This is a WORKAROUND that requires google obtaining a copy of the document (and making the document publicly accessible over the Internet). Not really an answer to the question, and absolutely devastating from a security/privacy point of view. Posted by u/montysucks - 68 votes and 66 comments
    – MattBianco
    Nov 10, 2021 at 10:30
  • What links stopped working? In a particular answer? Dec 22, 2021 at 19:48
  • OK, the poster left the building in 2017. Can someone else chime in? Dec 22, 2021 at 19:49
2

You can use this

webView.loadUrl("https://docs.google.com/viewer?url=" + "url of pdf file"); 
4
  • can anyone please mention the reason of downvoting. May 26, 2017 at 9:14
  • May be, your answer holds good for some programming technique, and the answer could be just the URL, even I can add it into an anchor tag, which is already mentioned above VeryPDF Java PDF Reader is a free document viewer, it includes printing, saving, text search, forms, digital signatures, and annotations features. The PDF ...
    – Siddaram H
    Dec 27, 2017 at 6:38
  • @akash maybe u need adding more explanation. it's the rules here
    – huzain07
    Apr 20, 2020 at 9:37
  • 6
    This is a WORKAROUND that requires google obtaining a copy of the document (and making the document publicly accessible over the Internet). Not really an answer to the question, and absolutely devastating from a security/privacy point of view. Open PDFs on Android: Using PDF readers for Android | Adobe Acrobat
    – MattBianco
    Nov 10, 2021 at 10:30
1
public class MainActivity extends AppCompatActivity {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openURL("http://docs.google.com/viewer?url=" + " your pdf link ");
            }
        });
    }

    private void openURL(String s) {
        Uri uri = Uri.parse(s);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri,"text/html");
        startActivity(intent);
    }
}
1
  • 4
    This is a WORKAROUND that requires google obtaining a copy of the document (and making the document publicly accessible over the Internet). Not really an answer to the question, and absolutely devastating from a security/privacy point of view.
    – MattBianco
    Nov 10, 2021 at 10:31
1

I'm using PDF.js here:

They have two demo links to first verify that it's actually working mobile ( for it does )

I found that when using <object data="${pdf}" width="100%" height="100%" type="application/pdf"> Firefox actually using PDF.js in the background but version 3.4 - which didn't work for me on mobile.

When using the latest PDF.js from the GitHub link, it's using version 3.5

But the reason I'm writing this answer, is actually because I found a very simple way of using PDF.js w/o all this Javascript, implemented the following <iframe> tag according to this document

<iframe
      src="/web/viewer.html?file=/path/to/server/document.pdf"
      width="1000px"
      height="1000px"
      style="border: none"></iframe>

Ofc I had to edit the links inside viewer.html to match my personal server structure, but it's working great for me on both desktop ( Chrome / Firefox ) and mobile Android (Brave)

0

Try the following. It worked for me.

WebView view = (WebView) findViewById(R.id.yourWebView);

view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setPluginState(WebSettings.PluginState.ON);
view.loadUrl("http://docs.google.com/gview?embedded=true&url="
              +"your document link(pdf,doc,docx...etc)");
9
  • setPluginState ON is important, thanks mentioning it
    – Beeing Jk
    May 14, 2018 at 9:07
  • @BeeingJk, setPluginState is deprecated from API 18 (anandihospital.com/questions/19362049/…).
    – CoolMind
    May 14, 2018 at 16:15
  • @CoolMind what you showed in link is setPluginsEnabled rather than setPluginState
    – Beeing Jk
    May 15, 2018 at 2:43
  • @BeeingJk, please, open the page again, press Ctrl + F, write "setPluginState" and press Enter.
    – CoolMind
    May 15, 2018 at 7:17
  • 4
    This is a WORKAROUND that requires google obtaining a copy of the document (and making the document publicly accessible over the Internet). Not really an answer to the question, and absolutely devastating from a security/privacy point of view. Enjoy the best free PDF reader with Adobe. Acrobat Reader lets you read, sign, comment, and interact with any type of PDF file.
    – MattBianco
    Nov 10, 2021 at 10:30
-7
  • Download the Acrobat Reader .apk file for Android to display PDF files

  • Put your PDF files on an SD card

  • Use the snippet of code below

     File file = new File("/sdcard/test.pdf");
     Uri path = Uri.fromFile(file);
     Intent i = new Intent(Intent.ACTION_VIEW);
     i.setDataAndType(path,"application/pdf");
     i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     startActivity(i);
    
1
  • 5
    This is for a web browser, not a native android application. -1 May 19, 2014 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.