For Reading PDF file in UIWebView with horizontally in iOS.
So i write following to add with UIScrollView to scroll horizontally.
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.contentSize = CGSizeMake(2000, 420);
scrollView.bounces = NO;
scrollView.alwaysBounceHorizontal = NO;
scrollView.alwaysBounceVertical = NO;
scrollView.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:self.spiderWeb];
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.spiderWeb loadRequest:request];
[self.view addSubview:scrollView];
It doesn't showing anything. Without ScrollView , i can read PDF in UIWebView with vertically. Where am i wrong?
Check the below links may be it is useful for you
https://github.com/iclems/iOS-htmltopdf
https://developer.apple.com/library/ios/samplecode/PrintWebView/Introduction/Intro.html
http://www.labs.saachitech.com/2012/10/23/pdf-generation-using-uiprintpagerenderer/
Related
I want to load a gif image with webView,And it works well.But when i find leaks with instruments,there is a memory leak. Who can tell me why,Here is the code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"icon_coinMoney_image" ofType:#"gif"];
UIWebView *gifWebView = ({
UIWebView *webView = [UIWebView new]; //Leaks Here
[self addSubview:webView];
[webView setBackgroundColor:[UIColor clearColor]];
webView.userInteractionEnabled = NO;
[webView setScalesPageToFit:YES];
webView.opaque = NO;
NSURL *url = [NSURL URLWithString:path];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
[webView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(backImageView.mas_left).offset(-MARGIN);
make.centerY.mas_equalTo(backImageView.mas_centerY).offset(1.f);
make.size.mas_equalTo(CGSizeMake(ZLL_AUTOSIZING_WIDTH(30.f), ZLL_AUTOSIZING_HEIGTH(30.f)));
}];
[webView.layer setMasksToBounds:YES];
[webView.layer setCornerRadius:ZLL_AUTOSIZING_WIDTH(15.f)];
webView;
});
I have downloaded a PDF file form a web url and saved it on disk. I want to view the same file in webView. i used:
[self.navigationController pushViewController:self.fileViewController animated:YES];
i am using this code to navigate to other view controller. Following is my code in viewDidLoad:
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
}
Just tell me that where i am wrong. thanks in anticipation.
I have checked your code and it works. May be you need to add webview as subview to your view controller by using following code: [self.view addSubview:webView];
I hope it will help you...
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
[self.view addSubview:webView];
NSString *path = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
}
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.
Can we open the pdf file from UIWebView?
Yes, this can be done with the UIWebView.
If you are trying to display a PDF file residing on a server somewhere, you can simply load it in your web view directly:
Objective-C
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSURL *targetURL = [NSURL URLWithString:#"https://www.example.com/document.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
Swift
let webView = UIWebView(frame: CGRect(x: 10, y: 10, width: 200, height: 200))
let targetURL = NSURL(string: "https://www.example.com/document.pdf")! // This value is force-unwrapped for the sake of a compact example, do not do this in your code
let request = NSURLRequest(URL: targetURL)
webView.loadRequest(request)
view.addSubview(webView)
Or if you have a PDF file bundled with your application (in this example named "document.pdf"):
Objective-C
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSURL *targetURL = [[NSBundle mainBundle] URLForResource:#"document" withExtension:#"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
Swift
let webView = UIWebView(frame: CGRect(x: 10, y: 10, width: 200, height: 200))
let targetURL = NSBundle.mainBundle().URLForResource("document", withExtension: "pdf")! // This value is force-unwrapped for the sake of a compact example, do not do this in your code
let request = NSURLRequest(URL: targetURL)
webView.loadRequest(request)
view.addSubview(webView)
You can find more information here: Technical QA1630: Using UIWebView to display select document types.
UIWebviews can also load the .pdf using loadData method, if you acquire it as NSData:
[self.webView loadData:self.pdfData
MIMEType:#"application/pdf"
textEncodingName:#"UTF-8"
baseURL:nil];
use this for open pdf file in webview
NSString *path = [[NSBundle mainBundle] pathForResource:#"mypdf" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[[webView scrollView] setContentOffset:CGPointMake(0,500) animated:YES];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0.0, 50.0)"]];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
NSString *folderName=[NSString stringWithFormat:#"/documents/%#",[tempDictLitrature objectForKey:#"folder"]];
NSString *fileName=[tempDictLitrature objectForKey:#"name"];
[self.navigationItem setTitle:fileName];
NSString *type=[tempDictLitrature objectForKey:#"type"];
NSString *path=[[NSBundle mainBundle]pathForResource:fileName ofType:type inDirectory:folderName];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 730)];
[webView setBackgroundColor:[UIColor lightGrayColor]];
webView.scalesPageToFit = YES;
[[webView scrollView] setContentOffset:CGPointZero animated:YES];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0.0, 50.0)"]];
[webView loadRequest:request];
[self.view addSubview:webView];
Swift version:
if let docPath = NSBundle.mainBundle().pathForResource("sample", ofType: "pdf") {
let docURL = NSURL(fileURLWithPath: docPath)
webView.loadRequest(NSURLRequest(URL: docURL))
}
An update to Martin Alléus's answer, to get the full screen whether it is a phone or a iPad without having to hard code:
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"pdf" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
Swift 4
let webView = UIWebView(frame: view.bounds)
let targetURL = Bundle.main.url(forResource: "sampleFileName", withExtension: "pdf")
let request = URLRequest(url: targetURL!)
webView.loadRequest(request)
view.addSubview(webView)
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
UIWebView *pdfWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSURL *targetURL = [NSURL URLWithString:#"http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[pdfWebView loadRequest:request];
[self.view addSubview:pdfWebView];
WKWebView: I find this question to be the best place to let people know that they should start using WKWebview as UIWebView is now deprecated.
Objective C
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame];
webView.navigationDelegate = self;
NSURL *nsurl=[NSURL URLWithString:#"https://www.example.com/document.pdf"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];
Swift
let myURLString = "https://www.example.com/document.pdf"
let url = NSURL(string: myURLString)
let request = NSURLRequest(URL: url!)
let webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.loadRequest(request)
view.addSubview(webView)
I haven't copied this code directly from Xcode, so it might, it might contain some syntax error. Please check while using it.