Skip to content

Instantly share code, notes, and snippets.

@oilking143
Created October 19, 2023 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oilking143/a1ea3cce9f73905046e20db0c37a847d to your computer and use it in GitHub Desktop.
Save oilking143/a1ea3cce9f73905046e20db0c37a847d to your computer and use it in GitHub Desktop.
private func testPrint() {
let a4Document = Bundle.main.url(forResource: "testPrint", withExtension: "pdf")
let airPrinter = UIPrinter(url: URL(string: printerUrl)!)
// UIPrintInteractionController presents a user interface and manages the printing
let printController = UIPrintInteractionController.shared
let isPrintingAvailable = UIPrintInteractionController.isPrintingAvailable
print("isPrintingAvailable : ", isPrintingAvailable)
// PDF 內容
printController.printingItem = a4Document
print("aaa : ")
// printController.print(to: airPrinter)
// UIPrintInfo contains information about the print job
let printInfo = UIPrintInfo(dictionary: nil)
printInfo.outputType = .general
printInfo.orientation = .portrait
printInfo.duplex = UIPrintInfo.Duplex.none
printInfo.duplex = .none
printInfo.jobName = "myPrintJob"
printController.printInfo = printInfo
print("bbb : ")
if let assignedPrintInfo = printController.printInfo {
if assignedPrintInfo.duplex == UIPrintInfo.Duplex.none {
print("printInfo 成功分配并且双面打印已禁用")
} else {
print("printInfo 分配失败或者双面打印没有被禁用")
}
} else {
print("printInfo 分配失败")
}
airPrinter.contactPrinter { (available) -> Void in
print("available : ", available)
if (available) {
printController.print(to: airPrinter, completionHandler: {a,b,c in
print("列印完成 a: ", a)
print("列印完成 b: ", b)
print("列印完成 c: ", c)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment