问题描述
我有一个(待办事项样式)列表应用程序。数据布置在具有自定义单元格的 diffable dataSource tableview 中。我有一个可用的下拉菜单,我正在尝试添加一个 print
按钮,以便用户可以根据需要打印出列表(在许多列表应用中似乎是标准配置)。
到目前为止,我在按钮的操作中有以下内容:
let printController = UIPrintInteractionController.shared
printController.delegate = self
let printInfo = uiprintinfo(dictionary: nil)
printInfo.outputType = .general
printInfo.jobName = "Print TableView"
printController.printInfo = printInfo
let tableViewPDF = self?.tableView.exportAsPdfFromTable()
printController.printingItem = tableViewPDF
let formatter = UIPrintFormatter()
formatter.perPageContentInsets = UIEdgeInsets(top: 50,left: 50,bottom: 50,right: 50)
printController.printFormatter = formatter
printController.present(animated: true,completionHandler: nil)
和委托人:
extension ListTableViewController: UIPrintInteractionControllerDelegate {
func printInteractionControllerParentViewController(_ printInteractionController: UIPrintInteractionController) -> UIViewController? {
return self
}
}
如您所见,我正在尝试将 tableview 转换为 PDF 并打印出来(也许其他东西可能更好?)。 tableView 上的 .exportAsPdfFromTable()
方法是一个 UITableView
扩展,如下所示:
extension UITableView {
// Export pdf from UITableView and save pdf in drectory and return pdf file path
func exportAsPdfFromTable() -> String {
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 = originalBounds
// 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("tablePdf.pdf")
if data.write(to: pdfPath,atomically: true) {
return pdfPath.path
} else {
return ""
}
}
}
结果是打印控制器显示空白页。对我缺少的东西有什么想法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)