Showing
2 changed files
with
63 additions
and
63 deletions
| @@ -8,6 +8,7 @@ | @@ -8,6 +8,7 @@ | ||
| 8 | - Fix wrong format in directPrintPdf [<AlhasanAlQaisi>] | 8 | - Fix wrong format in directPrintPdf [<AlhasanAlQaisi>] |
| 9 | - Add compatibility with Android Gradle Plugin 8.0 [asaarnak] | 9 | - Add compatibility with Android Gradle Plugin 8.0 [asaarnak] |
| 10 | - Add compatibility with Flutter 3.10 | 10 | - Add compatibility with Flutter 3.10 |
| 11 | +- Re-init UIPrinter cause issues with delegate [Hasan] | ||
| 11 | 12 | ||
| 12 | ## 5.10.3 | 13 | ## 5.10.3 |
| 13 | 14 |
| @@ -36,14 +36,14 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -36,14 +36,14 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 36 | private let semaphore = DispatchSemaphore(value: 0) | 36 | private let semaphore = DispatchSemaphore(value: 0) |
| 37 | private var dynamic = false | 37 | private var dynamic = false |
| 38 | private var currentSize: CGSize? | 38 | private var currentSize: CGSize? |
| 39 | - | 39 | + |
| 40 | public init(printing: PrintingPlugin, index: Int) { | 40 | public init(printing: PrintingPlugin, index: Int) { |
| 41 | self.printing = printing | 41 | self.printing = printing |
| 42 | self.index = index | 42 | self.index = index |
| 43 | pdfDocument = nil | 43 | pdfDocument = nil |
| 44 | super.init() | 44 | super.init() |
| 45 | } | 45 | } |
| 46 | - | 46 | + |
| 47 | override public func drawPage(at pageIndex: Int, in _: CGRect) { | 47 | override public func drawPage(at pageIndex: Int, in _: CGRect) { |
| 48 | let ctx = UIGraphicsGetCurrentContext() | 48 | let ctx = UIGraphicsGetCurrentContext() |
| 49 | let page = pdfDocument?.page(at: pageIndex + 1) | 49 | let page = pdfDocument?.page(at: pageIndex + 1) |
| @@ -53,7 +53,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -53,7 +53,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 53 | ctx?.drawPDFPage(page!) | 53 | ctx?.drawPDFPage(page!) |
| 54 | } | 54 | } |
| 55 | } | 55 | } |
| 56 | - | 56 | + |
| 57 | func cancelJob(_ error: String?) { | 57 | func cancelJob(_ error: String?) { |
| 58 | pdfDocument = nil | 58 | pdfDocument = nil |
| 59 | if dynamic { | 59 | if dynamic { |
| @@ -62,23 +62,23 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -62,23 +62,23 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 62 | printing.onCompleted(printJob: self, completed: false, error: error as NSString?) | 62 | printing.onCompleted(printJob: self, completed: false, error: error as NSString?) |
| 63 | } | 63 | } |
| 64 | } | 64 | } |
| 65 | - | 65 | + |
| 66 | func setDocument(_ data: Data?) { | 66 | func setDocument(_ data: Data?) { |
| 67 | let bytesPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: data?.count ?? 0) | 67 | let bytesPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: data?.count ?? 0) |
| 68 | data?.copyBytes(to: bytesPointer, count: data?.count ?? 0) | 68 | data?.copyBytes(to: bytesPointer, count: data?.count ?? 0) |
| 69 | let dataProvider = CGDataProvider(dataInfo: nil, data: bytesPointer, size: data?.count ?? 0, releaseData: dataProviderReleaseDataCallback) | 69 | let dataProvider = CGDataProvider(dataInfo: nil, data: bytesPointer, size: data?.count ?? 0, releaseData: dataProviderReleaseDataCallback) |
| 70 | pdfDocument = CGPDFDocument(dataProvider!) | 70 | pdfDocument = CGPDFDocument(dataProvider!) |
| 71 | - | 71 | + |
| 72 | if dynamic { | 72 | if dynamic { |
| 73 | // Unblock the main thread | 73 | // Unblock the main thread |
| 74 | semaphore.signal() | 74 | semaphore.signal() |
| 75 | return | 75 | return |
| 76 | } | 76 | } |
| 77 | - | 77 | + |
| 78 | DispatchQueue.main.async { [self] in | 78 | DispatchQueue.main.async { [self] in |
| 79 | let controller = UIPrintInteractionController.shared | 79 | let controller = UIPrintInteractionController.shared |
| 80 | controller.delegate = self | 80 | controller.delegate = self |
| 81 | - | 81 | + |
| 82 | let printInfo = UIPrintInfo.printInfo() | 82 | let printInfo = UIPrintInfo.printInfo() |
| 83 | printInfo.jobName = jobName! | 83 | printInfo.jobName = jobName! |
| 84 | printInfo.outputType = .general | 84 | printInfo.outputType = .general |
| @@ -88,27 +88,27 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -88,27 +88,27 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 88 | } | 88 | } |
| 89 | controller.printInfo = printInfo | 89 | controller.printInfo = printInfo |
| 90 | controller.printPageRenderer = self | 90 | controller.printPageRenderer = self |
| 91 | - | 91 | + |
| 92 | if self.printerName != nil { | 92 | if self.printerName != nil { |
| 93 | let printerURL = URL(string: self.printerName!) | 93 | let printerURL = URL(string: self.printerName!) |
| 94 | - | 94 | + |
| 95 | if printerURL == nil { | 95 | if printerURL == nil { |
| 96 | self.printing.onCompleted(printJob: self, completed: false, error: "Unable to find printer URL") | 96 | self.printing.onCompleted(printJob: self, completed: false, error: "Unable to find printer URL") |
| 97 | return | 97 | return |
| 98 | } | 98 | } |
| 99 | - | 99 | + |
| 100 | let printerURLString = printerURL!.absoluteString | 100 | let printerURLString = printerURL!.absoluteString |
| 101 | - | 101 | + |
| 102 | if !selectedPrinters.keys.contains(printerURLString) { | 102 | if !selectedPrinters.keys.contains(printerURLString) { |
| 103 | selectedPrinters[printerURLString] = UIPrinter(url: printerURL!) | 103 | selectedPrinters[printerURLString] = UIPrinter(url: printerURL!) |
| 104 | } | 104 | } |
| 105 | - | 105 | + |
| 106 | selectedPrinters[printerURLString]!.contactPrinter { available in | 106 | selectedPrinters[printerURLString]!.contactPrinter { available in |
| 107 | if !available { | 107 | if !available { |
| 108 | self.printing.onCompleted(printJob: self, completed: false, error: "Printer not available") | 108 | self.printing.onCompleted(printJob: self, completed: false, error: "Printer not available") |
| 109 | return | 109 | return |
| 110 | } | 110 | } |
| 111 | - | 111 | + |
| 112 | controller.print(to: selectedPrinters[printerURLString]!, completionHandler: self.completionHandler) | 112 | controller.print(to: selectedPrinters[printerURLString]!, completionHandler: self.completionHandler) |
| 113 | } | 113 | } |
| 114 | } else { | 114 | } else { |
| @@ -116,7 +116,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -116,7 +116,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 116 | } | 116 | } |
| 117 | } | 117 | } |
| 118 | } | 118 | } |
| 119 | - | 119 | + |
| 120 | override public var numberOfPages: Int { | 120 | override public var numberOfPages: Int { |
| 121 | if dynamic { | 121 | if dynamic { |
| 122 | printing.onLayout( | 122 | printing.onLayout( |
| @@ -128,32 +128,32 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -128,32 +128,32 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 128 | marginRight: paperRect.size.width - (printableRect.origin.x + printableRect.size.width), | 128 | marginRight: paperRect.size.width - (printableRect.origin.x + printableRect.size.width), |
| 129 | marginBottom: paperRect.size.height - (printableRect.origin.y + printableRect.size.height) | 129 | marginBottom: paperRect.size.height - (printableRect.origin.y + printableRect.size.height) |
| 130 | ) | 130 | ) |
| 131 | - | 131 | + |
| 132 | // Block the main thread, waiting for a document | 132 | // Block the main thread, waiting for a document |
| 133 | semaphore.wait() | 133 | semaphore.wait() |
| 134 | } | 134 | } |
| 135 | - | 135 | + |
| 136 | return pdfDocument?.numberOfPages ?? 0 | 136 | return pdfDocument?.numberOfPages ?? 0 |
| 137 | } | 137 | } |
| 138 | - | 138 | + |
| 139 | func completionHandler(printController _: UIPrintInteractionController, completed: Bool, error: Error?) { | 139 | func completionHandler(printController _: UIPrintInteractionController, completed: Bool, error: Error?) { |
| 140 | if !completed, error != nil { | 140 | if !completed, error != nil { |
| 141 | print("Unable to print: \(error?.localizedDescription ?? "unknown error")") | 141 | print("Unable to print: \(error?.localizedDescription ?? "unknown error")") |
| 142 | } | 142 | } |
| 143 | - | 143 | + |
| 144 | printing.onCompleted(printJob: self, completed: completed, error: error?.localizedDescription as NSString?) | 144 | printing.onCompleted(printJob: self, completed: completed, error: error?.localizedDescription as NSString?) |
| 145 | } | 145 | } |
| 146 | - | ||
| 147 | - public func printInteractionController(_ printController: UIPrintInteractionController, choosePaper paperList: [UIPrintPaper]) -> UIPrintPaper { | 146 | + |
| 147 | + public func printInteractionController(_: UIPrintInteractionController, choosePaper paperList: [UIPrintPaper]) -> UIPrintPaper { | ||
| 148 | if currentSize == nil { | 148 | if currentSize == nil { |
| 149 | return paperList[0] | 149 | return paperList[0] |
| 150 | } | 150 | } |
| 151 | - | 151 | + |
| 152 | let bestPaper = UIPrintPaper.bestPaper(forPageSize: currentSize!, withPapersFrom: paperList) | 152 | let bestPaper = UIPrintPaper.bestPaper(forPageSize: currentSize!, withPapersFrom: paperList) |
| 153 | - | 153 | + |
| 154 | return bestPaper | 154 | return bestPaper |
| 155 | } | 155 | } |
| 156 | - | 156 | + |
| 157 | func printPdf(name: String, withPageSize size: CGSize, andMargin margin: CGRect, withPrinter printerID: String?, dynamically dyn: Bool) { | 157 | func printPdf(name: String, withPageSize size: CGSize, andMargin margin: CGRect, withPrinter printerID: String?, dynamically dyn: Bool) { |
| 158 | currentSize = size | 158 | currentSize = size |
| 159 | dynamic = dyn | 159 | dynamic = dyn |
| @@ -162,17 +162,17 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -162,17 +162,17 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 162 | self.printing.onCompleted(printJob: self, completed: false, error: "Printing not available") | 162 | self.printing.onCompleted(printJob: self, completed: false, error: "Printing not available") |
| 163 | return | 163 | return |
| 164 | } | 164 | } |
| 165 | - | 165 | + |
| 166 | if size.width > size.height { | 166 | if size.width > size.height { |
| 167 | orientation = UIPrintInfo.Orientation.landscape | 167 | orientation = UIPrintInfo.Orientation.landscape |
| 168 | } | 168 | } |
| 169 | - | 169 | + |
| 170 | jobName = name | 170 | jobName = name |
| 171 | printerName = printerID | 171 | printerName = printerID |
| 172 | - | 172 | + |
| 173 | let controller = UIPrintInteractionController.shared | 173 | let controller = UIPrintInteractionController.shared |
| 174 | controller.delegate = self | 174 | controller.delegate = self |
| 175 | - | 175 | + |
| 176 | let printInfo = UIPrintInfo.printInfo() | 176 | let printInfo = UIPrintInfo.printInfo() |
| 177 | printInfo.jobName = jobName! | 177 | printInfo.jobName = jobName! |
| 178 | printInfo.outputType = .general | 178 | printInfo.outputType = .general |
| @@ -182,40 +182,39 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -182,40 +182,39 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 182 | } | 182 | } |
| 183 | controller.printInfo = printInfo | 183 | controller.printInfo = printInfo |
| 184 | controller.showsPaperSelectionForLoadedPapers = true | 184 | controller.showsPaperSelectionForLoadedPapers = true |
| 185 | - | 185 | + |
| 186 | controller.printPageRenderer = self | 186 | controller.printPageRenderer = self |
| 187 | - | 187 | + |
| 188 | if printerID != nil { | 188 | if printerID != nil { |
| 189 | let printerURL = URL(string: printerID!) | 189 | let printerURL = URL(string: printerID!) |
| 190 | - | 190 | + |
| 191 | if printerURL == nil { | 191 | if printerURL == nil { |
| 192 | self.printing.onCompleted(printJob: self, completed: false, error: "Unable to find printer URL") | 192 | self.printing.onCompleted(printJob: self, completed: false, error: "Unable to find printer URL") |
| 193 | return | 193 | return |
| 194 | } | 194 | } |
| 195 | - | 195 | + |
| 196 | let printerURLString = printerURL!.absoluteString | 196 | let printerURLString = printerURL!.absoluteString |
| 197 | - | 197 | + |
| 198 | if !selectedPrinters.keys.contains(printerURLString) { | 198 | if !selectedPrinters.keys.contains(printerURLString) { |
| 199 | selectedPrinters[printerURLString] = UIPrinter(url: printerURL!) | 199 | selectedPrinters[printerURLString] = UIPrinter(url: printerURL!) |
| 200 | } | 200 | } |
| 201 | - | ||
| 202 | - | 201 | + |
| 203 | selectedPrinters[printerURLString]!.contactPrinter { available in | 202 | selectedPrinters[printerURLString]!.contactPrinter { available in |
| 204 | if !available { | 203 | if !available { |
| 205 | self.printing.onCompleted(printJob: self, completed: false, error: "Printer not available") | 204 | self.printing.onCompleted(printJob: self, completed: false, error: "Printer not available") |
| 206 | return | 205 | return |
| 207 | } | 206 | } |
| 208 | - | 207 | + |
| 209 | controller.print(to: selectedPrinters[printerURLString]!, completionHandler: self.completionHandler) | 208 | controller.print(to: selectedPrinters[printerURLString]!, completionHandler: self.completionHandler) |
| 210 | } | 209 | } |
| 211 | return | 210 | return |
| 212 | } | 211 | } |
| 213 | - | 212 | + |
| 214 | if dynamic { | 213 | if dynamic { |
| 215 | controller.present(animated: true, completionHandler: completionHandler) | 214 | controller.present(animated: true, completionHandler: completionHandler) |
| 216 | return | 215 | return |
| 217 | } | 216 | } |
| 218 | - | 217 | + |
| 219 | self.printing.onLayout( | 218 | self.printing.onLayout( |
| 220 | printJob: self, | 219 | printJob: self, |
| 221 | width: size.width, | 220 | width: size.width, |
| @@ -226,18 +225,18 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -226,18 +225,18 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 226 | marginBottom: size.height - margin.maxY | 225 | marginBottom: size.height - margin.maxY |
| 227 | ) | 226 | ) |
| 228 | } | 227 | } |
| 229 | - | 228 | + |
| 230 | static func sharePdf(data: Data, withSourceRect rect: CGRect, andName name: String, subject: String?, body: String?) { | 229 | static func sharePdf(data: Data, withSourceRect rect: CGRect, andName name: String, subject: String?, body: String?) { |
| 231 | let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) | 230 | let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) |
| 232 | let fileURL = tmpDirURL.appendingPathComponent(name) | 231 | let fileURL = tmpDirURL.appendingPathComponent(name) |
| 233 | - | 232 | + |
| 234 | do { | 233 | do { |
| 235 | try data.write(to: fileURL, options: .atomic) | 234 | try data.write(to: fileURL, options: .atomic) |
| 236 | } catch { | 235 | } catch { |
| 237 | print("sharePdf error: \(error.localizedDescription)") | 236 | print("sharePdf error: \(error.localizedDescription)") |
| 238 | return | 237 | return |
| 239 | } | 238 | } |
| 240 | - | 239 | + |
| 241 | let activityViewController = UIActivityViewController(activityItems: [fileURL, body as Any], applicationActivities: nil) | 240 | let activityViewController = UIActivityViewController(activityItems: [fileURL, body as Any], applicationActivities: nil) |
| 242 | activityViewController.setValue(subject, forKey: "subject") | 241 | activityViewController.setValue(subject, forKey: "subject") |
| 243 | if UIDevice.current.userInterfaceIdiom == .pad { | 242 | if UIDevice.current.userInterfaceIdiom == .pad { |
| @@ -247,7 +246,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -247,7 +246,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 247 | } | 246 | } |
| 248 | UIApplication.shared.keyWindow?.rootViewController?.present(activityViewController, animated: true) | 247 | UIApplication.shared.keyWindow?.rootViewController?.present(activityViewController, animated: true) |
| 249 | } | 248 | } |
| 250 | - | 249 | + |
| 251 | func convertHtml(_ data: String, withPageSize rect: CGRect, andMargin margin: CGRect, andBaseUrl baseUrl: URL?) { | 250 | func convertHtml(_ data: String, withPageSize rect: CGRect, andMargin margin: CGRect, andBaseUrl baseUrl: URL?) { |
| 252 | let viewController = UIApplication.shared.delegate?.window?!.rootViewController | 251 | let viewController = UIApplication.shared.delegate?.window?!.rootViewController |
| 253 | let wkWebView = WKWebView(frame: viewController!.view.bounds) | 252 | let wkWebView = WKWebView(frame: viewController!.view.bounds) |
| @@ -255,32 +254,32 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -255,32 +254,32 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 255 | wkWebView.tag = 100 | 254 | wkWebView.tag = 100 |
| 256 | viewController?.view.addSubview(wkWebView) | 255 | viewController?.view.addSubview(wkWebView) |
| 257 | wkWebView.loadHTMLString(data, baseURL: baseUrl ?? Bundle.main.bundleURL) | 256 | wkWebView.loadHTMLString(data, baseURL: baseUrl ?? Bundle.main.bundleURL) |
| 258 | - | 257 | + |
| 259 | urlObservation = wkWebView.observe(\.isLoading, changeHandler: { _, _ in | 258 | urlObservation = wkWebView.observe(\.isLoading, changeHandler: { _, _ in |
| 260 | // this is workaround for issue with loading local images | 259 | // this is workaround for issue with loading local images |
| 261 | DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { | 260 | DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { |
| 262 | // assign the print formatter to the print page renderer | 261 | // assign the print formatter to the print page renderer |
| 263 | let renderer = UIPrintPageRenderer() | 262 | let renderer = UIPrintPageRenderer() |
| 264 | renderer.addPrintFormatter(wkWebView.viewPrintFormatter(), startingAtPageAt: 0) | 263 | renderer.addPrintFormatter(wkWebView.viewPrintFormatter(), startingAtPageAt: 0) |
| 265 | - | 264 | + |
| 266 | // assign paperRect and printableRect values | 265 | // assign paperRect and printableRect values |
| 267 | renderer.setValue(rect, forKey: "paperRect") | 266 | renderer.setValue(rect, forKey: "paperRect") |
| 268 | renderer.setValue(margin, forKey: "printableRect") | 267 | renderer.setValue(margin, forKey: "printableRect") |
| 269 | - | 268 | + |
| 270 | // create pdf context and draw each page | 269 | // create pdf context and draw each page |
| 271 | let pdfData = NSMutableData() | 270 | let pdfData = NSMutableData() |
| 272 | UIGraphicsBeginPDFContextToData(pdfData, rect, nil) | 271 | UIGraphicsBeginPDFContextToData(pdfData, rect, nil) |
| 273 | - | 272 | + |
| 274 | for i in 0 ..< renderer.numberOfPages { | 273 | for i in 0 ..< renderer.numberOfPages { |
| 275 | UIGraphicsBeginPDFPage() | 274 | UIGraphicsBeginPDFPage() |
| 276 | renderer.drawPage(at: i, in: UIGraphicsGetPDFContextBounds()) | 275 | renderer.drawPage(at: i, in: UIGraphicsGetPDFContextBounds()) |
| 277 | } | 276 | } |
| 278 | - | 277 | + |
| 279 | UIGraphicsEndPDFContext() | 278 | UIGraphicsEndPDFContext() |
| 280 | - | 279 | + |
| 281 | if let viewWithTag = viewController?.view.viewWithTag(wkWebView.tag) { | 280 | if let viewWithTag = viewController?.view.viewWithTag(wkWebView.tag) { |
| 282 | viewWithTag.removeFromSuperview() // remove hidden webview when pdf is generated | 281 | viewWithTag.removeFromSuperview() // remove hidden webview when pdf is generated |
| 283 | - | 282 | + |
| 284 | // clear WKWebView cache | 283 | // clear WKWebView cache |
| 285 | if #available(iOS 9.0, *) { | 284 | if #available(iOS 9.0, *) { |
| 286 | WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in | 285 | WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in |
| @@ -290,17 +289,17 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -290,17 +289,17 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 290 | } | 289 | } |
| 291 | } | 290 | } |
| 292 | } | 291 | } |
| 293 | - | 292 | + |
| 294 | // dispose urlObservation | 293 | // dispose urlObservation |
| 295 | self.urlObservation = nil | 294 | self.urlObservation = nil |
| 296 | self.printing.onHtmlRendered(printJob: self, pdfData: pdfData as Data) | 295 | self.printing.onHtmlRendered(printJob: self, pdfData: pdfData as Data) |
| 297 | } | 296 | } |
| 298 | }) | 297 | }) |
| 299 | } | 298 | } |
| 300 | - | 299 | + |
| 301 | static func pickPrinter(result: @escaping FlutterResult, withSourceRect rect: CGRect) { | 300 | static func pickPrinter(result: @escaping FlutterResult, withSourceRect rect: CGRect) { |
| 302 | let controller = UIPrinterPickerController(initiallySelectedPrinter: nil) | 301 | let controller = UIPrinterPickerController(initiallySelectedPrinter: nil) |
| 303 | - | 302 | + |
| 304 | let pickPrinterCompletionHandler: UIPrinterPickerController.CompletionHandler = { | 303 | let pickPrinterCompletionHandler: UIPrinterPickerController.CompletionHandler = { |
| 305 | (printerPickerController: UIPrinterPickerController, completed: Bool, error: Error?) in | 304 | (printerPickerController: UIPrinterPickerController, completed: Bool, error: Error?) in |
| 306 | if !completed, error != nil { | 305 | if !completed, error != nil { |
| @@ -308,12 +307,12 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -308,12 +307,12 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 308 | result(nil) | 307 | result(nil) |
| 309 | return | 308 | return |
| 310 | } | 309 | } |
| 311 | - | 310 | + |
| 312 | if printerPickerController.selectedPrinter == nil { | 311 | if printerPickerController.selectedPrinter == nil { |
| 313 | result(nil) | 312 | result(nil) |
| 314 | return | 313 | return |
| 315 | } | 314 | } |
| 316 | - | 315 | + |
| 317 | let printer = printerPickerController.selectedPrinter! | 316 | let printer = printerPickerController.selectedPrinter! |
| 318 | let data: NSDictionary = [ | 317 | let data: NSDictionary = [ |
| 319 | "url": printer.url.absoluteString as Any, | 318 | "url": printer.url.absoluteString as Any, |
| @@ -323,7 +322,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -323,7 +322,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 323 | ] | 322 | ] |
| 324 | result(data) | 323 | result(data) |
| 325 | } | 324 | } |
| 326 | - | 325 | + |
| 327 | if UIDevice.current.userInterfaceIdiom == .pad { | 326 | if UIDevice.current.userInterfaceIdiom == .pad { |
| 328 | let viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController | 327 | let viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController |
| 329 | if viewController != nil { | 328 | if viewController != nil { |
| @@ -331,10 +330,10 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -331,10 +330,10 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 331 | return | 330 | return |
| 332 | } | 331 | } |
| 333 | } | 332 | } |
| 334 | - | 333 | + |
| 335 | controller.present(animated: true, completionHandler: pickPrinterCompletionHandler) | 334 | controller.present(animated: true, completionHandler: pickPrinterCompletionHandler) |
| 336 | } | 335 | } |
| 337 | - | 336 | + |
| 338 | public func rasterPdf(data: Data, pages: [Int]?, scale: CGFloat) { | 337 | public func rasterPdf(data: Data, pages: [Int]?, scale: CGFloat) { |
| 339 | guard | 338 | guard |
| 340 | let provider = CGDataProvider(data: data as CFData), | 339 | let provider = CGDataProvider(data: data as CFData), |
| @@ -343,10 +342,10 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -343,10 +342,10 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 343 | printing.onPageRasterEnd(printJob: self, error: "Cannot raster a malformed PDF file") | 342 | printing.onPageRasterEnd(printJob: self, error: "Cannot raster a malformed PDF file") |
| 344 | return | 343 | return |
| 345 | } | 344 | } |
| 346 | - | 345 | + |
| 347 | DispatchQueue.global().async { | 346 | DispatchQueue.global().async { |
| 348 | let pageCount = document.numberOfPages | 347 | let pageCount = document.numberOfPages |
| 349 | - | 348 | + |
| 350 | for pageNum in pages ?? Array(0 ... pageCount - 1) { | 349 | for pageNum in pages ?? Array(0 ... pageCount - 1) { |
| 351 | guard let page = document.page(at: pageNum + 1) else { continue } | 350 | guard let page = document.page(at: pageNum + 1) else { continue } |
| 352 | let angle = CGFloat(page.rotationAngle) * CGFloat.pi / -180 | 351 | let angle = CGFloat(page.rotationAngle) * CGFloat.pi / -180 |
| @@ -355,7 +354,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -355,7 +354,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 355 | let height = Int(abs((cos(angle) * rect.height + sin(angle) * rect.width) * scale)) | 354 | let height = Int(abs((cos(angle) * rect.height + sin(angle) * rect.width) * scale)) |
| 356 | let stride = width * 4 | 355 | let stride = width * 4 |
| 357 | var data = Data(repeating: 0, count: stride * height) | 356 | var data = Data(repeating: 0, count: stride * height) |
| 358 | - | 357 | + |
| 359 | data.withUnsafeMutableBytes { (outputBytes: UnsafeMutableRawBufferPointer) in | 358 | data.withUnsafeMutableBytes { (outputBytes: UnsafeMutableRawBufferPointer) in |
| 360 | let rgb = CGColorSpaceCreateDeviceRGB() | 359 | let rgb = CGColorSpaceCreateDeviceRGB() |
| 361 | let context = CGContext( | 360 | let context = CGContext( |
| @@ -367,7 +366,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -367,7 +366,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 367 | space: rgb, | 366 | space: rgb, |
| 368 | bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue | 367 | bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue |
| 369 | ) | 368 | ) |
| 370 | - | 369 | + |
| 371 | if context != nil { | 370 | if context != nil { |
| 372 | context!.translateBy(x: CGFloat(width) / 2, y: CGFloat(height) / 2) | 371 | context!.translateBy(x: CGFloat(width) / 2, y: CGFloat(height) / 2) |
| 373 | context!.scaleBy(x: scale, y: scale) | 372 | context!.scaleBy(x: scale, y: scale) |
| @@ -376,18 +375,18 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -376,18 +375,18 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 376 | context!.drawPDFPage(page) | 375 | context!.drawPDFPage(page) |
| 377 | } | 376 | } |
| 378 | } | 377 | } |
| 379 | - | 378 | + |
| 380 | DispatchQueue.main.sync { | 379 | DispatchQueue.main.sync { |
| 381 | self.printing.onPageRasterized(printJob: self, imageData: data, width: width, height: height) | 380 | self.printing.onPageRasterized(printJob: self, imageData: data, width: width, height: height) |
| 382 | } | 381 | } |
| 383 | } | 382 | } |
| 384 | - | 383 | + |
| 385 | DispatchQueue.main.sync { | 384 | DispatchQueue.main.sync { |
| 386 | self.printing.onPageRasterEnd(printJob: self, error: nil) | 385 | self.printing.onPageRasterEnd(printJob: self, error: nil) |
| 387 | } | 386 | } |
| 388 | } | 387 | } |
| 389 | } | 388 | } |
| 390 | - | 389 | + |
| 391 | public static func printingInfo() -> NSDictionary { | 390 | public static func printingInfo() -> NSDictionary { |
| 392 | let data: NSDictionary = [ | 391 | let data: NSDictionary = [ |
| 393 | "directPrint": true, | 392 | "directPrint": true, |
-
Please register or login to post a comment