Comment convertir UIView en PDF dans iOS?

Il y a beaucoup de ressources sur la façon d'afficher un PDF dans l' UIView une application. Ce que je travaille maintenant est de créer un PDF à partir de UIViews .

Par exemple, j'ai un UIView , avec des sous-vues comme Textviews, UILabels , UIImages , alors comment puis-je convertir un grand UIView dans son set, y compris toutes ses sous-vues et sous-vues en PDF?

J'ai vérifié la reference iOS d'Apple . Cependant, il ne parle que d'écrire des morceaux de text / image dans un file PDF.

Le problème auquel je suis confronté est que le contenu que je veux écrire dans un file PDF est beaucoup. Si je les écris au format PDF, cela va être un énorme travail à faire. C'est pourquoi je cherche un moyen d'écrire des UIViews sur des PDF ou même des bitmaps.

J'ai essayé le code source que j'ai copié d'autres Q / A dans Stack Overflow. Mais cela me donne seulement un PDF vierge avec la taille des limites UIView .

 -(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSSsortingng*)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(); // Resortingeves the document directories from the iOS device NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSSsortingng* documentDirectory = [documentDirectories objectAtIndex:0]; NSSsortingng* documentDirectoryFilename = [documentDirectory ssortingngByAppendingPathComponent:aFilename]; // instructs the mutable data object to write its context to a file on disk [pdfData writeToFile:documentDirectoryFilename atomically:YES]; NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); } 

S'il vous plaît aider. Merci.

    Notez que la méthode suivante crée juste un bitmap de la vue; il ne crée pas de typographie réelle.

     (void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSSsortingng*)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(); // Resortingeves the document directories from the iOS device NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSSsortingng* documentDirectory = [documentDirectories objectAtIndex:0]; NSSsortingng* documentDirectoryFilename = [documentDirectory ssortingngByAppendingPathComponent:aFilename]; // instructs the mutable data object to write its context to a file on disk [pdfData writeToFile:documentDirectoryFilename atomically:YES]; NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); } 

    Assurez-vous également d'importer: QuartzCore / QuartzCore.h

    Si quelqu'un est intéressé, voici le code Swift 2.1:

      func createPdfFromView(aView: UIView, saveToDocumentsWithFileName fileName: Ssortingng) { 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) } } 

    De plus, si quelqu'un est intéressé, voici le code Swift 3:

     func createPdfFromView(aView: UIView, saveToDocumentsWithFileName fileName: Ssortingng) { 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) } } 

    Cela va générer des PDF à partir de UIView et ouvrir la boîte de dialog d'printing, objective C. Joindre - (IBAction)PrintPDF:(id)sender à votre button sur l'écran. Ajouter le #import <QuartzCore/QuartzCore.h>

    Fichier H

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

    Fichier M

     -(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSSsortingng*)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); NSSsortingng* documentDirectory = [documentDirectories objectAtIndex:0]; NSSsortingng* documentDirectoryFilename = [documentDirectory ssortingngByAppendingPathComponent:aFilename]; NSSsortingng *file = [documentDirectory ssortingngByAppendingPathComponent:@"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); NSSsortingng *documentsDirectory = [paths objectAtIndex:0]; NSSsortingng *path = [documentsDirectory ssortingngByAppendingPathComponent:@"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]; } } 

    Je ne sais pas pourquoi, mais la réponse de Casilic me donne un écran vide sur iOS6.1. Le code ci-dessous fonctionne.

     -(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; } -(NSSsortingng*)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSSsortingng*)aFilename { // Creates a mutable data object for updating with binary data, like a byte array NSMutableData *pdfData = [self createPDFDatafromUIView:aView]; // Resortingeves the document directories from the iOS device NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSSsortingng* documentDirectory = [documentDirectories objectAtIndex:0]; NSSsortingng* documentDirectoryFilename = [documentDirectory ssortingngByAppendingPathComponent: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; }