AirPrint:将pdf文件直接打印到打印机

时间:2014-11-04 11:48:34

标签: ios swift airprint

我想将pdf文件(iOS 8.1,AirPrint,Swift)直接打印到打印机。打印很好,但是我在Xcode中收到了这条消息:

  

2014-11-04 12:34:50.069 PrintApp [801:266564] - [PKPaperList   matchedPaper:preferBorderless:withDuplexMode:didMatch:]   paperToMatch = result =   使用MatchType = 0

这是什么意思?

这是我的代码:

let url: NSURL = NSURL(string:"http://www.sirup-shop.de/csv/test-paketlabel.pdf")!

let printInfo = UIPrintInfo(dictionary: nil)
printInfo.jobName = "Label"

let printController = UIPrintInteractionController.sharedPrintController()!
printController.printingItem = url
printController.printInfo = printInfo
printController.delegate = self
printController.printToPrinter(self.printer!, completionHandler: {
    (controller:UIPrintInteractionController!, completed:Bool, error:NSError!) -> Void in
    println("Label finished.")
})

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:2)

这是基于@Alok的答案的Swift版本

let printController = UIPrintInteractionController.shared
    let printInfo = UIPrintInfo(dictionary: [:])
    printInfo.outputType = UIPrintInfoOutputType.general
    printInfo.orientation = UIPrintInfoOrientation.portrait
    printInfo.jobName = "Sample"
    printController.printInfo = printInfo
    printController.showsPageRange = true
    printController.printingItem = NSData(contentsOf: URL(string: "https://www.adobe.com/support/products/enterprise/knowledgecenter/media/c4611_sample_explain.pdf")!)

    printController.present(animated: true) { (controller, completed, error) in
        if(!completed && error != nil){
            NSLog("Print failed - %@", error!.localizedDescription)
        }
        else if(completed) {
            NSLog("Print succeeded")
        }
    }

答案 1 :(得分:1)

我正在分享Objective-C代码,您可以将此作为参考。

UIPrintInteractionController *pc = [UIPrintInteractionController
                                    sharedPrintController];
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.orientation = UIPrintInfoOrientationPortrait;
printInfo.jobName =@"Report";

pc.printInfo = printInfo;
pc.showsPageRange = YES;
pc.printingItem = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://test.com/Print_for_Client_Name.pdf"]];


UIPrintInteractionCompletionHandler completionHandler =
^(UIPrintInteractionController *printController, BOOL completed,
  NSError *error) {
    if(!completed && error){
        NSLog(@"Print failed - domain: %@ error code %ld", error.domain,
              (long)error.code);
    }
};


[pc presentFromRect:CGRectMake(0, 0, 300, 300) inView:self.view animated:YES completionHandler:completionHandler];