2017-02-18 65 views
0

我正在尝试创建PDF,然后在webview中加载PDF以进行预览。现在我可以成功创建pdf。接下来,我想通过按下按钮创建pdf,然后在webview中加载。我参考了this linkthis link。虽然PDF已成功创建,但PDF无法加载到Web视图中。 任何人都可以提供建议吗?非常感谢你。ios - 在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; 
} 

回答

1
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)]; 

我没有看到任何地方你实际添加UIWebViewsubView

[self.view addSubview:webView]; 

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

@property (strong, nonatomic) UIWebView *webView; 

最后:

开始在IOS 8.0和OS X 10.10,使用WKWebView添加网页内容 :

如苹果之证件提到你应该使用WKWebView代替UIWebView到您的应用程序。 请勿使用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

+0

你在上面的评论暗示这个问题是一个重复?如果是这样,我不理解非接近投票或答案。 – danh

+0

@danh在阅读代码之后,我已经收回了重复标记,然后在阅读完代码后,我看到所有与这个问题无关的错误,我花时间给他解决方案,但让评论仍然存在在未来的参考之下,有人会遇到这个问题,并回答这个问题的实际问题。 – 2017-02-18 17:11:27

+0

我明白。也许用这些链接改进这个答案作为进一步的参考?然后重复的暗示评论可以消失。 – danh

0

您可以使用Apple快看看框架预览PDF文件。您还将获得共享选项,通过该选项用户可以通过邮件共享PDF或将其保存到iBook以供日后参考。

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