无法下载并打开PDF文件,但错误无法在ios中找到PDF标题 [英] Failed to download and open a PDF file with error failed to find PDF header in ios

查看:1351
本文介绍了无法下载并打开PDF文件,但错误无法在ios中找到PDF标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我在其中打开一个URL文件。

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

我已成功在webView中打开它。

I am successfully able to open it in a webView.

现在我想下载该PDF文件并将其保存在我的文件夹中,并希望通过我的邮件发送该PDF文件。

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];

但是当我尝试使用此代码时,它会让我错误地说找不到PDF标题: '%PDF'未找到

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

我甚至无法打开我的PDF。

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.

请帮帮我。

推荐答案

试试这个代码下载&保存你的pdf文件

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];
                });
            }
        }

    }];

这篇关于无法下载并打开PDF文件,但错误无法在ios中找到PDF标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