0

I am trying to download a PDF file with the following code:

 try {
                URL url = new URL(urls[0]);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.connect();

                if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                    return "Server returned HTTP " + conn.getResponseCode() + " "
                            + conn.getResponseMessage();
                }

                //Useful to display progress
                int fileLength = conn.getContentLength();


                //Download the mFile
                InputStream input = new BufferedInputStream(conn.getInputStream());


                //Create a temp file cf. https://developer.android.com/training/data-storage/files.html
//                mFile = File.createTempFile(FILENAME, "pdf", mContext.getCacheDir());
                mFile = new File(getFilesDir(), "temp.pdf");
                FileOutputStream fos = openFileOutput("temp.pdf",MODE_PRIVATE);




                byte[] buffer = new byte[10240];
                long total = 0;
                int count;
                while ((count = input.read(buffer)) != -1) {
                    if (isCancelled()) {
                        input.close();
                        return null;
                    }
                    total += count;

                    //Publish the progress
                    if (fileLength > 0) {
                        publishProgress((int) (total * 100 / fileLength));
                    }
                    fos.write(buffer);

                }
                Log.i(LOG_TAG, "File path: " + mFile.getPath());



                fos.flush();
                fos.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

Next, I want to render the downloaded file using PdfRenderer. Each time I pass the File object created using the above code to ParcelFileDescriptor.open() from the PdfRenderer class, I receive "Exception: file not in PDF format or corrupted".
The rendering code does the following to the received File object to create a PdfRenderer:

 mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
            // This is the PdfRenderer we use to render the PDF.
            mPdfRenderer = new PdfRenderer(mFileDescriptor);

How can I solve this? I have tried many options like creating temporary files with createTempFile and many StackOverFlow posts, but all I tried has failed. Does anyone know what my problem is caused by?

Mehdi Haghgoo
  • 3,144
  • 7
  • 46
  • 91
  • 1
    I recommend that you try MuPDF, this is my opinion though. When I last used PDF renderer, it ran into strange issues with some files and large files too, but with MuPDF, so far so good – Clement Osei Tano Dec 24 '17 at 13:59
  • First, note that `PdfRenderer` cannot render arbitrary PDF files. It is there for print preview only. Use `FileProvider` and allow the user to view the document in their preferred PDF reader. Or, use [other PDF rendering options](https://commonsware.com/blog/2017/01/04/options-viewing-pdfs.html). Beyond that, note that you are downloading to a `File` identified as `mFile`, but your `ParcelFileDescriptor.open()` call is using `file`. Perhaps `mFile` and `file` are not pointing at the same spot. – CommonsWare Dec 24 '17 at 14:00
  • @CoolGuyCG did you compile MuPDF with NDK? As far as I can see there is a lot of work in applying MuPDF source code to an Android app. Am I wrong? – Mehdi Haghgoo Dec 24 '17 at 17:46
  • 1
    Yes please, it may seem to be a lot of work, but it's quite easy if you follow the instructions given – Clement Osei Tano Dec 24 '17 at 19:24

1 Answers1

0

First you need to create a file in Android and open an output file stream to that:

        mFile = new File(getFilesDir(), "temp.pdf");
        FileOutputStream fos = openFileOutput("temp.pdf",MODE_PRIVATE);

Next, there is a pretty common Java glitch you will want to avoid when writing to the output stream (courtesy of this post) and change the stream writer to:

            fos.write(buffer, 0, count);
Mehdi Haghgoo
  • 3,144
  • 7
  • 46
  • 91