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