2

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;

}  
4

2 に答える 2

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];
于 2013-09-21T06:56:16.937 に答える