3

I need to create functionality which allows a user to download a PDF file from my HTML5/JavaScript-based mobile application. I will be getting the PDF file as a Base 64 encoded byte stream. How can I allow the user to download the PDF to their device upon clicking a button in the page?

gariepy
  • 3,576
  • 6
  • 21
  • 34
San
  • 5,567
  • 2
  • 26
  • 28
  • I tried document.location.href = 'data:application/pdf;base64,+data; PDF is shown in iphone but it is not downloadable. It is not at all displayed in android – San Sep 10 '12 at 21:24
  • Can you set headers from the server? `Content-Disposition:attachment;filename='downloaded.pdf'` – Blake Regalia Sep 10 '12 at 22:37
  • this PDF file is from a third party API. They just send me the binary array. I saw the same answer in other forums. Is there any to set the Content-Disposition:attachment;filename='downloaded.pdf' for the "data" URI? – San Sep 10 '12 at 23:01
  • I have to implement the functionality in mobile browser. Answers given in those links works in normal browsers. – San Sep 11 '12 at 16:11
  • Another possible duplicate: http://stackoverflow.com/questions/10547255/decoding-a-base64-file-in-javascript-jquery-for-download – Scott Bartell Sep 11 '12 at 22:21
  • I am repeating it - I need a solution for MOBILE browser. – San Sep 11 '12 at 22:58
  • 2
    I am sorry. but I do not think iPhones allow users to download PDF to the device. It will only show you the PDF but it will not let you download it. the only way to let iPhone users to download your PDF is by adding your PDF file to the apple store. correct if I am wrong but that is my knowledge about the iPhone since I own on. – syrkull Sep 13 '12 at 03:47
  • Thats right shnisaka... iphone wont allow users to download PDF. Almost 80% of our customers are using Android. So i need to get the solution atleast for Android. – San Sep 13 '12 at 14:28
  • Did You find the solution ?? can you please help me? – Surendra Oct 24 '20 at 17:53

1 Answers1

0

For IOS: You can download a pdf on iPhone apps and show it in a WebView. Some ways are listed at this (linked) question. You can also find there how to put the pdf in a folder of the device/iPhone running the app: How to download PDF and store it locally on iPhone?

let request = URLRequest(url:  URL(string: "http://www.msy.com.au/Parts/PARTS.pdf")!)
let config = URLSessionConfiguration.default
let session =  URLSession(configuration: config)
let task = session.dataTask(with: request, completionHandler: {(data, response, error) in
    if error == nil{
        if let pdfData = data {
            let pathURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("\(filename).pdf")
            do {
                try pdfData.write(to: pathURL, options: .atomic)
            }catch{
                print("Error while writting")
            }

            DispatchQueue.main.async {
                self.webView.delegate = self
                self.webView.scalesPageToFit = true
                self.webView.loadRequest(URLRequest(url: pathURL))
            }
        }
    }else{
        print(error?.localizedDescription ?? "")
    }
}); task.resume()

For Android: There are many ways to do it for Android. This one should work in most cases without any issue:

 URL u = new URL("http://www.path.to/a.pdf");
    //open a connection
    HttpURLConnection c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setDoOutput(true);
    c.connect();
    FileOutputStream f = new FileOutputStream(new File(root,"file.pdf"));
    //read the file
    InputStream in = c.getInputStream();

    byte[] buffer = new byte[1024];
    int len1 = 0;
    while ( (len1 = in.read(buffer)) > 0 ) {
         f.write(buffer,0, len1);
    }
    f.close();

This other one-line option works to download a pdf in android, but seems to have different behavior depending on the device and other installed apps (pdf reader):

`startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("path/filename.pdf")));`

I used this question and it's reply to get a working code example. You can find a long answer at the bottom of the post with many different use-cases and their solutions explained at the same post.: How to download a pdf file in Android?

María Antignolo
  • 388
  • 4
  • 17