Generating the html file dynamically from the user inputs - html

I have a requirement for my project . I want to generate html file dynamically from the user
inputs. I have searched a lot , but I couldn't find any links for this.Any help on this ?

I have done similar thing. I think what you need to do is. create a template html page with container divs with id and load that file. On the delegate method of webview onPagefinishedloading run java script to add the information dynamically on the divs to the template html page.

NSBundle *bundle = [NSBundle bundleForClass:[self Class]];
NSString *htmlPath = [bundle pathForResource:#"html file name" ofType:#"htm"];
NSString *htmlContent = [NSString stringWithContentOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
//Do your HTML edits here
[[self webView] loadHTMLString:htmlContent baseURL:nil];

Related

How to create DOM model in iOS SDK

How to create DOM model in iOS SDK to update changes in HTML file? I am making a project so where I have HTML file to convert PDF format so first I create HMTL file and then programmatically update dynamic data in HMTL file.
So how do I use DOM model in Objective-C to change HTML file data?
First create your HTML File with different tags as like "##v1#" there you want to edit your text and value. and call below function in your code.
NSString *html = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"page02" ofType:#"html" inDirectory:NO]encoding:NSUTF8StringEncoding error:nil];
html = [html stringByReplacingOccurrencesOfString:#"##v1#" withString:completeHTML];
[self.webViewTest loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
I think it would be help full for you.

How do I display a jpeg image stored in a core data database using html

I have several images soared in a core data database in this way. The entity is named note.
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
image = nil; // free memory
[self createNote];
note.photo_jpeg = imageData;
How do I reference the images in html generated for a web page to display several of these images? I think I need something like this, but I don't know what to put in the IMG SRC=...
NSString *imageHtml = [NSString stringWithFormat:#"<IMG SRC="what do i put here!!!" ALT="Photo" WIDTH=%i HEIGHT=%i>", , )];
[html appendString:imageHtml];
Update This is the solution I used:
[html appendFormat:#"<img alt=\"Embedded Image\" src=\"data:image/jpg;base64,%#\" WIDTH=400 />", [currentNote.photo_jpeg base64EncodedStringWithOptions:0]];
Where currentNote is of type note and is indexed through the notes I am displaying.
You'll need to put the image data inline with the HTML, encoded as base64.
Something like this:
NSData *imageData = // from your code
NSMutableString *html = // mutable string with whatever else you need
[html appendFormat:#"<img alt=\"Embedded Image\" src=\"data:image/jpg;base64,%#\" />", [imageData base64EncodedStringWithOptions:0]];
Keep in mind that this duplicates the image data, so if you're using a lot of images this way, make sure to watch how much memory you're using.
NSData have required methode to get base64 string.
img src="data:image/jpg;base64,HereBase64RepresentationOfYourJPG"
I do not think there is a way to have HTML dip into core data.
I would create a sub-directory for your core data store. In that subdirectory, create a sibling directory to hold your images.
You can store the images as plain jpeg files, and keep the path or bookmark-url to the file in your core data object.
This way, you can still access everything via core data, and the file is available to the HTML as well.
Just include the path to the file as part of the html.

iOS7: Display an Image in an HTML File loaded into a UIWebView

I am programming in iOS7. In my app I am using a UIWebView to load static HTML files that are packaged with the app. I want to show images, also packaged with the app, in my HTML files, but can not find the magic combination to get the images to show when the HTML file is loaded.
I saw some information on base64, which I did not understand, but I would like to find a simple solution because there will be others beside myself writing most of the HTML files.
Below is the basic format of the HTML code I am using. I have tried all manner of “/“ modifiers to no avail, including the full path as listed in the File Inspector.
<img src=“data:/Supporting Files/Info/InfoRecycle.png">
Does anyone know how I get the image to show within the HTML file?
Try putting the image in the same folder as your html file. Then reference it like this:
<img src="InfoRecycle.png">
See if that works.
I finally got it to work! I found the key to the answer in this post. I guess I did not find it earlier because I was focused on iOS7:
Using HTML and Local Images Within UIWebView
I had to make some modifications in my Xcode, so it now looks like this:
//..Loads a Local HTML File
strHTML = #"Info";
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:strHTML ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[_infoWebView loadHTMLString:htmlString baseURL:baseURL];
My reference to the image in the HTML code is now as suggested above by Stepan Grigoryan.
<img src="InfoRecycle.png">

How can I overwrite or save an existing local HTML file that's edited in a UIWebView?

The app uses a UIWebView to load up local HTML files (which are now just text) for editing. I need to save whats been edited by the user back to the local file. This is the only way I could think of doing that:
Once the text has been edited by the user, I've written a save method that takes the current HTML in the web view and saves it to an HTML file (with the same fileName as the original) which is written to the documents directory. In theory this should overwrite the original. But it doesn't.
-(void)save{
NSError *err;
NSString * docsDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *oldFile = [self.webView.request.URL lastPathComponent];
//Above line gets the actual HTML file name that's in the webview
NSString * path = [docsDir stringByAppendingPathComponent:oldFile];
//The following line gets what html code is in the actual webview
NSString *html = [self.webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('html')[0].innerHTML"];
[html writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
No file seems to be written. It doesn't overwrite the original nor does it write a new one.
Perhaps I shouldn't be saving it this way, but I can't think of any other methods of doing it.

Adding an image in an html file with iOS

In my app I'm presenting a report of the app data. The report is a "self-generated" html file presented on an UIWebView.
I need to include in the report (in the html file) an image that is stored in the device. At this moment I'm able to get the path of the image. It's something like "/var/mobile/Applications/A10781A1-DE2B-4651-ADFB-7A6AD9B3645A/Documents/EE20AF92-215E-4DF5-8E33-0713557A34C9"
How can I include the image in the html file?
You can include the image using:
<img src="img.png">
and
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
You can copy your image img.png to documents directory for example and set the baseURL to be the Documents directory.
Update:
I found the source where I found out how to do it some time back. Maybe you can dig from there some more useful info:
http://iphoneincubator.com/blog/windows-views/uiwebview-revisited
Its not clear if your building the html on the fly from your question. But you can do something like this.
NSMutableString *htmlPage = [[[NSMutableString alloc] initWithCapacity:1000] autorelease];
... // Build the page string
[htmlPage appendStringWithFormat:#"<img src=\"%#\" alt=\"image\" height=\"100\" width=\"100\"/>",[[NSBundle mainBundle] pathForResource:#"myImage" ofType:#"png"];
Or some variant of this.