【问题标题】:Obj-C View to PDF only renders first pageObj-C View to PDF 只呈现第一页
【发布时间】:2012-05-04 17:51:10
【问题描述】:

我有一个名为 UIView+PDFView 的 UIView 扩展类,它采用当前视图,将其拆分为页面,并呈现 PDF 文档。我的问题在于渲染部分;我可以成功地为内容“创建”页面,但是第一个页面之后的所有页面都是空白的。我的最终目标是获取当前视图,“缩放”等于页面的宽度,然后在 PDF 中对视图的其余部分进行分页。

问题不是什么:

  • 将 PDF 附加到电子邮件中
  • 将 PDF 发送到打印机/打印模拟器
  • 在实际正确生成 PDF 时只打印一页
  • 正确调整视图大小(SO Question 4919388)
    • 我在发送到方法之前执行此操作。通过使框架高 10 像素进行验证,仍会打印一页(且仅一页)整页
  • 正确翻译视图。通过翻译和比例进行验证;两者都正确更改了视图,但都没有在第一页以上呈现。

我的代码如下:

@interface UIView (PDFView)
-(id)createPDFAndSaveToDocumentsWithFileName:(NSString*)aFilename andDocumentInfo:(NSDictionary *)documentInfo;
@end

@implementation UIView (PDFView)

-(id)createPDFAndSaveToDocumentsWithFileName:(NSString*)aFilename andDocumentInfo:(NSDictionary *)documentInfo {
    // http://developer.apple.com/library/ios/DOCUMENTATION/GraphicsImaging/Reference/CGPDFContext/Reference/reference.html#//apple_ref/doc/constant_group/Auxiliary_Dictionary_Keys

    // 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
    CGSize pageSize = CGSizeMake(self.bounds.size.width, 792);
    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, documentInfo);
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();

    NSInteger currentPage = 0;
    BOOL done = NO;

    do {
        CGRect currentPageRect = CGRectMake(0, (pageSize.height*currentPage), pageSize.width, pageSize.height);
        UIGraphicsBeginPDFPageWithInfo(currentPageRect, nil);

        // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
        [self.layer renderInContext:pdfContext];

        // If we're at the end of the view, exit the loop.
        if ((pageSize.height*currentPage) > self.bounds.size.height) {
            done = YES;
        } else {
            currentPage++;
        }
    } while (!done);

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    if (aFilename == nil) {
        return pdfData;
    } else {
        // 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);
        return nil;
    }
}

【问题讨论】:

    标签: objective-c ios5 uiview pdf-generation


    【解决方案1】:

    我认为你的问题出在

    CGRect currentPageRect = CGRectMake(0, (pageSize.height*currentPage), pageSize.width, pageSize.height);
    

    请尝试使用以下任一语句

    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, 612.0, 792.0), nil);
    UIGraphicsBeginPDFPage();
    

    每次您希望在上下文中添加新页面时,使用上述语句,您就可以将页面添加到上下文中。

    如果您希望使用页面的默认大小,即 612 X 792,您可以直接使用UIGraphicsBeginPDFPage();

    对于自定义页面大小,您可以使用UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, 612.0, 792.0), nil);

    我认为这应该可以解决您的问题。

    【讨论】:

    • 我使用了您的代码并在 renderInContext: 方法之前添加了下面的转换并得到了我正在寻找的结果,谢谢! CGContextTranslateCTM(pdfContext, 0, -(pageSize.height*currentPage));
    • 谢谢@ScottBonAmi,这正是我的问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2014-06-27
    • 2019-05-02
    • 1970-01-01
    • 2018-09-17
    相关资源
    最近更新 更多