Michael
Michael

Reputation: 51

AirPrint: Printing a pdf file directly to the printer

I want to print a pdf file (iOS 8.1, AirPrint, Swift) directly to the printer. Printing is fine, but I get this message in Xcode:

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

What does this mean?

This is my code:

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.")
})

Thanks a lot for you help.

Upvotes: 3

Views: 5558

Answers (2)

Vitor Navarro
Vitor Navarro

Reputation: 171

This is the Swift version based on @Alok's answer

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")
        }
    }

Upvotes: 5

Alok
Alok

Reputation: 25968

I am sharing Objective-C Code, You can take this as a reference.

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];

Upvotes: 2

Related Questions