xcode 未能找到 PDF 标题:未找到“%PDF”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25158552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:20:20  来源:igfitidea点击:

failed to find PDF header: `%PDF' not found

iosobjective-cxcodepdfxcode6

提问by Marius Sch?nefeld

I get this log when i try to load a pdf and don't know why.

当我尝试加载 pdf 时收到此日志,但不知道为什么。

failed to find PDF header: `%PDF' not found.

hers my code:

她是我的代码:

- (void)viewDidLoad
{


    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://webdesign-schoenefeld.de/es-app/heute.pdf"]]];
    NSLog(@"---------------pdfloading....,------------------------");
    [webview addSubview:activityind];
    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0)
                                             target:self
                                           selector:@selector(loading)
                                           userInfo:nil
                                            repeats:YES];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

I use Xcode 6 Beta 5 and iOS 8 beta 5.

我使用 Xcode 6 Beta 5 和 iOS 8 beta 5。

Thanks for your help.

谢谢你的帮助。

采纳答案by Boris Suvorov

Update on SEP 9 2014: iOS8 GM fixed this issue for apps I'm working with.

2014 年 9 月 9 日更新:iOS8 GM 为我正在使用的应用程序修复了此问题。

Response prior to SEP 9 2014 It is reported as a known bug by Apple in iOS8 Beta 5 release notes. https://developer.apple.com/library/prerelease/ios/releasenotes/General/RN-iOSSDK-8.0/https://devforums.apple.com/message/1017919#1017919

2014 年 SEP 9 之前的响应 Apple 在 iOS8 Beta 5 发行说明中报告为已知错误。 https://developer.apple.com/library/prerelease/ios/releasenotes/General/RN-iOSSDK-8.0/ https://devforums.apple.com/message/1017919#1017919

We should wait next release and see if it is fixed.

我们应该等待下一个版本,看看它是否已修复。

回答by Gilles

I agree, there seems to be a bug (or change of behaviour) in iOS 8 beta 5 which means that your code does not work anymore (and it was valid. I have the same and it worked fine up to iOS 8 beta 4).

我同意,在 iOS 8 beta 5 中似乎有一个错误(或行为改变),这意味着你的代码不再工作(而且它是有效的。我有同样的,它在 iOS 8 beta 4 之前工作得很好) .

I think there is a problem with type detection because if I do the following, it works:

我认为类型检测存在问题,因为如果我执行以下操作,它会起作用:

NSLog(@"About to request PDF document loading...");
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlStr]];
[self.webView loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];

Now that bit of code should not be used "as is" as it is synchronous.

现在,不应“按原样”使用该位代码,因为它是同步的。

I feel Nithin's answer was valid but it does not answer the problem specifically seen with iOS 8 beta 5. The main code is the same and will hit the same iOS bug.

我觉得 Nithin 的回答是有效的,但它没有回答在 iOS 8 beta 5 中特别看到的问题。主要代码是相同的,并且会遇到相同的 iOS 错误。

回答by Vaibhav Saran

This worked for me

这对我有用

NSURL *targetURL = [NSURL fileURLWithPath:pdfFilePath]; // <pdfFilePath> if full path of your pdf file
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:targetURL];
[self.webview loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];

回答by serj

I had the same problem in iOS 8 beta 5. The code like https://stackoverflow.com/a/25302602/2639006works, but I would recommend to add last baseURL param, because internal pdf document links doesn't work without it.

我在 iOS 8 beta 5 中遇到了同样的问题。像https://stackoverflow.com/a/25302602/2639006这样的代码有效,但我建议添加最后一个 baseURL 参数,因为没有它,内部 pdf 文档链接不起作用.

NSString *path = [[NSBundle mainBundle] pathForResource:@"pdf_NAME"ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:targetURL];
[self.webView loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:targetURL];

回答by MusiGenesis

I have been beating myself up over this particular problem for years, and finally found the answer today. I've always found that some PDFs open fine in iOS (either with code like yours that tries to load it in a UIWebviewor by using UIApplications's canOpenUrl: method) but others can't be opened at all.

