Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
932 views
in Technique[技术] by (71.8m points)

objective c - Failed to download and open a PDF file with error failed to find PDF header in ios

I am having an app in which I am opening a PDF file from a url.

I am successfully able to open it in a webView.

Now I want to download that PDF file and save it in my documents folder and want to send that PDF file in my mail.

I searched a lot and found the best solution below.

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"myfile.pdf"];

    if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){

        NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL  URLWithString:@"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"]];

        //Store the downloaded file  in documents directory as a NSData format
        [pdfData writeToFile:filePath atomically:YES];
    }


    NSURL *url = [NSURL fileURLWithPath:filePath];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView setUserInteractionEnabled:YES];
    [webView setDelegate:self];
    webView.scalesPageToFit = YES;
    [webView loadRequest:requestObj];

But when i try this code, it gives me error saying failed to find PDF header: `%PDF' not found

I am even not able to open my PDF also.

I know same questions are asked before but I am not able to solve this error right now.

I don't know what I am doing wrong over here.

Sorry for the inconvenience.

Please help me with this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try this code for downloading & saving your pdf file

    UIActivityIndicatorView *indicator=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

   [indicator startAnimating];

    NSLog(@"Downloading Started");
    NSString *urlToDownload = @"http://gradcollege.okstate.edu/sites/default/files/PDF_linking.pdf";
    NSURL  *url = [NSURL URLWithString:urlToDownload];
    NSURLRequest *request=[[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:120.0f];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        [indicator stopAnimating];

        if (!connectionError) {

            if ( data )
            {
                NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString  *documentsDirectory = [paths objectAtIndex:0];

                NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"myfile.pdf"];

                //saving is done on main thread
                dispatch_async(dispatch_get_main_queue(), ^{
                    [data
                     writeToFile:filePath atomically:YES];
                    NSLog(@"File Saved !");

                    NSURL *url = [NSURL fileURLWithPath:filePath];
                    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
                    [_rssWebView setUserInteractionEnabled:YES];
                    //[_rssWebView setDelegate:self];
                    [_rssWebView loadRequest:requestObj];
                });
            }
        }

    }];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...