【问题标题】:ios - create and load pdf in webviewios - 在 webview 中创建和加载 pdf
【发布时间】:2017-07-08 03:44:28
【问题描述】:

我正在尝试创建 PDF,然后在 webview 中加载 pdf 以进行预览。现在我可以成功创建pdf了。接下来我想通过按下按钮创建pdf,然后加载到webview中。我参考了this linkthis link。虽然 pdf 创建成功,但 pdf 无法在 webview 中加载。 有人可以提供建议吗?非常感谢。

代码:

-(IBAction)preview:(id)sender
{
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
NSDate*todayDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-yyyy "];
NSString *mytext = [NSString stringWithFormat:@"form%@",[dateFormatter stringFromDate:todayDate]];
[self createPDFfromUIView:_myView saveToDocumentsWithFileName:mytext];
NSLog(@"is saved in %@", mytext);
NSString *path = [self createPDFfromUIView:_myView saveToDocumentsWithFileName:mytext];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
}
-(NSMutableData *)createPDFDatafromUIView:(UIView*)aView
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[aView.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
return pdfData;
}
-(NSString*)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
NSMutableData *pdfData = [self createPDFDatafromUIView:aView];
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[aView.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
NSLog(@"saved in %@", documentDirectoryFilename);
return documentDirectoryFilename;
}

【问题讨论】:

    标签: ios objective-c pdf uiwebview


    【解决方案1】:
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
    

    我看不到您实际将UIWebView 添加为subView 的任何地方。

    [self.view addSubview:webView];
    

    还有。您应该将UIWebView 添加到property,而不是每次有人按下按钮时分配/创建它。

    @property (strong, nonatomic) UIWebView *webView;
    

    最后:

    您应该使用 WKWebView 而不是 Apple 文档中提到的 UIWebView

    从 iOS 8.0 和 OS X 10.10 开始,使用 WKWebView 添加网页内容 到您的应用程序。 不要使用 UIWebView 或 WebView。

    编辑:

    对于阅读本文的人实际上遇到了问题,因为问题实际上意味着检查同一问题上的这些其他线程:

    Load text+pdf file in uiwebview

    iPhone : can we open pdf file using UIWebView?

    how to load pdf file in UIWebview center

    Display local pdf file in UIWebView in iphone

    Loading Local PDF File Into WebView

    【讨论】:

    • 您是否在上面的评论中暗示这个问题是重复的?如果是这样,我不明白非接近投票或答案。
    • @danh 在阅读完代码后发布此答案之前,我已经撤回了重复标志我看到了所有与问题无关的错误,我花时间给他解决方案,但让 cmets 仍然存在以供将来参考,以防有人遇到该问题的实际问题。
    • 我明白了。也许用这些链接作为进一步的参考来改进这个答案?然后重复暗示的评论就可以消失了。
    • 对不起,这个问题与其他帖子有些重复。
    • @user3472143 很高兴为您提供帮助。不用担心,因为您发布了代码并提出了一个很好的问题,我们可以帮助您解决它:)
    【解决方案2】:

    您可以使用 Apple Quick Look 框架来预览 pdf。您还将获得共享选项,用户可以通过该选项通过邮件共享 pdf 或将其保存到 iBooks 以供以后参考。

    完整描述可以参考我的博客link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 2019-10-08
      • 2015-08-08
      • 2015-12-09
      相关资源
      最近更新 更多