1

How to download a PDF file when a button is clicked?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user973067
  • 327
  • 2
  • 10
  • 20
  • Is the PDF available at a specific URI (e.g. http://www.example.com/foo.pdf)? – Brian Driscoll Jan 31 '12 at 15:24
  • yes it is.i have a long url for the pdf file. – user973067 Jan 31 '12 at 15:27
  • Ok, and the assumption is that the file will download and open within your app? Or, can it open in another app on the device (such as Adobe Reader or Preview)? – Brian Driscoll Jan 31 '12 at 15:30
  • Also, this is a possible duplicate of [Open a PDF document on iPhone](http://stackoverflow.com/questions/3950313/open-a-pdf-document-on-iphone). Did you search StackOverflow before you posted this question? – Brian Driscoll Jan 31 '12 at 15:32
  • it needs to download a file. it doesn't have to open it but it would be good if it does both opening and downloading. i am using webview to open it... – user973067 Jan 31 '12 at 15:33
  • Why not just point your Webview at the URL? That's really all you need to do. – Brian Driscoll Jan 31 '12 at 15:34
  • Possible duplicate of [How to download PDF and store it locally on iPhone?](https://stackoverflow.com/questions/2226615/how-to-download-pdf-and-store-it-locally-on-iphone) – Cœur Jan 28 '19 at 07:36

1 Answers1

3

Check out these posts:

iPhone : can we open pdf file using UIWebView?

Display PDF in UIWebView using loadData

How do I make UIWebView show a pdf file from its own app?

Basically, you save the pdf to your sandbox and then use a UIWebview to open the pdf and display it to the user. The posts above have a ton of sample code for you to check out and should get you started. Good luck!

UPDATE

also check out: How to download PDF and store it locally on iPhone?

This post has a bit about saving pdf's from an URL to disk. One thing I would add to this post is to NOT download a file in the main thread but rather save it in another thread.

EXPLORING THE APP SANDBOX

In response to the comment below: You can explore the application documents using the mac terminal application. If you have the application installed on the iPhone simulator use terminal to look through the contents of the sandbox. The path to your application will be something like: ~/Library/Application Support/iPhone Simulator/user/Applications/<GUID>

Check out this Apple Doc for how the filesystem works for iOS and for more information about where to store your application data: http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW2

Community
  • 1
  • 1
Joey J
  • 1,355
  • 10
  • 26
  • Hi, i tried them and successfully displayed on a webview, but i can not find the pdf file on the device. i downloaded a pdf reader but it doesnt find it... – user973067 Jan 31 '12 at 15:55
  • the application can only see documents within it's own application sandbox so it makes sense another stand-alone program won't see the PDF you downloaded using your program. If you are using the iPhone simulator you can explore the sandbox by using terminal. See my comments above. – Joey J Jan 31 '12 at 16:46
  • Hi, thanks for your comments. so i should not use this code (from 'iPhone: Download PDF and store locally?') then... what i need is to download a pdf file to read it locally. maybe i should abundon it and try emailing the pdf file to the client. – user973067 Jan 31 '12 at 16:54
  • Well, one of the restrictions (which is what I think is giving you problems) is that you cannot access your application data (in this case a PDF) from another application. So if your program allows you download and view the file you are not able to use another application to view that same file. Can you clarify exactly what you are trying to do with the downloaded file? – Joey J Jan 31 '12 at 16:57
  • Yes, what i need to do is to download a PDF file, and anable the user to save it(ex.onto their sdcard). so they can read it on anything(pc, tablet, etc) – user973067 Jan 31 '12 at 17:05
  • Ok, now I understand. No, to my knowledge (not saying it isn't possible but from what I know) it isn't possible to have the user access your file directly. Your program would have to provide the file to the user somehow - for example, the photo app allows you to e-mail photos. You could try to build a similar functionality into your application too. – Joey J Jan 31 '12 at 17:21
  • Thannks for your comment. I will read Apples document and talk to my work corregue about how to get around it. thanks very much. – user973067 Jan 31 '12 at 18:31