user3160648
user3160648

Reputation: 31

RTF to HTML conversion using Cocoa

I am a QT programmer and Cocoa is new for me. How can convert Rich Text Format(RTF text with images and hyper link) to HTML using cocoa under Mac OS X. I have rich text into a char type buffer.

Upvotes: 3

Views: 729

Answers (3)

CRD
CRD

Reputation: 53010

You can perform the conversion of RTF to HTML using NSAttributedString and the additions to it provided by Application Kit - read NSAttributedString Application Kit Additions Reference.

The are methods to create an NSAttributedString from RTF, such as initWithRTF:documentAttributes: and initWithURL:documentAttributes:.

To create HTML you can use dataFromRange:documentAttributes:error: specifying appropriate attributes, you'll need to specify at least NSHTMLTextDocumentType.

Upvotes: 1

hooleyhoop
hooleyhoop

Reputation: 9198

Depends really on your output requirements… i could convert the string "hello world" to valid html, but it might not be what you were expecting… anyway as an alternative approach to @Joshua's…

The textutil utility can be useful for all sorts of conversions. You can use it from Cocoa via NSTask

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/textutil"];
[task setArguments: @[@"-format", @"rtf", @"-convert", @"html", @"-stdin", @"-stdout"]];
[task setStandardInput:[NSPipe pipe]];
[task setStandardOutput:[NSPipe pipe]];
NSFileHandle *taskInput = [[task standardInput] fileHandleForWriting];
[taskInput writeData:[NSData dataWithBytes:cString length:cStringLength]];
[task launch];
[taskInput closeFile];

Synchronously

NSData *outData = [[[task standardOutput] fileHandleForReading] readDataToEndOfFile];
NSString *outStr = [[NSString alloc] initWithData:outData encoding:NSUTF8StringEncoding];

or async

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readCompleted:) name:NSFileHandleReadToEndOfFileCompletionNotification object:[[task standardOutput] fileHandleForReading]];
[[[task standardOutput] fileHandleForReading] readToEndOfFileInBackgroundAndNotify];

- (void)readCompleted:(NSNotification *)notification {
  NSData *outData = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
  NSString *outStr = [[NSString alloc] initWithData:outData encoding:NSUTF8StringEncoding];
  NSLog(@"Read data: %@", outStr);
  [[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleReadToEndOfFileCompletionNotification object:[notification object]];
}

Please don't just copy and paste this though.. it's just an example, not tested production code.

Upvotes: 1

Joshua Nozzi
Joshua Nozzi

Reputation: 61238

Create an NSAttributedString instance with the RTF data. Walk the attribute ranges of the string (this includes the attachments / pictures) and translate to HTML (append the proper HTML to an NSMutableString) as you go. This lets you convert any attributes you'd like while leaving behind those you don't want.

A helpful NSAttributedString method would be -enumerateAttributesInRange:options:usingBlock:. Inside the block you can determine whether you want to handle or ignore a given attribute. See the Handling Attachments section for information regarding dealing with your images (considered an attachment in RTFD, just another attribute type for a "character" in the attributed string).

Upvotes: 1

Related Questions