【问题标题】:Issue in creating pdf from UIView in iPad Application在 iPad 应用程序中从 UIView 创建 pdf 的问题
【发布时间】:2011-08-29 09:49:41
【问题描述】:

我正在我的 iPad 应用程序中从 UIView 创建一个 pdf。它的大小为 768 * 2000。当我创建 pdf 时,它会以相同的大小创建,并在一页上显示所有内容。所以当我从 iPad 打印时我遇到了问题。我正在使用以下代码来创建 pdf:-

-(void)drawPdf:(UIView *)previewView{   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Waypoint Data.pdf"];
    //CGRect tempRect = CGRectMake(0, 0, 768, 1068);
    CGContextRef pdfContext = [self createPDFContext:previewView.bounds path:(CFStringRef)writableDBPath];
    CGContextBeginPage (pdfContext,nil); // 6

    //turn PDF upsidedown

    CGAffineTransform transform = CGAffineTransformIdentity;    
    transform = CGAffineTransformMakeTranslation(0, previewView.bounds.size.height);
    transform = CGAffineTransformScale(transform, 1.0, -1.0);
    CGContextConcatCTM(pdfContext, transform);

    //Draw view into PDF
    [previewView.layer renderInContext:pdfContext]; 
    CGContextEndPage (pdfContext);// 8
    CGContextRelease (pdfContext);  
}

//Create empty PDF context on iPhone for later randering in it

-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path{

    CGContextRef myOutContext = NULL;

    CFURLRef url;

    url = CFURLCreateWithFileSystemPath (NULL, // 1

                                     path,

                                     kCFURLPOSIXPathStyle,

                                     false);

    if (url != NULL) {

        myOutContext = CGPDFContextCreateWithURL (url,// 2

                                              &inMediaBox,                                                NULL);        
        CFRelease(url);// 3     
    }   
    return myOutContext;// 4    
} 

谁能建议我如何减小 pdf 大小并且它有多个页面?

提前致谢。

【问题讨论】:

    标签: iphone ios ipad uiview airprint


    【解决方案1】:

    参见“iOS 绘图和打印指南”中的示例

    https://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html#//apple_ref/doc/uid/TP40010156-CH10-SW1

    基本上在清单 4-1 代码示例中,他们有一个 do while 循环,并注意它是如何在循环中启动一个新的 PDF 页面的:

    ...

    // Mark the beginning of a new page.
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
    

    ...

    在您当前的方法中,您只调用了一次开始页面方法之一,因此您将永远只有一个页面。

    【讨论】:

      【解决方案2】:

      您需要为要创建的每个新 PDF 页面调用 UIGraphicsBeginPDFPage。假设您有一个高度可变的 UIView,您可以在运行时将其分解为尽可能多的 PDF 页面:

      NSInteger pageHeight = 792; // Standard page height - adjust as needed
      NSInteger pageWidth = 612; // Standard page width - adjust as needed
      
      /* CREATE PDF */
      NSMutableData *pdfData = [NSMutableData data];
      UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0,0,pageWidth,pageHeight), nil);
      CGContextRef pdfContext = UIGraphicsGetCurrentContext();
      for (int page=0; pageHeight * page < theView.frame.size.height; page++)
      {
          UIGraphicsBeginPDFPage();
          CGContextTranslateCTM(pdfContext, 0, -pageHeight * page);
          [theView.layer renderInContext:pdfContext];
      }
      
      UIGraphicsEndPDFContext();
      

      【讨论】:

        猜你喜欢
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        • 2013-05-12
        • 2013-11-23
        • 1970-01-01
        • 2011-11-22
        • 2011-03-23
        • 1970-01-01
        相关资源
        最近更新 更多