Objective-C Full UIView to PDF

I have this code here which forces my UIView to put it in a PDF. My problem is that my view is a detail view controller and it is scrollable, and in PDF it just turns out that inside the detail view controller at that time, and not the full view, if I move in the detailed view, it will capture the whole part I am not all. Am I trying to do this?

- (IBAction)Share:(id)sender { NSMutableData *pdfData = [NSMutableData data]; // Points the pdf converter to the mutable data object and to the UIView to be converted UIGraphicsBeginPDFContextToData(pdfData, spreadSheet.bounds, nil); UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData [spreadSheet.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:@"test.pdf"]; // instructs the mutable data object to write its context to a file on disk [pdfData writeToFile:documentDirectoryFilename atomically:YES]; NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); UIActivityViewController * activityController = [[UIActivityViewController alloc] initWithActivityItems:@[pdfData] applicationActivities:nil]; UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityController]; [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width - 36, 60, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; } 

UPDATE

I am really struggling with this, I can’t fully view the PDF file, I also thought about going to my NSMutableArray and my NSArray, which is the data used in my UIView, and trying hidden in the PDF, but it sounds like a lot of time to make it beautiful if someone does not know how to do it. I guess I'm confused about which route I should take.

+6
source share
3 answers

From this

My problem that I am facing is that my view is a detailed view controller and it scrolls, and the PDF file only gets what is inside the detailed view of the controller at that time, and not the full view.

It seems that the problem is that your content in the viewer scrolls, and you create a pdf file only to capture the visible area.

Here is your problem

UIGraphicsBeginPDFContextToData (pdfData, spreadSheet.bounds, nil);

You need to fix this, you must provide it with a scrollable view (UITableView, UICollectionView or Scrollview) that limits the size of the content. This is how you do it.

  -(NSData*)pdfDataFromTableViewContent { NSMutableData *pdfData = [NSMutableData data]; //The real size, not only the visible part use your scrollable view ((UITableView,UICollectionView or Scrollview)) CGSize contentSize = self.tableView.contentSize; CGRect tableViewRealFrame = self.tableView.frame; //Size tableView to show the whole content self.tableView.frame = CGRectMake(0, 0, contentSize.width, contentSize.height); //Generate PDF (one page) UIGraphicsBeginPDFContextToData(pdfData,self.tableView.frame, nil); UIGraphicsBeginPDFPage(); [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIGraphicsEndPDFContext(); //Resize frame self.tableView.frame = tableViewRealFrame; return pdfData; } 

The code above generates one page. For more pages, use the following code

 //MultiPage CGRect mediaBox = self.tableView.frame; CGSize pageSize = CGSizeMake(self.view.frame.size.width, 800); UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); NSInteger currentPage = 0; BOOL done = NO; do { UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0.0, pageSize.width, pageSize.height), nil); CGContextTranslateCTM(pdfContext, 0, -(pageSize.height*currentPage)); [self.view.layer renderInContext:pdfContext]; if ((pageSize.height*(currentPage+1)) > mediaBox.size.height) done = YES; else currentPage++; } while (!done); UIGraphicsEndPDFContext(); 

You can also explore this ScrollViewToPDF working draft. It uses the same scrollview renderInContext layer, but here the PDF is created according to your requirements, such as a single-page PDF or multiple PDF pages. It captures all visible as well as invisible part of scrollView

+2
source

Rendering something in context only displays what is currently in the hierarchy of views. This means that if you use a UITableView or UICollectionView , not every cell representing your data is in the hierarchy at any given time. If it were me, I would try to temporarily set the view to a massive frame so that everything is in the hierarchy. If this did not work, I would write a custom view that, on request, could compose everything for all the data, and then reset to a more efficient layout after rendering is complete.

+1
source

Try the following code to convert the UIView to PDF.

Note that the following method only creates a raster image of the view; he does not create the actual printing house.

 -(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

0
source

All Articles