I am aware of taking the screenshot of UIWebView and converting it to PDF but I need to generate a proper pdf (text as text and not screenshot). Save2PDF is an application which creates proper PDF. Does anybody have an idea how do they do it?
我知道获取UIWebView的截图并将其转换为PDF但我需要生成一个正确的pdf(文本作为文本而不是截图)。 Save2PDF是一个创建适当PDF的应用程序。有人知道他们是怎么做到的吗?
2 个解决方案
#1
30
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.
我根据我发现的每一个好建议创建了一个课程。我一直在挖掘很多东西,我希望我的课程能为那些试图直接从一些HTML源代码创建多页PDF的人提供一些良好的开端。
You'll find the whole code here with some basic sample code : https://github.com/iclems/iOS-htmltopdf
你会在这里找到一些基本的示例代码:https://github.com/iclems/iOS-htmltopdf
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...)
我和你有同样的问题,我的要求是: - 完整的PDF(真实文本,没有位图) - 智能多页(与每X像素切割一个全高度webview相比......)
Thus, the solution I use is pretty nice as it resorts to the same tools iOS uses to split pages for print.
因此,我使用的解决方案相当不错,因为它采用iOS用于分割打印页面的相同工具。
Let me explain, I setup a UIPrintPageRenderer based on the web view print formatter (first tip) :
让我解释一下,我根据Web视图打印格式化程序设置了一个UIPrintPageRenderer(第一个提示):
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];
In the meantime, I have created a category on UIPrintPageRenderer to support:
与此同时,我在UIPrintPageRenderer上创建了一个类别来支持:
- (NSData*) printToPDF
{
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;
}
#2
-1
The entire point of this question was text as text and not screenshot
这个问题的重点是文本而不是截图
The following answer shows how to make a bitmap screenshot, in a pdf container...
以下答案显示了如何在pdf容器中制作位图截图...
#import <QuartzCore/CALayer.h>
- (NSString *)pdfPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"tmp.pdf"];
return writableDBPath;
}
- (NSDictionary *)pdfContextDictionary {
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:@"J. P. Author", kCGPDFContextAuthor,
@"My Cool App", kCGPDFContextCreator,
@"This is the Output", kCGPDFContextTitle,
nil];
return d;
}
- (void)printToPDF {
NSString *path = [self pdfPath];
UIView *displayView = self.view;
CGRect pageRect = displayView.bounds;
if (UIGraphicsBeginPDFContextToFile(path, pageRect, [self pdfContextDictionary]) == NO) {
return; // error
}
UIGraphicsBeginPDFPage();
CGContextRef context = UIGraphicsGetCurrentContext();
CALayer *layer = displayView.layer;
[layer renderInContext:context];
UIGraphicsEndPDFContext();
NSLog(@"[%@ %@] %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), path);
}
#1
30
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.
我根据我发现的每一个好建议创建了一个课程。我一直在挖掘很多东西,我希望我的课程能为那些试图直接从一些HTML源代码创建多页PDF的人提供一些良好的开端。
You'll find the whole code here with some basic sample code : https://github.com/iclems/iOS-htmltopdf
你会在这里找到一些基本的示例代码:https://github.com/iclems/iOS-htmltopdf
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...)
我和你有同样的问题,我的要求是: - 完整的PDF(真实文本,没有位图) - 智能多页(与每X像素切割一个全高度webview相比......)
Thus, the solution I use is pretty nice as it resorts to the same tools iOS uses to split pages for print.
因此,我使用的解决方案相当不错,因为它采用iOS用于分割打印页面的相同工具。
Let me explain, I setup a UIPrintPageRenderer based on the web view print formatter (first tip) :
让我解释一下,我根据Web视图打印格式化程序设置了一个UIPrintPageRenderer(第一个提示):
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];
In the meantime, I have created a category on UIPrintPageRenderer to support:
与此同时,我在UIPrintPageRenderer上创建了一个类别来支持:
- (NSData*) printToPDF
{
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;
}
#2
-1
The entire point of this question was text as text and not screenshot
这个问题的重点是文本而不是截图
The following answer shows how to make a bitmap screenshot, in a pdf container...
以下答案显示了如何在pdf容器中制作位图截图...
#import <QuartzCore/CALayer.h>
- (NSString *)pdfPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"tmp.pdf"];
return writableDBPath;
}
- (NSDictionary *)pdfContextDictionary {
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:@"J. P. Author", kCGPDFContextAuthor,
@"My Cool App", kCGPDFContextCreator,
@"This is the Output", kCGPDFContextTitle,
nil];
return d;
}
- (void)printToPDF {
NSString *path = [self pdfPath];
UIView *displayView = self.view;
CGRect pageRect = displayView.bounds;
if (UIGraphicsBeginPDFContextToFile(path, pageRect, [self pdfContextDictionary]) == NO) {
return; // error
}
UIGraphicsBeginPDFPage();
CGContextRef context = UIGraphicsGetCurrentContext();
CALayer *layer = displayView.layer;
[layer renderInContext:context];
UIGraphicsEndPDFContext();
NSLog(@"[%@ %@] %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), path);
}