2012-04-12 75 views
8

我有一个长的HTML页面,并希望将其转换为多页PDF文件。iOS - 从HTML内容创建多页PDF

对于给定的字符串内容,我遵循 applehere - how to make multi page PDF中提供的说明。

但格式化NSString(有一些像数据)比创建一个HTML页面是困难的。我已经创建了这个html并在UIWebView中显示它。

现在我想创建一个PDF从这个HTML到多页 PDF文件。

我使用的代码可以创建单个PDF。

 
- (void)createPDFfromUIView:(UIWebView *)aView saveToDocumentsWithFileName:(NSString*)aFilename 
{ 
    // 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, aView.bounds, nil); 
    UIGraphicsBeginPDFPage(); 
    CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 


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

    [aView.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:aFilename]; 

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

任何一个可以帮助吗?

回答

14

我根据我发现的每一个好建议创建了一个类。我一直在挖掘很多,我希望我的课程能够为尝试从某些HTML源代码直接创建多页PDF的人提供一些良好的开端。

你会一些基本的示例代码在这里找到整个代码:https://github.com/iclems/iOS-htmltopdf

我刚刚同一个问题,因为你和我的要求是: - 完整的PDF(真正的文本,没有位图) - 智能多页(与每X像素切割全高度webview相比...)

因此,我使用的解决方案非常好,因为它使用iOS用于分割页面以供打印的相同工具。

让我解释一下,我安装一个UIPrintPageRenderer基于Web视图的打印格式(第一尖):

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, CGRectZero, 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; 
} 
+0

Wehrung你是男人。我只是看了一下示例应用程序及其所需的全部内容。但是我的要求变了,做了其他的事情。但这真棒。我可能会在将来使用它。感谢您的努力和专门解释的事情。 – Viraj 2012-11-15 05:40:55

+0

这个框架的问题是它不会捕获任何文本框中的任何数据。他们都将是空白的。 – GuybrushThreepwood 2013-08-29 11:17:57

+0

它很棒!真棒人! – Rudi 2014-09-28 10:59:54