首页 > 解决方案 > 如何将 tableView 数据导出为 PDF 并发送到电子邮件?

问题描述

如何将 tableView 数据生成为 PDF 并通过电子邮件发送?我已经尝试过使用下面的代码,但我没有得到任何东西。我想以 PDF 格式导出 tableView 数据和电子邮件。谁能帮我 ?

   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)
    sendMail()
}

标签: iosswift4xcode10.1

解决方案


推荐阅读