iOS - 从HTML内容创建多页PDF [英] iOS - Creating multi page PDF from HTML content
问题描述
我有一个很长的html页面,想要将其转换为多页PDF文件。
I have a long html page and wanted to convert it in to a multipage PDF file.
我已按照
apple 和此处 - 如何为给定的字符串内容制作多页PDF 。
但是格式化NSString(有一些类似的数据)比创建一个html页面困难。我创建了这个html并在UIWebView中显示它。
But the formatting the NSString ( with some like data ) is difficult than creating a html page. I have created this html and displaying it in a UIWebView.
现在我想用这个HTML创建一个PDF到多页 PDF文件。
Now I want to create a PDF out of this HTML to a Multi page PDF file.
我正在使用的代码可以创建单个PDF。
The code I'm using can create single 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);
}
任何人都可以提供帮助吗?
Can any one give some help ?
推荐答案
我根据我发现的每一个好建议创建了一个类。我一直在挖掘很多东西,我希望我的课程能为那些试图直接从某些HTML源代码创建多页PDF的人提供一个良好的开端。
I created a class based on every good advice I found around. I've been digging a lot and I hope my class will offer some good start for anyone trying to create multi-page PDF directly out of some HTML source.
你'我会在这里找到一些基本的示例代码: https://github.com/iclems/iOS-htmltopdf
You'll find the whole code here with some basic sample code : https://github.com/iclems/iOS-htmltopdf
我和你有同样的问题我的要求是:
- 完整PDF(真实文本,无位图)
- 智能多页(与每X像素切割一个全高webview相比......)
I had just the same issue as you and my requirements were: - full PDF (real text, no bitmap) - smart multi-pages (compared to cutting a full height webview every X pixels...)
因此,我使用的解决方案相当不错,因为它采用了iOS用于拆分的相同工具打印页面。
Thus, the solution I use is pretty nice as it resorts to the same tools iOS uses to split pages for print.
让我解释一下,我根据网页视图打印格式化程序设置了一个UIPrintPageRenderer(第一个提示):
Let me explain, I setup a UIPrintPageRenderer based on the web view print formatter (first tip) :
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上创建了一个类别来支持:
In the meantime, I have created a category on UIPrintPageRenderer to support:
-(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;
}
这篇关于iOS - 从HTML内容创建多页PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!