How to load a pdf from the url in IPhone? - iphone

I have an application in which i am loading a pdf from the bundle by using
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("hhhh.pdf"), NULL, NULL);.
Now i want to load it from my web server. I was done it with a string url using
NSString *strUlr = [NSString stringWithFormat:#"https://s3.amazonaws.com/hgjgj.pdf"];
CFStringRef aCFString = (CFStringRef)strUlr;
//CFURLRef pdfURL = aCFString;
// NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(),aCFString, NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
NSLog(#"%#",pdf);
//CFRelease(pdfURL);
but with no success..Can anybody knows how to make this code working.I dont need to load it in to pdf because of some reasons.Can anybody help me?

You still try to get url to resource that is supposed to be in your application bundle, not on remote server. Correct method to create url will be CFURLCreateWithString:
CFURLRef pdfURL = CFURLCreateWithString(NULL, aCFString, NULL);
Keep in mind that it may be better to download your pdf and cache it locally before display - that approach will also give you more flexibility in error handling and probable authentication challenges for your url

For reading purpose only you can load pdf file in Uiwebview by url.

If i am not mistaken then you probably want to read pdf from web server in your iPhone, if so then you can have UIWebView for your requirements: Here's how you can read and store it locally :
First create UIWebView and include <<UIWebViewDelegate>>
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"YourPDF.pdf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
// download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"https://s3.amazonaws.com/hgjgj.pdf"]];
//Store the downloaded file in documents directory as a NSData format
[pdfData writeToFile:filePath atomically:YES];
}
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];

Related

Objective-c: Downloading a PDF file

I am trying to eneble a button to download a pdf file from a server.
(Onto the divice)
I'm not being successful with this code (to post this question, i've changed the address)
Would you give me some advice to make it work..?
Thanks in advance.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.mydomain.com/mypdffile.pdf"]];
//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"<Application_Home>/Documents/"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];
//Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView loadRequest:requestObj];
You are not building a correct NSURL object to reach a server. fileURLWithPath: is a method to build URL that points to files in the file system.
Use URLWithString: writing the complete url, that is for example "http://myserver.com/myfile.pdf"

How to fetch pdf file in Controller file in iphone

I have to read pdf file from my controller. How to load it ?
Thanks
You could use UIWebView to view the pdf file.
Check the below SO post.
Display pdf from the weburl iphone sdk
Tutorial link1
From apple
Using UIWebView to display select document types
Here's how you can read and store your pdf locally:
First create UIWebView and include <<UIWebViewDelegate>>
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"YourPDF.pdf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
// download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"https://s3.amazonaws.com/hgjgj.pdf"]];
//Store the downloaded file in documents directory as a NSData format
[pdfData writeToFile:filePath atomically:YES];
}
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];
Or, if you simply want to read/load pdf from your Resource folder then simply do this :
NSString* filePath= [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"pdf"]];
/// or you can even read docs file as : pathForResource:#"sample" ofType:#"docx"]
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];

iOS download and save HTML file

I am trying to download a webpage (html) then display the local html that has been download in a UIWebView.
This is what I have tried -
NSString *stringURL = #"url to file";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"index.html"];
[urlData writeToFile:filePath atomically:YES];
}
//Load the request in the UIWebView.
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]]; // Do any additional setup after loading the view from its nib.
But this gives a 'SIGABRT' error.
Not too sure what I have done wrong?
Any help would be appreciated. Thanks!
The path passed to the UIWebView is incorrect, like Freerunnering mentioned, try this instead:
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:#"http://www.google.nl"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
// Load file in UIWebView
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
Note: Correct error-handling needs to be added.
NSDocumentDirectory will be backed up to iCloud. You might be better off using NSCachesDirectory https://developer.apple.com/library/ios/#qa/qa1719/_index.html
Your app is a folder with a .app extension on the end. On the iPhone once it is installed you can't change this folder, hence why you save it to the documents directory.
In your example code you save the file to Documents/index.html and ask it to load appname.app/index.html.
[NSBundle mainBundle] doesn't give you the documents directory it gives you (i think) the .app folder (though it might be the folder that contains the app, documents, and other folders).
To give you'll want to change this line.
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
To (providing this is the same method as the rest of the code, else recreate the object 'filePath')
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath isDirectory:NO]]];
Here is the Swift 2.x version:
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var filePath: String = "\(paths[0])/\("index.html")"
// Download and write to file
var url: NSURL = NSURL(string: "http://www.google.nl")!
var urlData: NSData = NSData.dataWithContentsOfURL(url)
urlData.writeToFile(filePath, atomically: true)
// Load file in UIWebView
web.loadRequest(NSURLRequest(URL: NSURL.fileURLWithPath(filePath)))

