-2

嗨,在我的应用程序中,我必须生成 PDF 文件。该 pdf 文件可能包括图像、描述、数字、边框线和页码。谁能告诉我如何在 ios 中生成 pdf 文件。

在此处输入图像描述

4

1 回答 1

4

创建您的自定义视图以添加图像、表格视图或任何内容。将 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];
于 2013-11-14T05:39:35.030 回答