How can I create a pdf reader in iphone? It should open within the same framework of my application. I have got code for creating PDF files but nothing for viewing a PDF. can anyone help me on dis ground?
You can use UIWebView or Quartz to display PDFs.
For UIWebView use this:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake( 0, 0, 320, 480 )];
NSString *path = [[NSBundle mainBundle] pathForResource:#"something" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
[webView release];
I'm currently building an app that uses a PDF document as will. There are 2 way's to display a pdf document, by far the simplest (but slowest) way is using a UIWebView.
The other option is building some sort of reader yourself by using the quartz2d libraries.
Apple has some good documentation on it: quart2d programming guide.
And an Example called zooming PDF Viewer.
Avoid WebViews, they are slow and you have no control over them.
The best way to render PDFs on iOS is using the CGPDF* set of functions available with Quartz.
Be aware that it won't be super easy. Over time I have found and/or contributed to numerous questions on SO about PDFs on iPhone. Check those out:
Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?
CGPDF iPhone/iPad memory problems
Get PDF hyperlinks on iOS with Quartz
I have created a PDF renderer here from apple's code. Just swipe left or right to browse through pages, pinch to zoom etc.
Here is my github.
Github PDF test
Related
I am struck with this past two days i don't find any proper documents relating to CGPDF. I tried every possible way but i failed. Here is what i am trying to do. I have a PDF to display and i am displaying it in a UIWebView. I have created a CORE GRAPHICS PDF document reference using the path where the pdf is located (NSURL). I created a UIView over it and handled a single touch event. When the PDF loads and user clicks the view i want the page to scroll to a specific page. I know this can be done via calculating Page height and width. I wanted to know if there is a Way in CGPDF to pass a page number and it scroll to the relevant page. Below is my Code
- (void)viewDidLoad
{
[super viewDidLoad];
// set the pdfPafeHeight to -1 so it gets calculated.
self.pdfPageHeight = -1;
// set the delegate of the UIWebView's underlying UIScrollView to self.
self.webView.scrollView.delegate = self;
// create an NSURLRequest to load the PDF file included with the project
_filePath = [[NSBundle mainBundle] pathForResource:#"Sample" ofType:#"pdf"];
_url = [NSURL fileURLWithPath:_filePath isDirectory:NO];
_urlRequest = [NSURLRequest requestWithURL:_url];
// create a Core Graphics PDF Document ref using the same NSURL
_pdf = CGPDFDocumentCreateWithURL((CFURLRef) _url);
// use CGPDFDocumentGetNumberOfPages to get the number of pages in the document
self.pdfPageCount = (int)CGPDFDocumentGetNumberOfPages(_pdf);
// load the PDF file into the UIWebVie
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
[self.webView loadRequest:_urlRequest];
}
HERE IS HOW I WANTED HANDLE SINGLE TAP ON UIVIEW. I just wanted to know if there is any method in CGPDF to scroll to specific page when i pass a page number to it
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
}
There's no API for what you're trying to achieve, and even if you're able to use private API if this app is just for Enterprise install and doesn't need to go to the App Store - this will be tricky and any workaround will likely break with a major iOS version update. If you want to go this route, you can introspect the view controller hierarchy and will find that Apple internally has a framework called CorePDF which has an internal private API that you might be able to access - but be careful and know that this is not a solution for App Store apps.
The alternative is to start from scratch and use CGContextDrawPDFPage to draw PDF pages, building your own cache, scroll views and view controllers+gesture handling. We did that with the commercial PSPDFKit SDK which I work on since 2010 - it's more work than you'd think. We also eventually moved on and use a custom renderer to improve compatibility with the wide range of PDF files available and to offer a better performance than Apple's renderer, however not every use case might require this.
I am new to iOS app development and I just tried using UIWebView to display a mobile website in my app and was not quite successful.
What I did is just some minimal Xcode project configuration and coding that I found during some Googling efforts:
Create a new iOS application project in Xcode using the Single View
Application template.
Drag a Web View from the Object Library to the View Controller
scene of Main.storyboard.
While holding down the Control key, drag the Web View from the View
Controller scene to the ViewController.h editor, resulting a
source code line like this:
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
Add the following code in ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:#"http://www.amazon.com"];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
This is all what I did (and also everything those tutorials told me). After I built and ran the app (on simulator and on real iphone device), the site did load in the app's view (and did load the mobile version instead of the PC version), but it displayed the site as if the view were much larger than the actual iphone screen. Here is a screeshot (Amazon in this case but basically the same for other sites):
What should I do to make the iOS UIWebView display a mobile website correctly?
Just tried Dipen Chudasama's suggestion of disabling the "User Size Classes" option, for the first time of all those suggestions, the result did change a little bit (from left-align to sort of center-align), but still not what I am looking for. Here is the screenshot:
EDIT:
Thanks for all the suggestions given by you enthusiastic people. Loading a site like amazon.com in a UIWebView should be a rather easy task as I understand, nevertheless, I didn't succeed with any of the suggestions.
It would be great if anyone could share with me (via Github or alike) just an sample xcode project (starting from scratch with the latest version of xcode tool chain) with nothing but a UIWebView that could load amazon.com correctly. That way I can do a line by line diff and may be able to find what I did wrong in my own project.
The UIWebView has a property called "scalesPageToFit":
self.myWebView.scalesPageToFit = YES;
self.myWebView.contentMode = UIViewContentModeScaleAspectFit;
should do the trick.
You can scale the page to fit the screen width with this UIWebview property webView.scalesPageToFit = YES;
Otherwise you can run a js command in the UIWebView to do the scale:
NSString *js = [NSString stringWithFormat:#"document.body.style.zoom = 0.8;"];
[webView stringByEvaluatingJavaScriptFromString:js];
I got it..you just set iPhone size view in storyboard instead of any size, means disable Use Size Classes Check box and see, this will work..:)
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.amazon.com"];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
Thats it, Try this one only.
OR
you have to set appropriate constraint for this.
I had the same issue as you #goodbyeera, and then realized I hadn't set the AutoLayout constraints on the UIWebView. Therefore, when the WebView loaded it was too big for the iphone screen. By adding the constraints to fit the UIWebView to the screen fixed it all. Hope this might help you.
Uiwebview taking huge amount of time to load html content in iOS 7. The html content has mathjax library.
Its working good in iOS 5.1 and iOS 6.1.
Can you please help me how to fix this issue?
I am using loadRequest method to load html content into UIWebView.
Thanks,
Rahman
I have the same problem. After the update to iOS 7, the loadRequest takes about 10 seconds (with iOS 6 less than a second).
My code is like :
NSString *whichWiki = #"http://en.wikipedia.org/wiki/";
NSString *keyword = #"universe";
NSString *siteURLWiki =[NSString stringWithFormat:#"%#%#",whichWiki, keyword];
[webv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:siteURLWiki]]];
I tried 2 resolutions I found in Google:
self.webv.dataDetectorTypes = UIDataDetectorTypeNone; // Resolution 1
self.webv.suppressesIncrementalRendering = YES; // Resolution 2
but with results of no effects.
Strangely, I have this problem with URL of Wikipedia but NOT WITH URL of Google. I can get URLs of Google search results less than a second.
What's the difference between Wikipedia and Google ???
I unchecked the phone numbers detection on UIWebView from Storyboard. It works for me.
I just want to convert PPT or PPTX files into PDF files in iOS objective-c application.
(iPhone and iPad apps)
But I couldn't find any straightforward solution to do this.
Is there any method to convert powerpoint files to PDF files?
No other options needed, just want to convert it. (Not through generating images, because of resolution problem)
Thank you!
There is a way to do it in several steps.
Firstly, you should load your pptx file into UIWebView.
// fileURL – URL of your pptx file
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.delegate = self;
[webView loadRequest:request];
When your webView did finish load your get content of your webView:
NSString *htmlContent = [webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
Then you can render your html content as PDF without quality loose. There is a tutorial where is described how to do it.
I have a big issue with my current development stage on a project that I'm working on.
I'm have a UIScrollView which holds 50 multiline UILabels with dynamic content loaded from a localizable.string. The labels are individually formatted (font, bold, italic, color).
The problem is that the App's real memory usage jumps to almost 70MB (live bytes 3MB) and that is just unacceptable and with my current concept I would have to use ~200 UILabels in order to achieve my goal.
What can I do ? Is there a way to lazy load the UILabels or reuse them ? Should I use UITextView or UIWebView ?
How can I do that ?
Thanks.
I would recommend using something like https://github.com/AliSoftware/OHAttributedLabel or a UIWebView OHAttributedLabel should have a much smaller memory foot print than a WebView But it depends how many web views you would actually need.
if you use a UIWebView
You'd draw a web view in your view, if you are using IB create and hook up outlets and then in your .m load the html formatted text into via loadHTMLString:baseURL:
Well guys with your help, I've came up with this solution which works just extraordinary !
I'm using a html "template" file with markers inside it and a UIWebView.
Basically what I'm doing is the following:
get the html file path.
create a string with the contents of html template.
replace the markers from the html with my strings (NSLocalized strings - lots of text).
load into the WebView contents of the newly created string using "loadHTMLString".
Result: from a memory footprint of 70MB, now I have a memory footprint of 12MB (and that with the equivalent of ~20 A4 pages of text).
Here's the code:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"yourhtmlfile" ofType:#"html"];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[_webView setBackgroundColor:[UIColor clearColor]];
[_webView setOpaque:NO];
NSString *htmlBody = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF16StringEncoding error:nil];
htmlBody = [htmlBody stringByReplacingOccurrencesOfString:#"//marker_1//" withString:NSLocalizedString(#"localizedKey_1", nil)];
htmlBody = [htmlBody stringByReplacingOccurrencesOfString:#"//marker_2//" withString:NSLocalizedString(#"localizedKey_2", nil)];
htmlBody = [htmlBody stringByReplacingOccurrencesOfString:#"//marker_3//" withString:NSLocalizedString(#"localizedKey_3", nil)];
[self.webView loadHTMLString:htmlBody baseURL:baseURL];
And now I can use the html goodies like text formatting :D !
I hope that the above will help a lot of people :) !
Thank you guys for support !