129

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.

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.

Chris Saldanha
  • 1,299
  • 2
  • 9
  • 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. – FoamyGuy Sep 15 '11 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. – Robert Mar 11 '12 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 '23 at 12:29

12 Answers12

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.

buxik
  • 2,583
  • 24
  • 31
arsalank2
  • 1,683
  • 1
  • 16
  • 25
  • 4
    This was very helpful, thank you. I'm not sure it's 100% necessary, but it is probably worth mentioning that the should be urlencoded. – mason81 May 17 '12 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. – Lee Yi Hong Nov 17 '14 at 04: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 '15 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 '16 at 20:36
  • This still saves the document to your temp android directory if I recall correctly. – Jean-Paul Jul 11 '16 at 10:53
  • This is also providing download option at the right corner of the screen If You click more than 10 times It is redirection – Ravind Maurya Oct 12 '16 at 14:47
  • Is this url changes from locale to locale ? – Vishnu Prasanth G Mar 01 '18 at 14:23
  • there is a size limit in viewing pdf in google doc. is there any workaround for that? – last_fix Apr 05 '19 at 09:17
  • 2
    sorry, but this tip never worked for me, it always returns the same error "No preview available" :-( – Vitor Oct 08 '20 at 08:01
  • while I am in China, i could not reach any google services, is there a alternative way ? – KingAmo Dec 16 '20 at 11:28
  • 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. – MattBianco Nov 10 '21 at 10:30
  • The version in the comment by QFDev works for me. Does anyone have an idea to make this more convenient and quick to open a pdf using this url? (It would be great if we could somehow set chrome visiting this url with the link appended as the default action for clicking the link but that is probably asking too much.) – Kvothe Apr 04 '22 at 11:42
  • 1
    @MattBianco If the doc you are trying to preview is publicly accessible to the internet then I don't think this is a security concern. – Funyinoluwa Kashimawo Mar 04 '23 at 13:38
  • @FunyinoluwaKashimawo, even if it is not a security issue, it's building in a dependency to a third-party service that may not be available. See KingAmo's comment. – MattBianco Mar 10 '23 at 12:23
  • @MattBianco true. That was definitely a concern for me. You don't want to depend on another system you can't guarantee will always be up. – Funyinoluwa Kashimawo Mar 10 '23 at 23:20
32

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user7829578
  • 337
  • 3
  • 2
  • 1
    The perfect way to get the preview without full download. – Pratik Saluja Sep 21 '20 at 05:19
  • perfect answer . – Reza May 03 '21 at 11:05
  • 19
    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 '21 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);
Hamzeh Soboh
  • 7,572
  • 5
  • 43
  • 54
  • 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. – MattBianco Nov 10 '21 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 04 '22 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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jameson Quinn
  • 1,060
  • 15
  • 21
  • 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 04 '22 at 11:45
4

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.

  • 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 '14 at 16:35
  • Try using http://viewerjs.org/ viewer.js file looks promising because it is browser independent and relies on your server to do the heavy lifting. – user1493559 Dec 29 '16 at 06:12
  • 7
    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 '21 at 10:30
  • What links stopped working? In a particular answer? – Peter Mortensen Dec 22 '21 at 19:48
  • OK, the poster left the building in 2017. Can someone else chime in? – Peter Mortensen Dec 22 '21 at 19:49
3

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>
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.

Guilherme Oliveira
  • 2,132
  • 1
  • 14
  • 20
2

You can use this

webView.loadUrl("https://docs.google.com/viewer?url=" + "url of pdf file"); 
Akash Bisariya
  • 3,855
  • 2
  • 30
  • 42
  • can anyone please mention the reason of downvoting. – Akash Bisariya May 26 '17 at 09: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 – Siddaram H Dec 27 '17 at 06:38
  • @akash maybe u need adding more explanation. it's the rules here – huzain07 Apr 20 '20 at 09: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. – MattBianco Nov 10 '21 at 10:30
2
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);
    }
}
Piotr Labunski
  • 1,638
  • 4
  • 19
  • 26
  • 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 '21 at 10:31
1

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)");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
parvez rafi
  • 464
  • 4
  • 20
  • setPluginState ON is important, thanks mentioning it – Beeing Jk May 14 '18 at 09:07
  • @BeeingJk, `setPluginState` is deprecated from API 18 (https://stackoverflow.com/questions/19362049/setpluginsenabled-not-exist-for-webview). – CoolMind May 14 '18 at 16:15
  • @CoolMind what you showed in link is `setPluginsEnabled` rather than `setPluginState` – Beeing Jk May 15 '18 at 02:43
  • @BeeingJk, please, open the page again, press Ctrl + F, write "setPluginState" and press Enter. – CoolMind May 15 '18 at 07:17
  • @CoolMind ok thanks I also saw it here, https://developer.android.com/reference/android/webkit/WebSettings#setpluginstate, but I couldn't remove `setPluginState` else my webview won't work – Beeing Jk May 15 '18 at 07:41
  • @BeeingJk, thanks, you are right, these situatuins exist in Android (and devices). By the way, what is your API version? – CoolMind May 15 '18 at 07:57
  • check this link https://stackoverflow.com/questions/55829446/android-webview-cannot-render-the-pdf-sometimes-and-shows-blank-white-page-inste – Bolt UIX May 13 '21 at 01:28
  • 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 '21 at 10:30
  • Could you explain what you are doing here. Where would this script go? – Kvothe Apr 04 '22 at 11:48
0

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)

Ricky Levi
  • 7,298
  • 1
  • 57
  • 65
-6
  • 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);
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
unkown
  • 1,100
  • 5
  • 18
  • 39