I have an UIView with subviews like below mentioned image.
Here I have to print or share marked - view.
For Sharing, I convert the marked UIView to PDF and share it in Email. It is working Perfectly what my client expected.
For Printing, I convert the marked UIView to UIImage and print it. It is also working, But what I except is have to print that converted UIImage with scale to page. But the receipt printing is too small like below mentioned image (Epson TM-T88V printer).
My code to convert UIView to UIImage:
extension UIView {
func toImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
drawHierarchy(in: self.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
My code for printing the image:
@IBAction func act_PrintBtnPressed(_ sender: UIButton) {
m_PrintView.frame.size.height = view.frame.size.height
let printInfo = UIPrintInfo(dictionary:nil)
printInfo.outputType = UIPrintInfoOutputType.photo
printInfo.jobName = "PrintingReceipt"
printInfo.orientation = .portrait
let printController = UIPrintInteractionController.shared
printController.printInfo = printInfo
printController.printingItem = m_PrintView.toImage()
printController.showsNumberOfCopies = false
printController.present(animated: true, completionHandler: nil)
}
And lastly how to set number of copies have to print in code itself. Because I don't want to set number of copies in UI, so I set printController.showsNumberOfCopies
as false.
Please guide me to create this.
Finally I found it by myself. I created the uicontrols value by htmlString. And used UIPrintPageRenderer like below mentioned.
Here am creating the controls value to html string like below mentioned:
Now When user taps the print button:
It meets what I excepted.