2

I made an android app for viewing Pdf file fetched from URL by integrating pdfViewer library in my code.Firstly app downloading the file from web to external sd card then from there the app is getting opened with PdfViewer library.It is working fine if the file size is small but if the pdf file contains images and size is more , the downloaded file size shown in sdcard is 0kb. Can someone help me out why this is so?

Following is java code :

public class MainActivity extends Activity {
    static Context applicationContext;

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

        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        File folder = new File(extStorageDirectory, "pdfDownloads");
        folder.mkdir();
        File file = new File(folder, "android.pdf");
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        boolean downloadFile = downloadFile("http://www.irs.gov/pub/irs-pdf/fw4.pdf", file);
        if (file!=null && file.exists() && file.length() > 0){
            Intent intent = new Intent(this, com.example.soniapdf.Second.class);
            intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,
                    file.getAbsolutePath());
            startActivity(intent);
        }
    }

    public static boolean downloadFile(String fileUrl, File directory) {
        try {
            FileOutputStream f = new FileOutputStream(directory);
            URL u = new URL(fileUrl);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
            InputStream in = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len = 0;

            //
             int fileLength = c.getContentLength();
             long total = 0;
            //
                    Toast.makeText(applicationContext, "Downloading PDF...", 2000).show();


            while ((len = in.read(buffer)) > 0) {
                total += len;


            //Toast.makeText(applicationContext, "Downloading PDF: remaining " + (fileLength / total  )+ "%", 1).show();
                f.write(buffer, 0, len);
            }
            f.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}
sonia
  • 405
  • 2
  • 7
  • 23
  • is there any exception? – Praful Bhatnagar Feb 19 '13 at 09:25
  • 1
    you are creating the file before getting the success from the server... You should first make the http connection check the response code.. if the response code is 200.. then open the `FileOutputStream` and write the data... With your current code if there is any error while making the network connection, an empty file would be created... – Praful Bhatnagar Feb 19 '13 at 09:28
  • @PrafulBhatnagar can u help me with the code ? how can i make the http connection check the response code? – sonia Feb 19 '13 at 09:42

1 Answers1

2

This is a way for showing PDF in android app that is embedding the PDF document to android webview using support from http://docs.google.com/viewer

pseudo

String doc="<iframe src='http://docs.google.com/viewer?url=+location to your PDF File+' 
          width='100%' height='100%' 
          style='border: none;'></iframe>";

a sample is is shown below

String doc="<iframe src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true' 
          width='100%' height='100%' 
          style='border: none;'></iframe>";

Code

WebView  wv = (WebView)findViewById(R.id.webView); 
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
wv.getSettings().setAllowFileAccess(true);
wv.loadUrl(doc);
//wv.loadData( doc, "text/html",  "UTF-8");

and in manifest provide

<uses-permission android:name="android.permission.INTERNET"/>

SEE THIS ANSWER

EDIT

If your PDF document is accessible online, use the Google Docs Viewer to open your PDF in a WebView

REFER

 wv.loadUrl("https://docs.google.com/gview?embedded=true&url=http://www.irs.gov/pub/irs-pdf/fw4.pdf");

Don't Know how Stable These are

Here is the list of the other open sources PDF readers running on the top of the Android

Please note that these and any other project derived from MuPDF is bound by the terms of GPL and may not be suitable for the commerical use.

The following is a list of SDKs suitable for commerical use:

Community
  • 1
  • 1
edwin
  • 7,985
  • 10
  • 51
  • 82
  • can u send me the custom library link again? – sonia Feb 19 '13 at 09:45
  • @Rockin podappa http://stackoverflow.com/questions/14578530/how-to-open-display-documents-pdf-doc-without-external-app/14578661#14578661 – edwin Dec 09 '14 at 21:59
  • I used above code. It is working fine, I can show pdf file. But I need to copy text from this file. Please help me how I can achieve this. – Anurag Srivastava Nov 03 '16 at 14:39
  • I don't how this give a response of the main question, how to do it with pdfViewer? I think he's not asking how make it with a webview – Fernando Torres Dec 18 '19 at 02:27