如何将iOS中的UIView转换为PDF?

关于如何在应用程序的UIView显示PDF有很多资源。 我现在正在处理的是从UIViews创build一个PDF。

例如,我有一个UIView ,像Textviews, UILabelsUIImages这样的子视图,那么我怎样才能把一个大的 UIView作为一个整体,包括它的所有子视图和子视图转换成一个PDF?

我检查了苹果的iOS参考 。 但是,它只是谈论将文本/图像文件写入PDF文件。

我面临的问题是,我想以PDF格式写入文件的内容非常多。 如果我把它们逐个写入PDF文件,这将是一项巨大的工作。 这就是为什么我正在寻找一种方法来编写UIViews到PDF甚至位图。

我已经尝试了从Stack Overflow中的其他Q / A复制的源代码。 但它只给了我一个空白的PDF与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(); // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData [aView drawRect:aView.bounds]; // 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:aFilename]; // instructs the mutable data object to write its context to a file on disk [pdfData writeToFile:documentDirectoryFilename atomically:YES]; NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); } 

请帮忙。 谢谢。

请注意,以下方法创build视图的位图; 它不会创build实际的排版。

 (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]; NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; // instructs the mutable data object to write its context to a file on disk [pdfData writeToFile:documentDirectoryFilename atomically:YES]; NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); } 

还要确保你导入:QuartzCore / QuartzCore.h

如果有人感兴趣,这里是Swift 2.1代码:

  func createPdfFromView(aView: UIView, saveToDocumentsWithFileName fileName: String) { let pdfData = NSMutableData() UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil) UIGraphicsBeginPDFPage() guard let pdfContext = UIGraphicsGetCurrentContext() else { return } aView.layer.renderInContext(pdfContext) UIGraphicsEndPDFContext() if let documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first { let documentsFileName = documentDirectories + "/" + fileName debugPrint(documentsFileName) pdfData.writeToFile(documentsFileName, atomically: true) } } 

此外,如果有人感兴趣,这里是Swift 3代码:

 func createPdfFromView(aView: UIView, saveToDocumentsWithFileName fileName: String) { let pdfData = NSMutableData() UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil) UIGraphicsBeginPDFPage() guard let pdfContext = UIGraphicsGetCurrentContext() else { return } aView.layer.render(in: pdfContext) UIGraphicsEndPDFContext() if let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first { let documentsFileName = documentDirectories + "/" + fileName debugPrint(documentsFileName) pdfData.write(toFile: documentsFileName, atomically: true) } } 

这将从UIView生成PDF并打开打印对话框,目标C.附加- (IBAction)PrintPDF:(id)sender到屏幕上的button。 添加#import <QuartzCore/QuartzCore.h>框架

H文件

  @interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate,UIPrintInteractionControllerDelegate> { UIPrintInteractionController *printController; } - (IBAction)PrintPDF:(id)sender; 

M文件

 -(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename { NSMutableData *pdfData = [NSMutableData data]; UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil); UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); [aView.layer renderInContext:pdfContext]; UIGraphicsEndPDFContext(); NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString* documentDirectory = [documentDirectories objectAtIndex:0]; NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; NSString *file = [documentDirectory stringByAppendingPathComponent:@"yourPDF.pdf"]; NSURL *urlPdf = [NSURL fileURLWithPath: file]; [pdfData writeToFile:documentDirectoryFilename atomically:YES]; NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); } - (IBAction)PrintPDF:(id)sender { [self createPDFfromUIView:self.view saveToDocumentsWithFileName:@"yourPDF.pdf"]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourPDF.pdf"]; NSData *myData = [NSData dataWithContentsOfFile: path]; UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController]; if(pic && [UIPrintInteractionController canPrintData: myData] ) { pic.delegate = self; UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.jobName = [path lastPathComponent]; printInfo.duplex = UIPrintInfoDuplexLongEdge; pic.printInfo = printInfo; pic.showsPageRange = YES; pic.printingItem = myData; void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) { //self.content = nil; if(!completed && error){ NSLog(@"Print Error: %@", error); } }; [pic presentAnimated:YES completionHandler:completionHandler]; } } 

我不知道为什么,但casilic的答案给我在iOS6.1的空白屏幕。 下面的代码工作。

 -(NSMutableData *)createPDFDatafromUIView:(UIView*)aView { // 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(); return pdfData; } -(NSString*)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename { // Creates a mutable data object for updating with binary data, like a byte array NSMutableData *pdfData = [self createPDFDatafromUIView:aView]; // Retrieves the document directories from the iOS device NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString* documentDirectory = [documentDirectories objectAtIndex:0]; NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; // instructs the mutable data object to write its context to a file on disk [pdfData writeToFile:documentDirectoryFilename atomically:YES]; NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); return documentDirectoryFilename; }