ios - 在webview中创建并加载pdf

时间:2017-02-18 16:44:25

标签: ios objective-c pdf uiwebview

我正在尝试创建PDF,然后在webview中加载pdf进行预览。现在我可以成功创建pdf了。接下来我想通过按下按钮创建pdf,然后加载webview。我引用了this linkthis link。尽管已成功创建pdf,但无法在webview中加载pdf。 任何人都可以提供建议吗?非常感谢你。

代码:

-(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;
}

2 个答案:

答案 0 :(得分:1)

prePersist

我没有看到您将UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)]; 实际添加为UIWebView的任何地方。

subView

另外。您应该将[self.view addSubview:webView]; 添加到UIWebView,而不是每当有人按下按钮时分配/创建它。

property

最后:

您应该使用Apple文档中提到的WKWebView而不是@property (strong, nonatomic) UIWebView *webView;

  

从iOS 8.0和OS X 10.10开始,使用WKWebView添加Web内容   到你的应用程序。 不要使用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

答案 1 :(得分:0)

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

您可以参考我的博客link获取完整说明