0

I am trying to create the PDF and then load the pdf in webview for preview. Now that i can create the pdf successfully. Next I want to create the pdf by pressing button and then load in the webview. I have referenced this link and this link. Although the pdf is successfully created, the pdf cannot load in the webview. Could anyone provide suggestion? Thank you very much.

The code:

-(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;
}
Community
  • 1
  • 1
user3472143
  • 79
  • 2
  • 11

2 Answers2

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

I don't see anywhere where you actually add the UIWebView as a subView.

[self.view addSubview:webView];

Also. you should add the UIWebView to a property instead of allocating/creating it everytime someone presses a button.

@property (strong, nonatomic) UIWebView *webView;

Finally:

You should use a WKWebView instead of an UIWebView as mentioned in Apple Documentations:

Starting in iOS 8.0 and OS X 10.10, use WKWebView to add web content to your app. Do not use UIWebView or WebView.

EDIT:

For people reading this who actually runs in to the problem as the question actually implies check these other threads on the same question:

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

Community
  • 1
  • 1
  • Are you suggesting in comment above that this question is a duplicate? If so, I don't understand the non-close vote or the answer. – danh Feb 18 '17 at 17:07
  • @danh I had retracted the duplicate flag already before posting this answer after reading through the code I saw all the errors that had nothing to do with the question at all, I took the time to give him the solution but let the comments still be there for future reference incase someone ran into this question with the actual problem to the question. –  Feb 18 '17 at 17:11
  • I understand. Perhaps improve this answer with those links as further reference? Then the duplicate-implying comment can go away. – danh Feb 18 '17 at 17:14
  • Sorry, the question has some duplicate with other post. – user3472143 Feb 18 '17 at 17:28
  • @user3472143 Glad to help. No worries since you posted code and made a good question we could help you solve it :) –  Feb 18 '17 at 17:29
0

You can use the Apple Quick look framework to preview the pdf. You will also get the sharing option through which user can share the pdf through mail or save it to iBooks for later reference.

You can refer my blog link for complete description

Nilesh Jha
  • 1,626
  • 19
  • 35