微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift 5 TableView 转 PDF

如何解决Swift 5 TableView 转 PDF

我有一个具有动态行高和多行的 tableView。我正在使用下面的代码将其转换为 PDF。


// Export pdf from UITableView and save pdf in drectory and return pdf file path
func exportAsPdfFromTable() -> String {

    self.showsverticalScrollIndicator = true
    let originalBounds = self.bounds
    self.bounds = CGRect(x:originalBounds.origin.x,y: originalBounds.origin.y,width: self.contentSize.width,height: self.contentSize.height)
    let pdfpageFrame = CGRect(x: 0,y: 0,width: self.bounds.size.width,height: self.contentSize.height)

    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData,pdfpageFrame,nil)
    UIGraphicsBeginpdfpageWithInfo(pdfpageFrame,nil)
    guard let pdfContext = UIGraphicsGetCurrentContext() else { return "" }
    self.layer.render(in: pdfContext)
    UIGraphicsEndPDFContext()
    self.bounds = .null
    // Save pdf data
    return self.saveTablePdf(data: pdfData)

}

// Save pdf file in document directory
func saveTablePdf(data: NSMutableData) -> String {

    let paths = FileManager.default.urls(for: .documentDirectory,in: .userDomainMask)
    let docDirectoryPath = paths[0]
    let pdfPath = docDirectoryPath.appendingPathComponent("myPDF.pdf")
    if data.write(to: pdfPath,atomically: true) {
        return pdfPath.path
    } else {
        return ""
    }
}
    
}

那是我的 tableView

enter image description here

然而,导出的 PDF 仅显示 tableView 的可见部分:

enter image description here

如何在 PDF 中显示完整的 tableView?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。