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

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

问题描述

我有一个应用程序,可以在其中从 url 打开 PDF 文件.

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.

很抱歉给您带来不便.

请帮我解决这个问题.

推荐答案

试试这个代码下载&保存您的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天全站免登陆