I want to display MSOffice extensions in webview. Is this possible?
I've tried this:
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath
stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
// Now create Request for the file that was saved in your documents folder
//UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 250, self.view.frame.size.width, 300)];
NSURL *targetURL = [NSURL URLWithString:fileurl];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
But I get the message:
"An error occured while reading the document".
Try below if your ppt file is drag dropped in your project
[webView loadRequest:[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"FileName" ofType:#"ppt"]isDirectory:NO]]];
If you are getting your file from server in form of NSData then use below
NSURL *url=[NSURL URLWithString:[#"yourURL" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData* responseData = [NSData dataWithContentsOfURL: url];
[webView loadData:responseData MIMEType:#"application/ppt"
textEncodingName:#"utf-8" baseURL:[NSURL URLWithString:#""]];
Related
I have the pdf file in local resource folder i want to open this file using UIWebView and user can read. But the PDF file is not displaying to read.
NSString *pathToBundle = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:pathToBundle];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"Insupen_User_Guide" ofType:#"pdf"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
//CGRect fullScreenRect=[[UIScreen mainScreen]applicationFrame];
catalogue=[[UIWebView alloc]initWithFrame:CGRectMake(50,400,600,600)];
[catalogue loadHTMLString:htmlString baseURL:baseURL];
catalogue.delegate=self;
[self.view addSubview:catalogue];
try this.
NSString *strPath = [[NSBundle mainBundle]pathForResource:#"Insupen_User_Guide" ofType:#"pdf"];
[catalogue loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[strPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
maybe this will help you.
NSString *path = [[NSBundle mainBundle]pathForResource:#"HR" ofType:#"pdf"];
NSURL *targeturl = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targeturl];
// _webview = [[UIWebView alloc]init];
[[_webview scrollView] setContentOffset:CGPointMake(0, 500) animated:YES];
[_webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0.0, 50.0)"]];
[_webview loadRequest:request];
// worked for me perfectly you can try this
In the code you posted you are turning the contents of the PDF file into a UTF8 encoded string and displaying the string inside the web view, this is not what you want.
Instead, you need to make a NSURLRequest pointing to the local PDF and load the request in the web view like so
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:#"Insupen_User_Guide" ofType:#"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pdfPath];
NSURLRequest *pdfUrlRequest = [NSURLRequest requestWithURL:pdfUrl];
[catalogue loadRequest:pdfUrlRequest];
You can also display PDF in UIDocumentInteractionController using this.
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"PDF file name" ofType:#"pdf"]]];
docController.delegate = self;
//[DocView addSubview:docController];
[docController presentPreviewAnimated:YES];
And add these delegate methods in your class.
#pragma mark documentInteractionController methods
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller {
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller {
return self.view.frame;
}
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(SEL)action{
return FALSE;
}
Hello,
How would I retrieve an NSUrl's PDF locally from a NSDictionary. I save it like so:
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
NSURL URLWithString:#"http://www.example.com/info.pdf"]];
// Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[
[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
stringByAppendingPathComponent:#"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 setDelegate:self];
[webView loadRequest:requestObj];
But how would I retrieve the file and display it in a UIWebView??
Cheers,
SebOH
Try this solution. Once you downloaded the file you don't need to download every time. you can access the directory where the file has been stored.
// Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[
[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"Documents"
]];
NSString *filePath = [resourceDocPath
stringByAppendingPathComponent:#"myPDF.pdf"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
NSURL URLWithString:#"http://www.example.com/info.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 setDelegate:self];
[webView loadRequest:requestObj];
You are doing the exact thing, "retrieve the file and display it in a UIWebView??" using the below code
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setDelegate:self];
[webView loadRequest:requestObj];
Is there some problem you are facing with the above lines of code, is the PDF loading in the webview?
So if you don't want to download the file every time do this check,
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
//download the file
}
So the whole solution would be
// Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[
[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
stringByAppendingPathComponent:#"Documents"
]];
NSString *filePath = [resourceDocPath
stringByAppendingPathComponent:#"myPDF.pdf"];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
//download the file
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
NSURL URLWithString:#"http://www.example.com/info.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 setDelegate:self];
[webView loadRequest:requestObj];
Basically, I want to click a button in my web view that will download a PDF from a remote website and display it with a close/back button. I'm using QLViewController so people can pinch-zoom the PDF, and I'm using PhoneGap 3.0 with XCode 5 to code it.
Here's the code for my plugin that will show the new view with the back button but I can't seem to get the PDF to show. Any ideas?
#import "PDFViewer.h"
#import <Cordova/CDV.h>
#implementation PDFViewer
- (void)loadRemotePdf:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString *website = [command.arguments objectAtIndex:0];
NSString *filename = [command.arguments objectAtIndex:1];
if (website != nil && [website length] > 0) {
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
[webView canGoBack];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// NSURL *myUrl = [NSURL URLWithString:website];
// NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
// [webView loadRequest:myRequest];
// [window addSubview: webView];
NSURL *url = [NSURL URLWithString:website];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, filename];
[urlData writeToFile:filePath atomically:YES];
// Create test view controller
QLPreviewController *previewer = [[QLPreviewController alloc] init];
previewer.dataSource = self;
previewer.delegate = self;
// Create navigation controller
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:previewer];
[window addSubview: [nav view]];
[window makeKeyAndVisible];
}
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}
else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
// return result
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index;
{
NSURL *fileURL = [NSURL fileURLWithPath: filePath];
return fileURL;
}
#end
Yes, this can be done with the UIWebView.
If you are trying to display a PDF file residing on a server somewhere, you can simple load it to your web view directly:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
//Or if you have a PDF file bundled with your application (in this example named "document.pdf"):
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
I have used this code for displaying PDF on webview.
webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"fonts1" ofType:#"pdf"];
NSLog(#"%#",path);
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webview loadRequest:request];
webview.scalesPageToFit=YES;
[self.view addSubview:webview];
PDF in bundle I get PDF correctly.
What is wrong in this code.
try below one
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
If it is local then just put
UIWebView *WebViewObj = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
[self.view addSubview:WebViewObj];
[self.WebViewObj loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"filename" ofType:#"pdf"]]]];
If you are trying to display a PDF file residing on a server somewhere, you can simple load it to your web view directly:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
Or if you have a PDF file bundled with your application (in this example named "document.pdf"):
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
Let me know if you face any problem.
if i add
UIWebView *
at the beginning of your code everything works fine already tested
i dont think that is your problem
you should check the name of the file maybe is misspelled or check if the pdf file appears with red letters
if red letters the file is missing/damaged/moved
good luck
I think your pdf file not in target member so remove file and add like below
sagarcool89,SAMIR RATHOD,and dhaya: Every one given the right solutions:
UIWebView *pdfDisplay = [[UIWebView alloc] initWithFrame:self.view.frame];
NSURL *pathURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"IHI0053A_acle" ofType:#"pdf"]];
[pdfDisplay loadRequest:[NSURLRequest requestWithURL:pathURL]];
[self.view addSubview:pdfDisplay];
and it's the correct way to load pdf file in uiwebview.
I am trying to view a PDF file from a server but I only get a view in black and doesn't contain anything. Here's my code:
pdfViewer = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 768, 949)];
pdfViewer.backgroundColor = [UIColor whiteColor];
pdfViewer.delegate = self;
[sgContainer addSubview:pdfViewer];
chatSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
chatSpinner.frame = CGRectMake(334, 462, 100, 100);
[pdfViewer addSubview:chatSpinner];
NSString *httpSource = #"http://www.mywebsite.com/folder/subfolder/file.pdf";
NSURL *fullUrl = [NSURL URLWithString:httpSource];
NSURLRequest *httpRequest = [NSURLRequest requestWithURL:fullUrl];
[pdfViewer loadRequest:httpRequest];
If you are trying to display a PDF file residing on a server somewhere, you can simple load it to your web view directly:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
Or if you have a PDF file bundled with your application (in this example named "document.pdf"):
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
Let me know if you face any problem.