我通过读取webView的内部HTML内容并通过读取AsciiString来使用这两种方法,但是当我将其转换为PDF格式数据截断时,在两种方法中。请查看我的代码并告诉我我的错误。
-(NSData *)createPDFfromUIWebView:(UIWebView*)webView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
[self removeFileByName];
// NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];
// NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
// NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];
// NSString *heightStr = (NSString *)[webView stringByEvaluatingJavaScriptFromString:@"getParam();"];
// NSString *heightStr = (NSString *)[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerText;"];
// NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.all[0].innerHTML"];
NSURL *requestURL = [[self.webViewIphone request] URL];
NSError *error;
NSString *heightStr = [NSString stringWithContentsOfURL:requestURL
encoding:NSASCIIStringEncoding
error:&error];
debug(@"HTml string %@",heightStr);
int height = [heightStr length];
CGFloat screenHeight = webView.scrollView.contentSize.height;
debug(@"%f",screenHeight);
int pages = ceil(height / screenHeight);
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, webView.bounds, nil);
CGRect frame = [webView frame];
for (int i = 0; i < pages; i++) {
// Check to screenHeight if page draws more than the height of the UIWebView
if ((i+1) * screenHeight > height) {
CGRect f = [webView frame];
f.size.height -= (((i+1) * screenHeight) - height);
[webView setFrame: f];
}
UIGraphicsBeginPDFPage();
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// CGContextTranslateCTM(currentContext, 72, 72); // Translate for 1" margins
[[[webView subviews] lastObject] setContentOffset:CGPointMake(0, screenHeight * i) animated:NO];
[webView.layer renderInContext:currentContext];
}
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSData *dataTemp = [NSData dataWithContentsOfFile:documentDirectoryFilename];
return dataTemp;
}
答案 0 :(得分:1)
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName =[docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.xlsx",downloadedfilename]];
[[NSFileManager defaultManager] createFileAtPath:reportName contents:receivedData attributes:nil];
NSURL *url = [NSURL fileURLWithPath:fileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
您可以加载xlsx,pdf,word文档文件,如上面的代码
答案 1 :(得分:0)
这用于将pdf文件加载到UIWebview中。
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSURL *targetURL = [NSURL URLWithString:@"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];