AirPrint HTML file made locally iPhone - iphone

I am currently creating a html file and storing it locally, i give the user the choice of sending the html by HTML or print.
For some reason it is failing to print. if i test -
if([UIPrintInteractionController canPrintData:myData]){
it doesn't get past this point.
Is it possible to print local HTML files if so how to go about it.
Dan
edit code to create html -
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *Daily = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.html",self.title]];
[urlToLoad writeToFile:Daily atomically:YES encoding:NSUTF8StringEncoding error:NULL];
NSString *fileName = [NSString stringWithFormat:#"%#/%#.html", documentsDirectory,self.title];
NSData *myData = [NSData dataWithContentsOfFile:fileName];

[UIPrintInteractionController canPrintData:] is expecting the data to contain properly formatted PDF data, not HTML.
Try doing this instead:
UIPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:html];
[[UIPrintInteractionController sharedPrintController] setPrintFormatter:formatter];

Related

iOS UIWebview Load local device HTML file path [duplicate]

This question already has answers here:
How to load local html file into UIWebView
(18 answers)
Closed 9 years ago.
I am developing one iOS application in which I want to load the HTML file on UIWebView, my html file path is as like below,
iPhone HTML file path:
/var/mobile/Applications/65A8E000-21A6-429E-90A0-6BF03BAB3EA5/SarkBuilderApp.app/SarkBuilerHtmlFile_new.html
I want to load this iPhone device's HTML file path to UIWebView.
How can I do this?
Thank you very much for your quick answer. Let me explain my basic requirement,
I am devleoping one iOS application in which my requirement is to fill some field on the screen and generate a .pdf file.
For that I have taken one pre-defined HTML form, and after that I updated that .html file and saved it via the code below:
NSString *fileName = #"new_file_htmlfile.html";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *html_file_name_location = [documentsDirectory stringByAppendingPathComponent:fileName];
NSError *error;
// Write the file
[data writeToFile:html_file_name_location atomically:YES
encoding:NSUTF8StringEncoding error:&error];
By doing that I have generated the new HTML file and I have read the file path from the code. The iPhone device path is:
/var/mobile/Applications/65A8E000-21A6-429E-90A0-6BF03BAB3EA5/SarkBuilderApp.app/SarkBuilerHtmlFile_new.html
iPhone emulator path is:
/Users/rightwaysolution/Library/Application Support/iPhone Simulator/6.1/Applications/20748991-EE5C-44FE-ACFF-D93542FCF95B/Documents/new_file_htmlfile.html
Now I am trying to load that iPhone device HTML file on my UIWebView so that I can convert it into a .pdf.
Could you please explain to me how I can access that file?
use this code to achieve what you want
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"SarkBuilerHtmlFile_new" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
but for this make sure your html file must be within your app folder in xcode
Edits:-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"txtFile.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
NSString* htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
OR
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"txtFile.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
NSString* htmlString = [NSString stringWithContentsOfFile:content encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];

save pdf file in device

I generated a pdf file in my program and I have it here :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingFormat:#"/CEX.pdf"];
NSData *data = [NSData dataWithContentsOfFile:file];
I know how to save photos in the photo gallery but have no idea what should I do with pdf file and how and where to save it in the device. Can anyone help me please ?! :)
The code you posted is for reading an existing PDF file from the Documents directory.
If you want to write the PDF, you need to get the NSData object representing the PDF, create a path to the file, then use the NSData writeToFile:options:error: method to save the data to the file.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingPathComponent:#"MyDocument.pdf"];
NSData *pdfData = ... // data representing your created PDF
NSError *error = nil;
if ([pdfData writeToFile:file options:NSDataWritingAtomic error:&error]) {
// file saved
} else {
// error writing file
NSLog(#"Unable to write PDF to %#. Error: %#", file, error);
}
BTW - in your original code, replace:
NSString *file = [documentsDirectory stringByAppendingFormat:#"/CEX.pdf"];
with:
NSString *file = [documentsDirectory stringByAppendingPathComponent:#"CEX.pdf"];
Don't use string formats unless you really have a string format to process.

Error while writing data to a file in iphone

in my application, i have placed an empty
myFile.txt
when i have internet connection , i get json data from internet and save the json string in it with following code
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"txt"];
[myString writeToFile:filePath automatically:YES encoding:NSUTF... error:nil];
//now while retrieving it when no internet connection
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"txt"];
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF.. error:nil];
now myString returns with #""... why i am not been able to write data?
Best Regards
Whatever you want to do is not possible (or at least strongly discouraged) to update files in the app bundle.
If you’re downloading files and want to store them on the device you should use the Documents directory. You can get the path to this directory with:
- (NSString *)getDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
First check weather the contents are getting written to file. You can view the file in xcode.
What i suggest is when you run the app create this empty file. Then you can Read and write to that file easily.
Try Using Below code:
NSError* error = nil;
NSString* jsonFileName = [NSString stringWithFormat:#"myfile"];
NSString* jsonPath = [[NSBundle mainBundle] pathForResource:jsonFileName
ofType:#"txt"];
NSString* jsonString = [NSString stringWithContentsOfFile:jsonPath
encoding:NSUTF8StringEncoding error:&error];
Documents directory:
Write to file
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/textfile.txt",
documentsDirectory];
//create content - four lines of text
NSString *content = #"One\nTwo\nThree\nFour\nFive";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
Display content:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/textfile.txt",
documentsDirectory];
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error:nil];

Replacing image URLs in html string

Hello I have a html string, with tags for every image.
I can extract the url of every image, but what I want is to save those images in the device, and then replace each one's image url with the corresponding path from my sandbox and then display them via UIWebView.
Any ideas how do I do that?
Thank you!
Well if you have the URL for an image, you can save it like this
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL urlWithString:string]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"MyFile"];
[imageData writeToFile:filePath atomically:YES];
To replace the URLs in the HTML string simply use the method
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement

About 2000 strings to a file

I need to put separate lines into a file, but it seems that it's not supported by
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"myFile"];
[dataString writeToFile:appFile atomically:YES];
It does put a string to a file but it overwrites previous one.
Any suggestions?
To append data to an existing file, create an NSFileHandle instance for that file, then call -seekToEndOfFile and finally -writeData:. You'll have to convert your string into an NSData object yourself (with the correct encoding). And don't forget to close the file handle when you're finished.
The easier, but also less efficient way, is to read the existing file contents into a string, then append the new text to that string and write everything out to disk again. I wouldn't do that in a loop that executes 2000 times, though.
Thanks Ole! That's what I've been looking for.
Some sample code for the others:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//creating a path
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"nameOfAFile"];
//clearing or creating (NSFileHande doesn't support creating a file it seems)
NSString *nothing = #""; //remember it's CLEARING! so get rid of it - if you want keep data
[nothing writeToFile:appFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
//creating NSFileHandle and seeking for the end of file
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:appFile];
[fh seekToEndOfFile];
//appending data do the end of file
NSString *dataString = #"All the stuff you want to add to the end of file";
NSData *data = [dataString dataUsingEncoding:NSASCIIStringEncoding];
[fh writeData:data];
//memory and leaks
[fh closeFile];
[fh release];
[dataString release];