在ios中生成PDF文件

时间:2013-11-14 04:50:11

标签: iphone

您好,在我的应用程序中,我必须生成PDF文件。该pdf文件可能包含图像,描述,数字,边框线和页码。任何人都可以告诉我如何在ios中生成pdf文件。

enter image description here

1 个答案:

答案 0 :(得分:4)

创建自定义视图以添加图像,tableview或任何内容。将pdf转换器指向可变数据对象,并指向要转换为pdf的UIView。

试试这个:

NSMutableData *pdfData = [NSMutableData data];
    UIGraphicsBeginPDFContextToData(pdfData,self.bounds, nil);
    UIGraphicsBeginPDFPage();

    CGContextRef pdfContext = UIGraphicsGetCurrentContext();

    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
    [self.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:@"ess.pdf"];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];