简体   繁体   中英

how to convert byte code into pdf in ios when hit url in ios

this is one of my activity I hit a url using NSURL connection and then Parse this data and get JSON data this data in byte code like as
NSString *ptr= { 82, 111, 111, 116, 32, 50, 32, 48, 32, 82, 10, 47, 83, 105, 122, 101, 32, 51, 50, 10, 47, 73, 110, 102, 111, 32, 49, 32, 48, 32, 82, 10, 62, 62, 10, 115, 116, 97, 114, 116, 120, 114, 101, 102, 10, 49, 52, 51, 52, 50, 55, 10, 37, 37, 69, 79, 70, 10.........etc }

I got this in a NSString and after this

  NSMutableArray *bytes=[[NSMutableArray alloc]init];
  [bytes addObject:ptr];

  NSMutableData *data = [[NSMutableData alloc] initWithCapacity:bytes.count];
  NSLog(@"file %@",data);
  for (NSNumber *byteVal in bytes)
  {
    Byte b = (Byte)(byteVal.intValue);
    [data appendBytes:&b length:1];
  }

 NSArray *docDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *docDirectory = [docDirectories objectAtIndex:0];
 NSString *fileName = [docDirectory stringByAppendingPathComponent:@"abcd.pdf"];
 NSLog(@"file %@",fileName);

 [data writeToFile:fileName atomically:NO];

 NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

 NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"abcd.pdf"];

 NSURL *targetURL = [NSURL fileURLWithPath:filePath];
 NSURLRequest *requestFile = [NSURLRequest requestWithURL:targetURL];

 NSLog(@"request path %@",requestFile);

This is output. It creates pdf but i can't understand how to open and see the generated pdf file.
file /Users/rahulsharma/Library/Application Support/iPhone Simulator/6.1/Applications/CD29ED82-8039-411F-8BBA-2784E5445EDE/Documents/abcd.pdf

request path <NSURLRequestfile://localhost/Users/rahulsharma/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/CD29ED82-8039-411F-8BBA-2784E5445EDE/Documents/abcd.pdf>

Please help me solve this problem on how to get the pdf.

 NSString *responseText;
 NSMutableArray *byteArray;
viewdidload
{
  [self report];
 }
-(void)report
{
finished=FALSE;
NSString *case_id=[NSString stringWithFormat:@"8cc61590-42b0-4433-b8ee-25e40d5e6033"];

NSString *investigation_id=[NSString stringWithFormat:@"33a8b03f-4ad2-424a-8aa9-02a07d3937eb"];
NSString *test_id=[NSString stringWithFormat:@"e5a7e867-422c-4365-b920-87fdf5d82783"];


dictionary = [NSMutableDictionary dictionary];

[dictionary setObject:case_id forKey:@"CaseId"];
[dictionary setObject:investigation_id forKey:@"InvestigationId"];
[dictionary setObject:test_id forKey:@"TestId"];
// [dictionary setObject:Procedure forKey:@"proc"];
// [dictionnary setObject:count  forKey:@"count"];
NSLog(@"dict %@",dictionary);
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];
NSLog(@"json %@",jsonData);
NSURL *url1 = [NSURL URLWithString:@"http://192.168.1.202:81/LaboratoryModule/LISService.asmx/GetpatienttestReport"];
NSLog(@"url is %@",url1);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url1];
[request setHTTPMethod:@"POST"];

[request addValue: @"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:jsonData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if( theConnection )
{
    webData = [NSMutableData data] ;
    NSLog(@"%@",webData);

}
else
{
    NSLog(@"theConnection is NULL");
}
while(!finished)
{
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

}

 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

 [webData setLength: 0];
 NSLog(@"web is = %@",webData);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
NSLog(@"web is = %@",webData);

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");

}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

responseText = [[NSString alloc] initWithData:self.webData encoding:NSUTF8StringEncoding];
NSLog(@"val is %@",responseText);

SBJsonParser *par=[[SBJsonParser alloc]init];
NSString *filecontent=[[NSString alloc]initWithString:responseText];
NSDictionary *data=(NSDictionary*)[par objectWithString:filecontent error:nil];
NSLog(@" data is %@",data);
byteArray=[data objectForKey:@"d"];
NSLog(@" string is %@",byteArray);


NSLog(@"DONE. Received Bytes: %d", [webData length]);

finished=TRUE;       
}

If you want to open and view PDF file in iPhone then you can use a UIWebView to open and show a PDF file. Pass this requestFile in this -

[webView loadRequest:requestFile];

here webView is the variable name for UIWebView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM