How to Convert UIView to PDF within iOS?
Note that the following method creates just a bitmap of the view; it does not create actual typography.
(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);
}
Also make sure you import:
QuartzCore/QuartzCore.h
Unable create pdf with UIView by swift?
In Obj code you are rendering view.layer
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
while in swift code you forgot to do so
replace view.layer.draw(in: UIGraphicsGetCurrentContext()!)
with rendering
Hope it is helpful
Need uiview to pdf component in ios
(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);
}
Also make sure you import: QuartzCore/QuartzCore.h
Related Topics
Why Is the Tab Bar Disappearing
Split a String into an Array in Swift
How to Set Up a Simple Delegate to Communicate Between Two View Controllers
How to Add Spacing Between Uitableviewcell
Update Badge With Push Notification While App in Background
Force View Controller to Reload to Refresh Uiappearance Changes
React-Native, How to Get File-Asset Image Absolute Path
Ios: How to Make a Shadow for Uiview on 4 Side (Top,Right,Bottom and Left)
Download Array of Images as Byte Array and Convert to Images
Firebase Custom Event Parameters Not Visible in Console
How to Display 3 Cells Per Row in Uicollectionview
Nsdictionary to Nsdata and Nsdata to Nsdictionary in Swift
How to Prevent Tableview Section Head from Sticking While Scrolling
How to Download Multiple Files Sequentially Using Nsurlsession Downloadtask in Swift
How to Check If a String Contains Another String in Objective-C
How to Insert New Cell into Uitableview in Swift
Design For Facebook Authentication in an iOS App That Also Accesses a Secured Web Service