Peter_Lustig
Peter_Lustig

Reputation: 72

Printing pdf file in webview

I want to add a print Button into my app to print local pdf files.

So I added a UIToolBar + UIBarButtonItem and linked it with my following code:

@IBAction func printButton(_ sender: Any) {

    let printController = UIPrintInteractionController.shared

    let printInfo = UIPrintInfo(dictionary:nil)

    printInfo.outputType = UIPrintInfo.OutputType.general

    printInfo.jobName = (wkWebView.url?.absoluteString)!

    printInfo.duplex = UIPrintInfo.Duplex.none

    printInfo.orientation = UIPrintInfo.Orientation.portrait

    printController.printPageRenderer = nil

    printController.printingItems = nil

    printController.printingItem = wkWebView.url!

    printController.printInfo = printInfo

   // printController.showsPageRange = true

    printController.showsNumberOfCopies = true


    printController.presentFromBarButtonItem(printButton, animated: true, completionHandler: nil)

}

On the last line I get this error message:

Cannot convert value of type '(Any) -> ()' to expected argument type 'UIBarButtonItem'

I tried different ways but I'm stuck. It would be awesome if someone can help me. I think it's easy to solve but not at the moment for me.

Upvotes: 1

Views: 765

Answers (2)

Jeesson_7
Jeesson_7

Reputation: 811

You are required to pass the argument as bar button. But you are passing sender, which is "Any" at the moment.. either use a guard to downcast it as bar button like

 guard let barButton = sender as? UIBarButtonItem else {
            print("not bar button")
            return
        }
 printController.presentFromBarButtonItem(barButton, animated: true, completionHandler: nil)

Or change the sender to

IBAction func printButton(_ sender: UIBarButtonItem)

 printController.presentFromBarButtonItem(sender, animated: true, completionHandler: nil)

Upvotes: 2

s3cretshadow
s3cretshadow

Reputation: 237

Remove action than try to addAction with sender

IBAction func printButton(_ sender: Any) //instead  of
IBAction func printButton(_ sender: UIBarButtonItem)

Upvotes: 0

Related Questions