Converting PPT or PPTX to PDF in iOS objective-c (xcode) - ios

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.

Related

Formatting strings in UIWebView

I'm building an iOS app which supports iOS 9 and above.
I'm using a UIWebView to display text stored in RTF documents which I am including in my app's bundle. I use the following code to insert the contents of the RTF files into the web view:
NSURL *filePath = [[NSBundle mainBundle] URLForResource:self.detailItem withExtension:#"rtf"];
NSURLRequest *request = [NSURLRequest requestWithURL:filePath];
[self.detailWebView loadRequest:request];
I am also using this to format the text to the correct size:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *fontSize = #"80";
NSString *jsString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", [fontSize intValue]];
[self.detailWebView stringByEvaluatingJavaScriptFromString:jsString];
}
I never usually use JavaScript, so I'm not claiming to know how that works, but it enables me to adjust the fontSize string and achieve exactly the size that I want. The text displays in the same font as the rest of the stings in my app and everything looks fine.
The problem is that I want to be able to display some other text in the web view but I want to load it from an NSString in code, so I can append other strings and manipulate what the user sees. When I do this instead of loading from the RTF file, I get very different formatting:
[self.detailWebView loadHTMLString:#"This is a string" baseURL:nil];
This comes out in some nasty Times New Roman style font, and the text way smaller than the text that is loaded from the RTF files. I realise I can build some html tags into my string to add formatting, but I want to understand why the js formatting in webViewDidFinishLoad is not being applied, and what I can do to achieve universal formatting across all the strings I use in my web view.
If you need some UIWebView text formatting on iOS, you can look at my approach with SWIFT. https://github.com/Vanyaslav/Swiftyyjsformatter
Hope, it helps.

How to keep UIWebView from caching images?

I am using UIWebView to display an HTML page inside an IOS app. The HTML page contains a png file that I create on the local disk and write to. Everything works fine the first time I display the page, but when I try to create a new image and redisplay the page, the original image is displayed.
The issue appears to be that I am using the same file name for the PNG file. Even though I write to the PNG file with a new image, the UIWebView is caching the image from the original load, and displays the original image, not the new one. I have verified that the new image is being written to correctly by loading it into Safari.
So how can I clear the UIWebView's cache of this image? I realize that another option would be to give the png file a different file name each time I create it, but then I'd either accumulate png files or I'd have to add code to clear out the png files when done - which I'd rather not do.
Have a look at this answer:
NSString *testURL = [NSString stringWithFormat:#"%#?t=%#", url, randQuery];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:testURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]];
You can try:
NSString *theURL = [NSString stringWithFormat:#"%#?t=%#", url, [[NSProcessInfo processInfo] globallyUniqueString]];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:theURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:8.0]];

UIWebview taking too much of time to load html content in iOS 7

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.

Adding a lot of dynamic formatted text to iOS App

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 !

integrating PDF reader

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

Resources