I have a printing routine in Objective-C that prints a local PDF file.
I would like to get the same routine working in Swift. Can anyone help?
- (void)printFile:(NSURL *)url {
if ([UIPrintInteractionController .canPrintURL(url]) {
UIPrintInteractionController *
controller = [UIPrintInteractionController
sharedPrintController()];
controller.printingItem = url;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
PrintInfo.outputType = UIPrintInfoOutputGeneral;
PrintInfo.jobName = [url lastPathComponent];
controller.printInfo = printInfo;
controller.showPageRange = YES;
[controller presentAnimated:YES completionHandler:Null];
}
}
Assign paperRect and printableRect let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi render. setValue(page, forKey: "paperRect") render. setValue(page, forKey: "printableRect") // 4. Create PDF context and draw let pdfData = NSMutableData() UIGraphicsBeginPDFContextToData(pdfData, .
This should point you in the right direction, follow the link to get a good idea of what this does.
@IBAction func print(sender: UIBarButtonItem) {
if UIPrintInteractionController.canPrintURL(imageURL) {
let printInfo = UIPrintInfo(dictionary: nil)
printInfo.jobName = imageURL.lastPathComponent
printInfo.outputType = .Photo
let printController = UIPrintInteractionController.sharedPrintController()!
printController.printInfo = printInfo
printController.showsNumberOfCopies = false
printController.printingItem = imageURL
printController.presentAnimated(true, completionHandler: nil)
}
}
taken from http://nshipster.com/uiprintinteractioncontroller/
Update for swift 4.2
@IBAction func printAction(_ sender: UIButton) {
if let guide_url = Bundle.main.url(forResource: "RandomPDF", withExtension: "pdf"){
if UIPrintInteractionController.canPrint(guide_url) {
let printInfo = UIPrintInfo(dictionary: nil)
printInfo.jobName = guide_url.lastPathComponent
printInfo.outputType = .photo
let printController = UIPrintInteractionController.shared
printController.printInfo = printInfo
printController.showsNumberOfCopies = false
printController.printingItem = guide_url
printController.present(animated: true, completionHandler: nil)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With