多年来,我一直在为这个特定的问题而自责,今天终于找到了答案。我一直发现有些 PDF 在 iOS 中打开得很好(或者像你这样的代码试图在 a 中加载它,UIWebview或者使用 UIApplications 的 canOpenUrl: 方法),但其他人根本无法打开。

Basically, the PDF needs to have a header/footer set in order to be openable by iOS (technically it's actually just Safari that has this problem, as Quicklook and Acrobat Reader can handle header-less PDFs on the devics with no problem).

基本上,PDF 需要设置页眉/页脚才能被 iOS 打开(从技术上讲,它实际上只是 Safari 有这个问题,因为 Quicklook 和 Acrobat Reader 可以毫无问题地在设备上处理无页眉的 PDF)。

回答by anorskdev

I ran into this error message when sending a PDF document I generated to the printer (at least in the simulator). Got around it by adding a title to the PDF document by supplying a CFDictionary instead of nil as the last parameter to building the pdf context. Here is Swift 3.0 help:

在将生成的 PDF 文档发送到打印机时(至少在模拟器中),我遇到了此错误消息。通过提供 CFDictionary 而不是 nil 作为构建 pdf 上下文的最后一个参数,向 PDF 文档添加标题,从而解决了这个问题。这是 Swift 3.0 帮助:

    let infoDict : CFDictionary = [kCGPDFContextTitle as String : "myTitleHere"] as CFDictionary  // Add title to PDF

    let pdfData = NSMutableData()
    if let dataConsumer : CGDataConsumer = CGDataConsumer(data: pdfData) {
        let myMediaBox = CGRect(x: 0, y: 0, width: page.pageWidth, height: page.pageHeight)
        if let pdfContext = CGContext(consumer: dataConsumer, mediaBox: &myMediaBox, infoDict) {
            // .... more pdf stuff
        }
    }

回答by kevinl

I'm having the same issue building an app off the iOS 8 SDK and Xcode 6. My issue was that NSXMLParser was calling the foundCharacters method twice on my selected tag. I figured this out by checking if my string data was a multiple of 4 so that I could even attempt to convert it to a base 64 encoded data object. Once I found out that the method was called twice, I figured that concatenating the strings together would produce the correct data:

我在从 iOS 8 SDK 和 Xcode 6 构建应用程序时遇到了同样的问题。我的问题是 NSXMLParser 在我选择的标签上调用了 foundCharacters 方法两次。我通过检查我的字符串数据是否是 4 的倍数来解决这个问题,这样我什至可以尝试将其转换为 base 64 编码的数据对象。一旦我发现该方法被调用了两次,我认为将字符串连接在一起会产生正确的数据:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if([self.currentElement isEqualToString:kWarrantyPDFXMLTagDocumentData]) {

        if (self.combinedDocumentString == nil || self.combinedDocumentString.length == 0) {
            self.combinedDocumentString = string;
        }
        else {
            self.combinedDocumentString = [self.combinedDocumentString stringByAppendingString:string];
        }

        if (![self stringIsBase64:self.combinedDocumentString]) {
            return;
        }
        pdfData = [[NSData alloc] initWithData:[NSData base64DataFromString:self.combinedDocumentString]];

        // If you want to save your PDF, do so here.
        NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"test.pdf"];
        [pdfData writeToFile:path atomically:YES];
    }
}



- (BOOL)stringIsBase64:(NSString *)str {
    if ([str length] % 4 == 0) {
        static NSCharacterSet *invertedBase64CharacterSet = nil;
        if (invertedBase64CharacterSet == nil) {
            invertedBase64CharacterSet = [[NSCharacterSet
                                        characterSetWithCharactersInString:
                                        @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="]
                                       invertedSet];
        }
        return [str rangeOfCharacterFromSet:invertedBase64CharacterSet
                                 options:NSLiteralSearch].location == NSNotFound;
    }
    return NO;
}