ios - 创建 tableview 所有数据的 pdf

标签 ios swift uitableview pdf

我已经创建了 tableview 所有单元格的 pdf。但我已经创建了 pdf,它不显示 tableview 的所有数据,而且当 pdf 页面高度根据单元格数据设置时。我不知道如何解决这个问题。我正在使用此代码创建 pdf。请帮助我。

  let _: NSData!
        do {
            let priorBounds = table_view.bounds
            let fittedSize = table_view.sizeThatFits(CGSizeMake(priorBounds.size.width, .infinity))
            table_view.bounds = CGRectMake(0, 0, fittedSize.width, fittedSize.height)
            let pdfPageBounds = CGRectMake(0, 0, 612, 800)
            let pdfData = NSMutableData()
            UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil)
            do {
                var pageOriginY = 0
                while pageOriginY < Int(fittedSize.height)  {
                    UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil)
                    CGContextSaveGState(UIGraphicsGetCurrentContext()!)
                    do {
                        CGContextTranslateCTM(UIGraphicsGetCurrentContext()!, 0, -CGFloat(pageOriginY))
                        table_view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
                    }
                    CGContextRestoreGState(UIGraphicsGetCurrentContext()!)
                    pageOriginY += Int(pdfPageBounds.size.height)
                }
            }
            UIGraphicsEndPDFContext()
            table_view.bounds = priorBounds
            if let documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first {
                let documentsFileName = documentDirectories + "/" + (filename as String)
                debugPrint(documentsFileName)
                pdfData.writeToFile(documentsFileName, atomically: true)
            }
        }

最佳答案

试试这个。

func createPdfFromTableView()
{
    let priorBounds: CGRect = self.tblView.bounds
    let fittedSize: CGSize = self.tblView.sizeThatFits(CGSize(width: priorBounds.size.width, height: self.tblView.contentSize.height))
    self.tblView.bounds = CGRect(x: 0, y: 0, width: fittedSize.width, height: fittedSize.height)
    self.tblView.reloadData()
    let pdfPageBounds: CGRect = CGRect(x: 0, y: 0, width: fittedSize.width, height: (fittedSize.height))
    let pdfData: NSMutableData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil)
    UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil)
    self.tblView.layer.render(in: UIGraphicsGetCurrentContext()!)
    UIGraphicsEndPDFContext()
    let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
    let documentsFileName = documentDirectories! + "/" + "pdfName"
    pdfData.write(toFile: documentsFileName, atomically: true)
    print(documentsFileName)
}

关于ios - 创建 tableview 所有数据的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40647467/

相关文章:

ios - 在 iOS 6 中呈现 Modal ViewController

iphone - 如何在 iPhone 中包含可变长度图像作为 UITableViewCell 背景?

ios - 自动调整大小 tableview 单元格扩展高度中的嵌套水平 collectionview 在为行重新加载单元格时使 Collection View 滚动重置

ios - 使用两个 NSSortDescriptor 来过滤数组

objective-c - NSString 分配中的摘要无效

ios - 以编程方式向 Swift 中的 View 添加约束时出错

iOS Swift - 如何在应用程序中检测系统警报

ios - 如何在其他 View Controller 上复制导航 Controller 图像

带分页的 iOS TableView?

ios - 如何将 iOS 10 通用应用程序中的单个 UIViewController 限制为纵向布局?