How to read pdf file from document directory in iPhone? - iphone

Currently i am working in iPhone application, i have an pdf file in resource folder (Local pdf file) then i read that pdf file (paper.pdf) successfully, below i have mentioned read local pdf file for your reference.
Example:
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("paper.pdf"), NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
Then i have tried to store pdf file (from URL) in NSDocument directory, stored successfully.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.msy.com.au/Parts/PARTS.pdf"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myPDF11.pdf"];
[pdfData writeToFile:filePath atomically:YES];
Then i tried read that pdf file (from NSDocument directory) but i didn't know that, please help me.
Thanks in Advance

its very simple please use below code to display your pdf from document directory to Webview
load PDF from Document Directory
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
webView.scalesPageToFit = YES;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myPDF11.pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
}
load PDF from Application Bundle Resource Folder
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
webView.scalesPageToFit = YES;
NSString *path = [[NSBundle mainBundle] pathForResource:#"myPDF11" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
}

Try to use this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myPDF11.pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:filePath];
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
From NSURL reference:
The NSURL class is toll-free bridged with its Core Foundation counterpart, CFURLRef. For more information on toll-free bridging, see “Toll-Free Bridging”.

Related

How to open Pdf file in different option like as iBook ,Go Daddy etc and save there in iPhone device?

In one Of my activity I have generated pdf file on WebView and save this file in document folder successfully but this file path show for simulator then get pdf file easily, but when we run my application on iPhone simulator then file show on device successfully and give file path but I can't find file where it save how to get this file open another problem I am using this code.
NSMutableData *data2 = [[NSMutableData alloc] initWithCapacity:byteArray.count];
for (NSNumber *byteVal in self.WebService->byteArray)
{
Byte b = (Byte)(byteVal.intValue);
[data2 appendBytes:&b length:1];
}
NSString *resourceToPath = [[NSString alloc]initWithString:[[[[NSBundle mainBundle]resourcePath]stringByDeletingLastPathComponent]stringByAppendingPathComponent:#"Documents"]];
fileName = [resourceToPath stringByAppendingPathComponent:#"myPDF.pdf"];
[data2 writeToFile:fileName atomically:YES];
NSURL *targetURL = [NSURL fileURLWithPath:fileName];
NSURLRequest *requestFile = [NSURLRequest requestWithURL:targetURL];
UIWebView *webView1=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
webView1.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) ;
webView1.scalesPageToFit = YES;
webView1.scrollView.scrollEnabled =NO;
[webView1 loadRequest:requestFile];
[self.view addSubview:webView1];
for download pdf file I am using this.....
-(void)DownloadPdffile
{
NSArray *docDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [docDirectories objectAtIndex:0];
NSString *filePAth = [docDirectory stringByAppendingPathComponent:#"myPDF.pdf"];
NSURL *url2 = [NSURL filePAth];
UIDocumentInteractionController *docContr = [UIDocumentInteractionController
interactionControllerWithURL:url2];
docContr.delegate=self;
[docContr presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
Using this code I've saved Pdf file in simulator successfully but in iPhone device I can't find where is my Pdf file and then using download pdf then .....this image open....!
But when I tapped any one of option then my application is closed so tell me how to open pdf in these option and save there also
If I understand what you are asking correctly, you need to refer to your documents folder differently in your -(void)DownloadPdffile method. Like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *pdfFilePath [documentsDir stringByAppendingPathComponent:yourPdfFile.pdf];// your yourPdfFile file here
NSURL *url = [NSURL fileURLWithPath:pdfPath];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
[docController retain];
// docController is released when dismissed (autorelease in the delegate method)
BOOL isValid = [docController presentOpenInMenuFromRect:yourReadPdfButton.frame inView:self.view animated:YES]; // Provide where u want to read pdf from yourReadPdfButton
(from Prince's answer)

UIDocumentInteractionController Controller issues

i want to download pdf file from webservice and open it with UIDocumentInteractionController.how can i do it? i use following code for download
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"myurl/abc.pdf"]];
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"abcd.pdf"];
pdfData writeToFile:filePath atomically:YES];
now what should i do to open it with UIDocumentInteractionController
you can open PDF in UIDocumentInteractionController using bellow code This is a example of displaying pdf from NSBundle you can also displaying from Document directory like same as we display images. You need to full path of document and convert it into NSURL using fileURLWithPath:-
In yourViewController.h file:-
#interface yourViewController : UIViewController<UIDocumentInteractionControllerDelegate>
{
UIDocumentInteractionController *documentationInteractionController;
}
In **yourViewController.m file:-**
-(void) viewDidAppear:(BOOL)animated {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"abcd.pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:filePath];
documentationInteractionController = [UIDocumentInteractionController interactionControllerWithURL:pdfUrl];
documentationInteractionController.delegate = self;
[documentationInteractionController presentPreviewAnimated:YES];
[super viewDidAppear:animated];
}
Delegate of UIDocumentInteractionController
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return self;
}
Demo link
happy coding... :)

display the PDF file in webview

