Ios 在Objective-C中创建具有适当格式和图像的PDF

Ios 在Objective-C中创建具有适当格式和图像的PDF,ios,objective-c,pdf,Ios,Objective C,Pdf,请仔细阅读我的剧本 我有一个UITextView和一个UIImageView文本视图的底部。 每次TextView中都会有动态内容,因此,我要求用户进行签名,它将在底部ImageView中显示为图像 现在的要求是,我必须在一个PDF文件中传递这些详细信息以及签名,因此我必须创建包含TextView文本和ImageView图像的PDF文件 注意:TextView也包含Html文本,因此它在PDF中也应以相同的格式显示。 根据需要检查以下图像和当前PDF。 这是必需的PDF 这是当前的PDF 只

请仔细阅读我的剧本

我有一个
UITextView
和一个
UIImageView
文本视图的底部。 每次TextView中都会有动态内容,因此,我要求用户进行签名,它将在底部ImageView中显示为图像

现在的要求是,我必须在一个PDF文件中传递这些详细信息以及签名,因此我必须创建包含TextView文本和ImageView图像的PDF文件

注意:TextView也包含Html文本,因此它在PDF中也应以相同的格式显示。

根据需要检查以下图像和当前PDF。

这是必需的PDF

这是当前的PDF


只放置对HTML支持和图像与文本合并都有帮助的代码。请不要显示简单的PDF创建,因为我已经这样做了。

试试这个有用的第三方库:

使用此功能解决您的问题:

+ (id)createPDFWithHTML:(NSString*)HTML pathForPDF:(NSString*)PDFpath pageSize:(CGSize)pageSize margins:(UIEdgeInsets)pageMargins successBlock:(NDHTMLtoPDFCompletionBlock)successBlock errorBlock:(NDHTMLtoPDFCompletionBlock)errorBlock;

您不需要第三方库,Cocoa和Cocoa touch具有丰富的PDF支持。我已经给你打了一个小开始,在你的viewController中这样做。可能会有一些小错误,我已经使用swift好几年了,但是我在这里使用了我非常生锈的objC,因为你这样标记了这个问题。有什么问题请告诉我,祝你好运

  -(NSData *)drawPDFdata{

    //  default pdf..
    // 8.5 X 11 inch @72dpi
    // = 612 x 792
    CGRect rct = {{0.0 , 0.0 } , {612.0 , 792.0}}
    NSMutableData *pdfData = [[NSMutableData alloc]init];
    UIGraphicsBeginPDFContextToData(pdfData, rct, nil);
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    UIGraphicsBeginPDFPage();


 //textView drawing
    CGContextSaveGState(pdfContext);
    CGContextConcatCTM(pdfContext, CGAffineTransformMakeTranslation(50.0,50.0));//this  is  just an offset for the textView drawing. You will  want to play with the values, espeecially if supporting multiple screen sizes you might tranform the scale  as well..

    [textView.layer renderInContext:pdfContext]
    CGContextRestoreGState(pdfContext);


//imageView drawing
    CGContextSaveGState(pdfContext);
    CGContextConcatCTM(pdfContext, CGAffineTransformMakeTranslation(50.0,50.0)); //this  is  just an offset for the imageView drawing. Thee same stuff applies as above..
    [imageView.layer renderInContext:pdfContext]
    CGContextRestoreGState(pdfContext);



//cleanup
    UIGraphicsEndPDFContext();
    return pdfData;

}
这里有几个客户端函数可以使用这个NSData

   //ways to use the pdf Data
-(Bool)savePDFtoPath: (NSString *)path {

    return [ [self drawPDFdata]  writeToFile:path atomically:YES] ;

}

//requires Quartz framework.. (can be  drawn straight to a UIView)
// note you MAY owe a CGPDFDocumentRelease() on the  result  of this function (sorry i've not used objC in a couple of years...)
-(CGPDFDocument *)createPDFdocument {

    NSData *data = [self  drawPDFdata];
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL , data , sizeof(data) ,NULL);

    CGPDFDocument result = CGPDFDocumentCreateWithProvider(provider);

    CGDataProviderRelease(provider); //not sure if this is still required under ARC??  (def not in swift)

    return result;
}

你们能用图书馆做这个吗。所以我会给你我认为最好的方法是制作一个html/css来创建内容,然后把结果放在一个web视图中。加载完web视图后,使用webview.printformatter呈现pdf。您需要使用webview打印格式化程序,因为如果没有它,图像不会被渲染为base64。我已经检查了这个库,但它不允许我使用文本视图中的html文本,也不允许合并签名签名签名是图像?我已经在您的场景中很好地使用了这个功能…您想做些什么。?