2013-10-08 149 views
1

我试图从HTML(webview)创建PDF。我做的。但是,我面临严重的问题是我得到1垂直&每个页面上的水平线。正如我附在这个链接。从HTML(webview)创建PDF时的白线

Click for image 如果您看不到线条,请将其缩放。

我试过这个代码

// Creates a mutable data object for updating with binary data, like a byte array 
    NSMutableData *pdfData = [NSMutableData data]; 

    // Points the pdf converter to the mutable data object and to the UIView to be converted 
    UIGraphicsBeginPDFContextToData(pdfData, webView.bounds, nil); 
    UIGraphicsBeginPDFPage(); 
    CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData 

    [webView.layer renderInContext:pdfContext]; 

    // remove PDF rendering context 
    UIGraphicsEndPDFContext(); 

    // Retrieves the document directories from the iOS device 
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 

    NSString* documentDirectory = [documentDirectories objectAtIndex:0]; 
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"tmp.pdf"]; 

    // instructs the mutable data object to write its context to a file on disk 
    [pdfData writeToFile:documentDirectoryFilename atomically:YES]; 
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); 


     // Creates a mutable data object for updating with binary data, like a byte array 
    NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"]; 

    int height = [heightStr intValue]; 
    // CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    // CGFloat screenHeight = (self.contentWebView.hidden)?screenRect.size.width:screenRect.size.height; 
    CGFloat screenHeight = webView.bounds.size.height; 
    int pages = ceil(height/screenHeight); 

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

    // instructs the mutable data object to write its context to a file on disk 
    [pdfData writeToFile:self.PDFpath atomically:YES]; 
    [webView setFrame:frame]; 

任何一个有这个想法?

+0

我试了一整天.. Plz帮助... –

回答

0

好吧,我明白了。这不是我上面使用的代码的行,从UIWebView的每个页面的4个部分,并将它们打印到PDF作为图像。所以这是这些部分之间的差距。