Search code examples
htmliphoneiosios5

Get proper format of the text file in HTML form


I am using the following code to convert text to pdf form:

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"All_lang_unicode" ofType:@"txt"];

NSString *str;
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {

    str = [[NSString alloc] initWithData:myData encoding:NSUTF16StringEncoding];
    NSLog(@"STRING : %@",str);  
}

NSString *html = [NSString stringWithFormat:@"<body>%@</body>",str];


UIMarkupTextPrintFormatter *fmt = [[UIMarkupTextPrintFormatter alloc]
                                   initWithMarkupText:html];
UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
[render addPrintFormatter:fmt startingAtPageAtIndex:0];
CGRect page;
page.origin.x=0;
page.origin.y=0;
page.size.width=792;
page.size.height=612;


CGRect printable=CGRectInset( page, 0, 0 );
[render setValue:[NSValue valueWithCGRect:page] forKey:@"paperRect"];
[render setValue:[NSValue valueWithCGRect:printable] forKey:@"printableRect"];

NSLog(@"number of pages %d",[render numberOfPages]);

NSMutableData * pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData( pdfData, CGRectZero, nil );

for (NSInteger i=0; i < [render numberOfPages]; i++)
{
    UIGraphicsBeginPDFPage();
    CGRect bounds = UIGraphicsGetPDFContextBounds();
    [render drawPageAtIndex:i inRect:bounds];

}

UIGraphicsEndPDFContext();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * pdfFile = [documentsDirectory stringByAppendingPathComponent:@"test.pdf"];
[pdfData writeToFile:pdfFile atomically:YES];

But problem is that I am not getting the proper formatting of the text. when I print using NSLog(); I get the proper content but when I place the string in STRING the spacing and newline is missing.. all coming in same line. i.e. continuous.

(UPDATE : )

NSLog OUTPUT:(Proper)

NEW DELHI: Sachin Tendulkar's streak of low scores might have raised a question mark over his future but senior BCCI official and IPL chairman Rajiv Shukla on Monday came out in support of the senior batsman saying one needs to look at his "colossal record" before making any comment.

"He will hang up his boots when he thinks it's time for him to go. He does not need any advice on this. Before making a comment on his performance you have to see his colossal record and his past performance," Shukla told reporters outside the Parliament adding that the veteran cricketer will come back strongly in the forthcoming matches.

and Im getting as:

NEW DELHI: Sachin Tendulkar's streak of low scores might have raised a question mark over his future but senior BCCI official and IPL chairman Rajiv Shukla on Monday came out in support of the senior batsman saying one needs to look at his "colossal record" before making any comment. "He will hang up his boots when he thinks it's time for him to go. He does not need any advice on this. Before making a comment on his performance you have to see his colossal record and his past performance," Shukla told reporters outside the Parliament adding that the veteran cricketer will come back strongly in the forthcoming matches.

Can any one please suggest modification in this code so that I can get the proper format.


Solution

  • If I get it right, you should replace your new line characters with <br> or <p>. Try

    str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];

    How to detect new lines in Objective-C

    Solution of your next question might look like this:

    NSArray *words = [str componentsSeparatedByString:@" "];  
    NSString *line = @"";  
    NSUInteger maxLineLength = 100;  
    NSString *resultStr = @"";  
    for (NSString *word in words) {  
        if ([line length] + [word length] > maxLineLength) {  
            resultStr = [resultStr stringByAppendingFormat:@"%@<br>", line];  
            line = word;  
        } else {  
            line = [line stringByAppendingFormat:@" %@", word];  
        }  
    }  
    resultStr = [resultStr stringByAppendingString:line];