diff --git a/GeliBusinessPlatform.xcworkspace/xcuserdata/junyelu.xcuserdatad/UserInterfaceState.xcuserstate b/GeliBusinessPlatform.xcworkspace/xcuserdata/junyelu.xcuserdatad/UserInterfaceState.xcuserstate index 91489fd8105be475e80c2dc7fde5dce8aaa88c49..d793973b88640dbbf8b82a952ac1adc148cbc2e4 100644 Binary files a/GeliBusinessPlatform.xcworkspace/xcuserdata/junyelu.xcuserdatad/UserInterfaceState.xcuserstate and b/GeliBusinessPlatform.xcworkspace/xcuserdata/junyelu.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git "a/GeliBusinessPlatform/ViewController/1.1\345\205\245\345\272\223&\345\207\272\345\272\223/\345\207\272\345\272\223/NewCKVc.swift" "b/GeliBusinessPlatform/ViewController/1.1\345\205\245\345\272\223&\345\207\272\345\272\223/\345\207\272\345\272\223/NewCKVc.swift" index e538cbbba3fdc163f7a0bb273467d5ff2e7031a6..f8db538d586bb89f93fe4079f5e8ac1fa538e621 100644 --- "a/GeliBusinessPlatform/ViewController/1.1\345\205\245\345\272\223&\345\207\272\345\272\223/\345\207\272\345\272\223/NewCKVc.swift" +++ "b/GeliBusinessPlatform/ViewController/1.1\345\205\245\345\272\223&\345\207\272\345\272\223/\345\207\272\345\272\223/NewCKVc.swift" @@ -8,8 +8,9 @@ import UIKit import LGButton +import WebKit -class NewCKVc: BaseViewController,UITableViewDelegate,UITableViewDataSource,CKNonGmCellDelegate,CKGmCellDelegate, PiCiXuanZeViewControllerDelegate,GLAlertSelectViewDelegate,NewSelectItemVcDelegate,GeliAlertTextViewDelegate, Print_Gprinter_View_Delegate{ +class NewCKVc: BaseViewController,UITableViewDelegate,UITableViewDataSource,CKNonGmCellDelegate,CKGmCellDelegate, PiCiXuanZeViewControllerDelegate,GLAlertSelectViewDelegate,NewSelectItemVcDelegate,GeliAlertTextViewDelegate, Print_Gprinter_View_Delegate,WKNavigationDelegate{ func NewSelectItemVcSelect(datas: Array<InputInitInfoGoodsInfoModel>, pici: Array<Array<GetGoodBatchDataModel>>,select:Array<Int>,str:Array<String>,typeArr:Array<Int>,imgArr:Array<String>) { rkSelectArr.removeAll() datas.forEach { (item) in @@ -198,20 +199,38 @@ class NewCKVc: BaseViewController,UITableViewDelegate,UITableViewDataSource,CKNo } var goodsInfoArr:Array<Any> = [] @IBOutlet weak var gm_view: UIView! + let wkWebView = WKWebView() + // 页é¢åŠ è½½å®Œæˆä¹‹åŽè°ƒç”¨ + func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!){ + + print("页é¢åŠ è½½å®Œæˆ...") + webView.takeScreenshotOfFullContent { (img) in + + + let command = TscCommand() + command.addSize(40, 30) + command.addGap(withM: 2, withN: 0) + command.addReference(0, 0) + command.addTear("ON") + command.addQueryPrinterStatus(Response(rawValue: 1)!) + command.addCls() + + command.addBitmapwithX(0, withY: 0, withMode: 0, withWidth: 400, with: img) + + command.addPrint(1, 1) + ConnecterManager.sharedInstance()?.write(command.getCommand()) + } + } @IBAction func ck_action(_ sender: UIButton) { - let command = TscCommand() - command.addSize(40, 30) - command.addGap(withM: 2, withN: 0) - command.addReference(0, 0) - command.addTear("ON") - command.addQueryPrinterStatus(Response(rawValue: 1)!) - command.addCls() - let image = UIImage(named: "3.png") - command.addBitmapwithX(0, withY: 0, withMode: 0, withWidth: 400, with: image) + let urlStr = "https://c.gelifood.com/market/src/views/Order/codeDetail.html?order_id=12767" + let urlString = urlStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! + let url = URL(string: urlString) + + let request = URLRequest.init(url: url!, cachePolicy: .reloadRevalidatingCacheData, timeoutInterval: 10) + wkWebView.navigationDelegate = self + wkWebView.load(request) - command.addPrint(1, 1) - ConnecterManager.sharedInstance()?.write(command.getCommand()) return if isGm { @@ -643,3 +662,103 @@ class NewCKVc: BaseViewController,UITableViewDelegate,UITableViewDataSource,CKNo glSelectView = nil } } + +/// WebViewPrintPageRenderer: use to print the full content of webview into one image +internal final class WebViewPrintPageRenderer: UIPrintPageRenderer { + + private var formatter: UIPrintFormatter + + private var contentSize: CGSize + + /// 生æˆPrintPageRenderer实例 + /// + /// - Parameters: + /// - formatter: WebViewçš„viewPrintFormatter + /// - contentSize: WebViewçš„ContentSize + required init(formatter: UIPrintFormatter, contentSize: CGSize) { + self.formatter = formatter + self.contentSize = contentSize + super.init() + self.addPrintFormatter(formatter, startingAtPageAt: 0) + } + + override var paperRect: CGRect { + return CGRect.init(origin: .zero, size: contentSize) + } + + override var printableRect: CGRect { + return CGRect.init(origin: .zero, size: contentSize) + } + + private func printContentToPDFPage() -> CGPDFPage? { + let data = NSMutableData() + UIGraphicsBeginPDFContextToData(data, self.paperRect, nil) + self.prepare(forDrawingPages: NSMakeRange(0, 1)) + let bounds = UIGraphicsGetPDFContextBounds() + UIGraphicsBeginPDFPage() + self.drawPage(at: 0, in: bounds) + UIGraphicsEndPDFContext() + + let cfData = data as CFData + guard let provider = CGDataProvider.init(data: cfData) else { + return nil + } + let pdfDocument = CGPDFDocument.init(provider) + let pdfPage = pdfDocument?.page(at: 1) + + return pdfPage + } + + private func covertPDFPageToImage(_ pdfPage: CGPDFPage) -> UIImage? { + let pageRect = pdfPage.getBoxRect(.trimBox) + let contentSize = CGSize.init(width: floor(pageRect.size.width), height: floor(pageRect.size.height)) + + // usually you want UIGraphicsBeginImageContextWithOptions last parameter to be 0.0 as this will us the device's scale + UIGraphicsBeginImageContextWithOptions(contentSize, true, 2.0) + guard let context = UIGraphicsGetCurrentContext() else { + return nil + } + + context.setFillColor(UIColor.white.cgColor) + context.setStrokeColor(UIColor.white.cgColor) + context.fill(pageRect) + + context.saveGState() + context.translateBy(x: 0, y: contentSize.height) + context.scaleBy(x: 1.0, y: -1.0) + + context.interpolationQuality = .low + context.setRenderingIntent(.defaultIntent) + context.drawPDFPage(pdfPage) + context.restoreGState() + + let image = UIGraphicsGetImageFromCurrentImageContext() + UIGraphicsEndImageContext() + + return image + } + + /// print the full content of webview into one image + /// + /// - Important: if the size of content is very large, then the size of image will be also very large + /// - Returns: UIImage? + internal func printContentToImage() -> UIImage? { + guard let pdfPage = self.printContentToPDFPage() else { + return nil + } + + let image = self.covertPDFPageToImage(pdfPage) + return image + } +} + +extension WKWebView { + public func takeScreenshotOfFullContent(_ completion: @escaping ((UIImage?) -> Void)) { + self.scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false) + DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) { + let renderer = WebViewPrintPageRenderer.init(formatter: self.viewPrintFormatter(), contentSize: self.scrollView.contentSize) + let image = renderer.printContentToImage() + completion(image) + } + } +}