user1899840
user1899840

Reputation: 543

How to load document from tmp folder in webviewer of pdftron?

I m using HTML5 of webviewer component for iOS Application.

I have added the "HTML5" folder as "Create folder reference for any added folder" and "Copy items into destination group's folder" is unchecked.

The sample code given in iOS has a folder named "xod" with a default document and the folder is added as "Create folder refernce for any added folder".

The code mentioned in the sample for above scenario is as follows:

- (void)viewDidLoad
{
  [super viewDidLoad];

UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.frame];

webView.delegate = self;
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self.view addSubview:webView];

NSString* fullPath = [[NSBundle mainBundle] pathForResource:@"/html5/MobileReaderControl" ofType:@"html"];

NSURL *url = [NSURL fileURLWithPath:fullPath];
NSString* absoluteString  = [url absoluteString];
NSString* stringWithQuery = [absoluteString stringByAppendingString:@"#d=iosrange://xod/GettingStarted.xod"];
NSURL* webviewUrl = [NSURL URLWithString:stringWithQuery];

NSURLRequest* webviewerRequest = [[NSURLRequest alloc] initWithURL:webviewUrl];
[webView loadRequest:webviewerRequest];
}

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *URL = [request URL];

if ([[URL scheme] isEqualToString:@"iosrange"]) {
    NSString *requestString = [URL absoluteString];
    // get rid of the protocol
    NSString *withoutProtocol = [[requestString componentsSeparatedByString:@"//"] objectAtIndex:1];

    NSArray *urlParts = [withoutProtocol componentsSeparatedByString:@"#"];
    // document location is everything before the question mark
    NSString *docPath = [[urlParts objectAtIndex:0] stringByDeletingPathExtension];
    NSString *docLocation = [[NSBundle mainBundle] pathForResource:docPath ofType:@"xod"];

    // query string has start and possibly stop values for the byte range
    NSArray *queryString = [[urlParts objectAtIndex:1] componentsSeparatedByString:@"&"];
    NSInteger rangeStart = [[queryString objectAtIndex:0] integerValue];
    NSInteger originalRangeStart = rangeStart;
    NSInteger rangeEnd = [[queryString objectAtIndex:1] integerValue];

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:docLocation];
    if (rangeStart < 0) {
        // a negative range means it's from the end of the file
        // we need to calculate what that offset is from the beginning of the file
        NSInteger fileSize = [fileHandle seekToEndOfFile];
        rangeStart = fileSize + rangeStart;
    }

    [fileHandle seekToFileOffset:rangeStart];

    NSData *data;
    if (rangeEnd == 0) {
        data = [fileHandle readDataToEndOfFile];
    } else {
        data = [fileHandle readDataOfLength:(rangeEnd - rangeStart)];
    }

    // convert data to base64
    NSString *stringBuffer = [Utils encodeBase64WithData:data];

    // call JavaScript function with the data
    // setTimeout is used to make sure that it returns asynchronously
    NSString *jsFunction = [NSString stringWithFormat:@"setTimeout(function() {window.CoreControls.PartRetrievers.IOSPartRetriever.PartSuccess('%@', %d);}, 0)", stringBuffer, originalRangeStart];
    [webView stringByEvaluatingJavaScriptFromString:jsFunction];

    return NO;
}
return YES;
}

But my document is saved in the "tmp" folder of the application, as it is downloaded from server. and I m unable to load the document from the tmp folder. Please help.

I cannot move the document from "tmp" folder to "xod" as I cannot change the Bundle at runtime and the document is also used in other places.

When I added the document in "xod" folder in the bundle it loads fines. But when I try to load the doucment from "tmp" folder in the application it doesnot load. I tried appending the path of document in "stringWithQuery", but no luck.

The main problem is "stringWithQuery". Please help.

I want to load the document from "tmp" folder.

Thanks in advance.

Upvotes: 1

Views: 611

Answers (1)

James
James

Reputation: 902

The problem appears to be that the sample code is written to read the .xod file from the app bundle, but you are downloading the xod file to the temporary directory. So you need to change

NSString* fullPath = [[NSBundle mainBundle] pathForResource:docPath ofType:@"xod"];

to

NSString* fullPath = [[NSTemporaryDirectory() stringByAppendingPathComponent:docPath] stringByAppendingPathExtension:@"xod"];

Upvotes: 2

Related Questions