i am doing one application.In that i am displaying the pdf file in webview like below
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[default1 objectForKey:#"KeyToSelectedFile"]];
NSString *filename1=[fileName stringByAppendingPathComponent:s1];
NSLog(#"%#",filename1);
NSURL *url = [NSURL fileURLWithPath:filename1];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
web=[[UIWebView alloc]initWithFrame:CGRectMake(0,0, 1020, 748)];
web.delegate=self;
[web loadRequest:request];
web.backgroundColor=[UIColor whiteColor];
[self.view addSubview:web];
But when i click open on first time it will be crashing the app and from next time onwards it's opening correctly.And at first time o got the error like
dyld: fast lazy binding from unknown image
So please tell me how to display pdf in uiwebview without crashing.
I checked this, it is working fine, try
UIWebView * pdfWebView = [[UIWebView alloc] initWithFrame:'your Frame'];
NSArray * Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * bundlePath = [[Paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.pdf",yourFileName]];
if([[NSFileManager defaultManager] fileExistsAtPath:bundlePath])
{
NSLog(#"Path : %#",bundlePath);
[pdfWebView loadData:[NSData dataWithContentsOfFile:bundlePath] MIMEType:#"application/pdf" textEncodingName:nil baseURL:nil];
[self.view addSubview:pdfWebView];
}
Try this way for your code,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[default1 objectForKey:#"KeyToSelectedFile"]];
NSString *filename1=[fileName stringByAppendingPathComponent:s1];
web=[[UIWebView alloc]initWithFrame:CGRectMake(0,0, 1020, 748)];
web.delegate=self;
web.backgroundColor=[UIColor whiteColor];
NSData *data = [NSData dataWithContentsOfFile:filename1];
[web loadData:data MIMEType:#"application/pdf" textEncodingName:#"UTF-8" baseURL:url];
[self.view addSubview:web];
NSData *data = [NSData dataWithContentsOfFile: filename1];
[web loadData:data MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
web.backgroundColor=[UIColor whiteColor];
[self.view addSubview:web];
This will work perfect.
UIWebView * webview = [[UIWebView alloc] initWithFrame:Frmaesize
NSString * path = [[NSBundle mainBundle] pathForResource:#"Filename" ofType:#"pdf"];
NSURL * URL = [NSURL path];
NSURLRequest * request = [NSURLRequest requestWithURL:URL];
[webview loadRequest:request];
[self.view addsubView:webview];

UIWebView displays locally stored document on simulator but not device

I have an issue where I'm trying to view a document (Word document, image, or video) that I have stored locally. I'm using UIWebView and on the Simulator it works perfectly, but on the device, it's a blank screen (no errors thrown in console). Here's my code to view the document:
UIWebView *newWebView;
if (appDelegate.IS_IPAD)
{
newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
}
else
{
newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
}
newWebView.scalesPageToFit = YES;
[self setWebView: newWebView];
[newWebView release], newWebView = nil;
[[self webView] setDelegate: self];
[[self view] addSubview:[self webView]];
NSURL *nsURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
[[self webView] loadRequest:[NSURLRequest requestWithURL:nsURL]];
Here's how I'm saving the document locally before viewing. I do think the file is getting successfully saved on the device as it does on the simulator.
// Save the file on the device
NSURL *contentUrl = [NSURL URLWithString:selectedContent.contentUrl];
NSString *fileName = [[contentUrl absoluteString] lastPathComponent];
NSString *homeDirectoryPath = NSHomeDirectory(); // Create the path
NSString *unexpandedPath = [homeDirectoryPath stringByAppendingString:#"/MyApp/"];
NSString *folderPath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedPath stringByExpandingTildeInPath]], nil]];
NSString *unexpandedImagePath = [folderPath stringByAppendingFormat:#"/%#", fileName];
NSString *filePath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedImagePath stringByExpandingTildeInPath]], nil]];
if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath isDirectory:NULL])
{
[[NSFileManager defaultManager] createDirectoryAtPath:nil withIntermediateDirectories:NO attributes:nil error:nil];
}
// Do the actual write of the file to the device
NSData *fileData = [NSData dataWithContentsOfURL:myUrl]; // Create the file data reference
[fileData writeToFile:filePath atomically:YES]; //Save the document to the home directory
Watch out for case issues. The simulator is not case sensitive whereas the iPhone is!
I solved this by saving my document to the default Documents Directory. Here's the code I used to accomplish this:
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [arrayPaths objectAtIndex:0];
NSString *filePath = [docDirectory stringByAppendingString:#"/"];
filePath = [filePath stringByAppendingString:fileName];
NSData *fileData = [NSData dataWithContentsOfURL:myUrl];
[fileData writeToFile:filePath atomically:YES];

Open downloaded files in webview in iphone

I am new to iphone development. I am developing an iphone application in which i am downloading files using web service with following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:#"/filename.pdf"];
NSData *fileData = [self decodeBase64WithString:soapResults];
[fileData writeToFile:fileLoc atomically:YES];
And my file is in path : /Users/myusername/Library/Application Support/iPhone Simulator/4.0/Applications/7A627E64-DEAB-40FA-BE04-35ED50580B65/filename.pdf
How can i open this file using uiwebview??? I tried this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:#"/filename"];
[self loadDocument:fileLoc inView:mywebview];
My loadDocument method is:
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
But it showing error... I think location is the problem.. How can i find the path in simulator as well as in orignal device???
Please help me...
pathForResource:ofType: can only be used to load resource files, which are shipped as part of your application bundle. You're trying to load a downloaded file from your Documents directory. So you should be able to just do
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:#"/filename.pdf"];
NSURL *url = [NSURL fileURLWithPath:fileLoc];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
What is the error you are getting? What does your loadDocument method look like? You should load the document into the WebView something like this:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fileLoc]]];