ios - 在 Swift、iOS 中将表格 View 转换为多页 PDF 的最佳方法之一

标签 ios swift uitableview pdf

使用此函数获取多个页面的 pdf 版本的 tableView,只需将您的 tableView 名称传递给此函数:-

func pdfDataWithTableView(tableView: UITableView) {


        let priorBounds = tableView.bounds
        let fittedSize = tableView.sizeThatFits(CGSizeMake(priorBounds.size.width, tableView.contentSize.height))
        tableView.bounds = CGRectMake(0, 0, fittedSize.width, fittedSize.height)
        let pdfPageBounds = CGRectMake(0, 0, tableView.frame.width, self.view.frame.height)
        let pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds,nil)
        var pageOriginY: CGFloat = 0
        while pageOriginY < fittedSize.height {
            UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil)
            CGContextSaveGState(UIGraphicsGetCurrentContext())
            CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY)
            tableView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
            CGContextRestoreGState(UIGraphicsGetCurrentContext())
            pageOriginY += pdfPageBounds.size.height
        }
        UIGraphicsEndPDFContext()
        tableView.bounds = priorBounds

        var docURL = (NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)).last! as NSURL
        docURL = docURL.URLByAppendingPathComponent( "myDocument.pdf")
        pdfData.writeToURL(docURL, atomically: true)
    }

最佳答案

快速更新

   func pdfDataWithTableView(tableView: UITableView) {    
        let priorBounds = tableView.bounds
        let fittedSize = tableView.sizeThatFits(CGSize(width:priorBounds.size.width, height:tableView.contentSize.height))
        tableView.bounds = CGRect(x:0, y:0, width:fittedSize.width, height:fittedSize.height)
        let pdfPageBounds = CGRect(x:0, y:0, width:tableView.frame.width, height:self.view.frame.height)    
        let pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds,nil)
        var pageOriginY: CGFloat = 0
        while pageOriginY < fittedSize.height {
            UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil)
            UIGraphicsGetCurrentContext()!.saveGState()
            UIGraphicsGetCurrentContext()!.translateBy(x: 0, y: -pageOriginY)
            tableView.layer.render(in: UIGraphicsGetCurrentContext()!)
            UIGraphicsGetCurrentContext()!.restoreGState()
            pageOriginY += pdfPageBounds.size.height
        }
        UIGraphicsEndPDFContext()
        tableView.bounds = priorBounds
        var docURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
        docURL = docURL.appendingPathComponent("myDocument.pdf")
        pdfData.write(to: docURL as URL, atomically: true)
    }

关于ios - 在 Swift、iOS 中将表格 View 转换为多页 PDF 的最佳方法之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39288559/

相关文章:

iOS 应用程序拒绝(订阅)元数据被拒绝

swift - 可哈希协议(protocol)的自动原子唯一 ID

来自不同 Storyboard的 Swift PushViewController

ios - TableView 位于 ViewController 内,但保留继承/子类系统

iphone - 当没有要删除的单元格时,如何关闭 UITableView 中的编辑模式?

iPad autoresizingMask 创建底边距

ios - Urban Airship : Error "Failed to associate named user with status: 403"

ios - Xcode 9 : Swift dependency analysis error

ios - 从单元格内部删除数据

ios - 在 iOS 中以编程方式创建 UINavigationController