17

我知道截取 UIWebView 的屏幕截图并将其转换为 PDF,但我需要生成正确的 pdf(文本作为文本而不是屏幕截图)。Save2PDF 是一个创建正确 PDF 的应用程序。有人知道他们是如何做到的吗?

4

2 回答 2

32

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.

You'll find the whole code here with some basic sample code : 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...)

Thus, the solution I use is pretty nice as it resorts to the same tools iOS uses to split pages for print.

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];

In the meantime, I have created a category on UIPrintPageRenderer to support:

- (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;
}
于 2012-11-12T11:29:09.537 回答
-1

这个问题的重点是文本作为文本而不是截图

以下答案显示了如何在 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);
}
于 2012-06-04T13:49:21.533 回答