ios - Objective-C 完整的 UIView 到 PDF

标签 ios objective-c pdf uiview

我这里有这段代码,可以将我的 UIView 放入 PDF 中。我遇到的问题是我的 View 是一个详细 View Controller 并且是可滚动的并且 PDF 只获取当时详细 View Controller 内部的内容而不是完整 View ,如果我在详细 View 中四处移动,它会捕获任何内容我讲的部分,不是全部。我想做的事情可行吗?

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

}

更新

我一直在努力解决这个问题,我无法获得 PDF 的完整 View ,我也一直在考虑采用我的 NSMutableArray 和我的 NSArray,这是我的 UIView 中使用的数据,并尝试将其转换为 PDF,但将其格式化得很好听起来非常耗时,除非有人知道如何做到这一点。我想我对应该走哪条路感到困惑。

最佳答案

从此

My issue I am having is that my view is a detail view controller and is scrollable and the PDF only gets what inside the detail view controller at that time and not a full view.

看起来问题是您在 View 中的内容是可滚动的,而您创建的 pdf 仅捕获可见区域。

这是你的问题

UIGraphicsBeginPDFContextToData(pdfData, spreadSheet.bounds, nil);

您需要修复它,您需要为其提供可 ScrollView (UITableView、UICollectionView 或 Scrollview)内容大小范围,这就是您的做法。

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

以上代码将生成一页。 对于更多页面,请使用以下代码

//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();

您也可以查看这个工作项目 ScrollViewToPDF . 它使用相同的 ScrollView 层 renderInContext 但这里的 PDF 是根据您的要求创建的,例如一页 PDF 或多页 PDF。 它捕获 ScrollView 的所有可见和不可见部分

关于ios - Objective-C 完整的 UIView 到 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32978514/

相关文章:

python - 在Python中从PDF中提取具有特定标题的数据

c# - 如何从 HttpResponse 编码二进制 PDF?

ios - encodeWithCoder 错误

iOS swift : How to find if an NSURLSession has timed out

ios - WKWebView 与本地文件

objective-c - 如何强制 NSSplitView 的所有 subview 在启动时可见?

ios - 从托管内容重新下载一次应用内购买

ios - 如何获取网站的 HTML 内容

iphone - 如何在iPhone应用程序中设置英语以外的基本语言?

.net - 我有数十万页需要扫描和记录