Swift 3 打印功能

前端之家收集整理的这篇文章主要介绍了Swift 3 打印功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 加载到webview再打印

2. 直接打印nsdata

func printViaWebView(fileName: String)
{
 let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0]
        let filePath = "\(documentsPath)/\(filename)"
        let url = NSURL(fileURLWithPath: filePath)
        let urlRequest = NSURLRequest(url: url as URL)
        
        webView.loadRequest(urlRequest as URLRequest)
        
        
        let pic = UIPrintInteractionController.shared
        let printInfo : UIPrintInfo = UIPrintInfo(dictionary: nil)
        
        printInfo.outputType = UIPrintInfoOutputType.general
        printInfo.jobName = url.absoluteString!
        
        pic.printInfo = printInfo
        pic.printFormatter = webView.viewPrintFormatter()
        pic.showsPageRange = false
        
        pic.present(animated: true,completionHandler: nil)
}

func printViaNSData(fileName: String){
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,true)[0]
        let filePath = "\(documentsPath)/\(filename)"
        let url = NSURL(fileURLWithPath: filePath)
        let urlRequest = NSURLRequest(url: url as URL)
        
        webView.loadRequest(urlRequest as URLRequest)
        
        
        let pic = UIPrintInteractionController.shared
        let printInfo : UIPrintInfo = UIPrintInfo(dictionary: nil)
        
        printInfo.outputType = UIPrintInfoOutputType.general
        //printInfo.jobName = url.absoluteString!
        //printInfo.orientation = .portrait
        
        pic.printingItem = NSData(contentsOfFile: filePath)
        pic.showsPaperSelectionForLoadedPapers = true;
        pic.showsPageRange = true;
        pic.showsNumberOfCopies = true;
        pic.printInfo = printInfo
        //pic.printFormatter = webView.viewPrintFormatter()
        
        pic.present(animated: true,completionHandler: nil)
}

猜你在找的Swift相关文章