Creating a PDF programmatically on the iPhone?

Hey all, was wondering if anyone could point me to the right direction. I'm curious about how I would go about creating a PDF programmatically on the iPhone. The documentation mentions CGPDFDocumentCreateWithProvider but i'm not sure how to go about using it. Thanks!
Likewise I do not believe it is possible to view a PDF on the iPhone without an external service to render the output
This is incorrect. You can render a PDF document within a UIWebView instance, for example:
NSString *urlPathStr = [[NSBundle mainBundle] pathForResource:#"myPDFFile" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:urlPathStr];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:urlRequest];
You can also render a PDF within a UIView object for better performance.
The PDFKit library is not yet available for the iPhone.
You might look at the QuartzDemo sample application for some Quartz2D methods for rendering and manipulating a PDF document.
CGPDFDocumentCreateWithProvider or CGPDFDocumentCreateWithURL are the methods you have to use.
i tried this methode CGPDFDocumentCreateWithURL.it will create a pdf in the url that you have provided to the methode as first parameter.
Once created ,,load the url in the web view to view the pdf.
you can enter content in pdf using cgcontextRef class.
Here's how you can read and store your pdf locally in iphone application:
First create UIWebView and include <<UIWebViewDelegate>>
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"YourPDF.pdf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
// download file , here "https://s3.amazonaws.com/hgjgj.pdf" = pdf downloading link
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"https://s3.amazonaws.com/hgjgj.pdf"]];
//Store the downloaded file in documents directory as a NSData format
[pdfData writeToFile:filePath atomically:YES];
}
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];
Or, if you simply want to read/load pdf from your Resource folder then simply do this :
NSString* filePath= [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"pdf"]];
/// or you can even read docs file as : pathForResource:#"sample" ofType:#"docx"]
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[yourWebView setUserInteractionEnabled:YES];
[yourWebView setDelegate:self];
yourWebView.scalesPageToFit = YES;
[yourWebView loadRequest:requestObj];
Or, if you want to create your own pdf from bunch of text and strings,here's how i did it:
http://iosameer.blogspot.com/2012/09/creating-pdf-file-from-simple.html

How to download an .html file for local use?

I am trying to download an HTML file from the internet and store it in my app for later (offline) viewing. I use the following method to download the file but my NSLogs report that no data has been downloaded. Whats the problem here? (assume an active internet connection, assume a pre-defined path to local documents directory)
urlAddress = #"http://subdomain.somesite.com/profile.html";
// Create and escape the URL for the fetch
NSString *URLString = [NSString stringWithFormat:#"%#%#", #"ttp://subdomain.somesite.com/profile.html"];
NSURL *URL = [NSURL URLWithString:
[URLString stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding]];
// Do the fetch - blocks!
NSData *theData = [NSData dataWithContentsOfURL:URL];
if(theData == nil) {
NSLog(#"NO DATA DOWNLOADED");
}
// Do the write
NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:#"Profile/profile.html"];
[theData writeToFile:filePath atomically:YES];
NSLog(#"WRITING profile.html to path %#!", filePath);
Because you shouldn't do the stringByAddingPercentEscapesUsingEncoding: stuff, which turn the URL into an invalid one http%3a%2f%2f....
Directly use
NSData *theData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:#"http://subdomain.somesite.com/profile.html"]];