ios - 如何将 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()
}
解决方案
推荐阅读
- c++ - 多次打印出整个数组
- javascript - 使用 Grid fs 在 mongo 数据库中存储数千个图像文件,这是个好主意吗?
- python - 计算字符串中的子字符串,不包括一个字符串
- apache - 如何在 apache 上模拟 500 http 错误
- python - 不知道为什么我没有收到通知
- sql - Oracle SQL比较列值并选择最大值在第1列中的行
- c++ - 具有静态 std::array 的神经网络比使用动态 C 数组的神经网络慢
- excel - 根据工作表名称获取 H 列的总和
- ios - 关于 Swift 中自定义切换按钮的问题
- c# - 如何将字符串添加到文本文件的前面