2013-08-01 37 views
0
將HTML轉換爲PDF

我使用下面的代碼的HTML轉換爲PDF格式(如張貼cwehrung在各種各樣的地方)與iOS上

UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init]; 

[render addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:0]; 

CGRect printableRect = CGRectMake(self.pageMargins.left, 
           self.pageMargins.top, 
           self.pageSize.width - self.pageMargins.left - self.pageMargins.right, 
           self.pageSize.height - self.pageMargins.top - self.pageMargins.bottom); 

CGRect paperRect = CGRectMake(0, 0, self.pageSize.width, self.pageSize.height); 

[render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"]; 
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"]; 

NSData *pdfData = [render printToPDF]; 

[pdfData writeToFile: self.PDFpath atomically: YES]; 

問題上創建UIPrintPageRenderer類別支持:

-(NSData*) printToPDF 
{ 
    [self doNotRasterizeSubviews:self.view]; 

    NSMutableData *pdfData = [NSMutableData data]; 

    UIGraphicsBeginPDFContextToData(pdfData, self.paperRect, nil); 

    [self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)]; 

    CGRect bounds = UIGraphicsGetPDFContextBounds(); 

    for (int i = 0 ; i < self.numberOfPages ; i++) 
    { 
     UIGraphicsBeginPDFPage(); 

     [self drawPageAtIndex: i inRect: bounds]; 
    } 

    UIGraphicsEndPDFContext(); 

    return pdfData; 
} 

我遇到的問題是圖像和文本之間的對齊。

我將HTML文件上傳到here,很容易在轉換後的PDF文件中看到問題,我上傳到here。 (見第三本書和第六本書)

任何幫助將不勝感激!

+0

的Theres心不是一個差的太多....我想這是很好 – IronManGill

+0

你可以改變框架pdf。 – user2545330

+0

in UIGraphicsBeginPDFContextToData 嘗試設置邊界 根據Apple Documents:bound - 一個指定PDF頁面的默認大小和位置的矩形。 (此值用作每個新頁面的默認媒體框。)矩形的原點通常應爲(0,0)。指定空矩形(CGRectZero)將默認頁面大小設置爲8.5×11英寸(612×792點)。 嘗試將CGRectZero更改爲另一個值.. :) – iProgrammer

回答

1

我們可以HTML轉換成PDF在UIWebView的另一way-

中打開HTML和代碼是 -

-(void)display 
{ 
webViewHeight = [[self.printWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] integerValue]; 

CGRect screenRect = self.printWebView.frame; 

double currentWebViewHeight = webViewHeight; 


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *imageCacheDirPath = [documentsDirectory stringByAppendingPathComponent:@"pdfImages"]; 
if (![[NSFileManager defaultManager] fileExistsAtPath: imageCacheDirPath]) 
{ 
    [[NSFileManager defaultManager] createDirectoryAtPath:imageCacheDirPath withIntermediateDirectories:NO attributes:nil error:NULL]; 
} 
else 
{ 
    NSError *error; 
    [[NSFileManager defaultManager] removeItemAtPath: imageCacheDirPath error: &error]; 
    [[NSFileManager defaultManager] createDirectoryAtPath:imageCacheDirPath withIntermediateDirectories:NO attributes:nil error:NULL]; 
} 

while (currentWebViewHeight > 0) 
{ 
    imageName ++; 
    UIGraphicsBeginImageContext(screenRect.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    [[UIColor blackColor] set]; 
    CGContextFillRect(ctx, screenRect); 
    [self.printWebView.layer renderInContext:ctx]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    NSString *pngPath = [imageCacheDirPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",imageName]]; 
    if(currentWebViewHeight < 440) 
    { 
     CGRect lastImageRect = CGRectMake(0,440-currentWebViewHeight, self.printWebView.frame.size.width, currentWebViewHeight); 
     CGImageRef lastImageRef = CGImageCreateWithImageInRect([newImage CGImage], lastImageRect); 
     newImage = [UIImage imageWithCGImage:lastImageRef]; 
     CGImageRelease(lastImageRef); 
    } 
    [UIImagePNGRepresentation(newImage) writeToFile:pngPath atomically:YES]; 
    [self.printWebView stringByEvaluatingJavaScriptFromString:@"window.scrollBy(0,440);"]; 
    currentWebViewHeight -= 440; 
} 
[self drawPdf]; 
} 

- (void) drawPdf 
{ 
CGSize pageSize = CGSizeMake(612, webViewHeight); 
NSString *fileName = @"Demo.pdf"; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *imageCacheDirPath = [documentsDirectory stringByAppendingPathComponent:@"pdfImages"]; 

NSString *pdfFileName = [imageCacheDirPath stringByAppendingPathComponent:fileName]; 
NSLog(@"file path:%@",pdfFileName); 

UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil); 

// Mark the beginning of a new page. 
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil); 

double currentHeight = 0.0; 
for (int index = 1; index <= imageName ; index++) 
{ 
    NSString *pngPath = [imageCacheDirPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", index]]; 
    UIImage *pngImage = [UIImage imageWithContentsOfFile:pngPath]; 

    [pngImage drawInRect:CGRectMake(0, currentHeight, pageSize.width, pngImage.size.height)]; 
    currentHeight += pngImage.size.height; 
} 
UIGraphicsEndPDFContext(); 
} 
+0

這很好,除非你真的希望PDF不是一堆圖像。在這種情況下,它是無用的。要製作可搜索的PDF(例如,將文本視爲文本並將圖像視爲圖像),您不能將其渲染爲圖像。 – Bergasms

+0

好的檢查http://stackoverflow.com/questions/5817840/print-paper-size-and-content-inset/5988357#5988357可能會幫助 – RMRAHUL

+0

可搜索PDF可能是我的方式去,所以代碼不會做工作對我來說......但是投你一票爲你的努力。 – Joshua