我有一个scrollView
有多个子视图,如详细发票。我想将scrollView
转换为pdf,以便我可以轻松地将其发送到打印机并通过电子邮件发送。我们应该怎么做?有没有可能或简单的方法呢?
答案 0 :(得分:1)
您可以使用Uiview
执行此操作。所以首先你应该在scrollview中添加uiview,然后在该视图上添加其他内容。你可以制作类似的方法,
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// 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
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView.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];
documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
}
调用此方法,传递uiview
(您要从中制作pdf),字符串表示文件名。此方法将创建pdf并将数据存储到文档目录。如果你不想存储,那么评论该部分。
您可以将pdfdata
显示为webview
,例如
[webview loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
希望这会有所帮助:)