gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:31:40 25 4
gpt4 key购买 nike

请仔细阅读我的场景,

我有一个 UITextView 和一个 UIImageView 的 TextView 底部。每次 TextView 中都会有动态内容,相应地,我要求用户签名,它将在底部 ImageView 中显示为图像。

现在的要求是我必须在一个 PDF 文件中将这些详细信息连同签名一起传递到服务器,因此我必须创建包含 TextView 文本和 ImageView 图像的 PDF 文件。

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

根据需要检查下面的图片和当前的 pdf。

这是必需的 PDF This is required PDF

这是当前的 PDF This is current PDF

仅将有助于 HTML 支持和图像与文本合并的代码放入。请不要显示简单的 PDF 创建,因为我已经完成了。

最佳答案

您不需要第 3 方库,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;
}

关于ios - 在 Objective - C 中创建具有适当格式和图像的 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43603852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com