2

I have an HTML string to be printed using AirPrint in my iOS app. However, the string is customisable and the user is allowed to set margin/paddings, font/font size, orientation etc.

Thus, sometimes the output can be more than 2 pages where the second page is blank. There I want to set a range for pages to be printed. Actually, I just want to print only first page no matter what the output is.

Here's the print interaction controller dialog, you can see there's no range set and the output is more than one page:

UIPrintInteractionController Dialog

And here's my code:

// create an html output text
let markupText = "<div style='width: 100%; text-align: center;'><div style='display: inline-block; margin-top: 35mm; margin-left: 45mm;'>Hello, printer.</div></div>"

// initialize print formatter
let formatter = UIMarkupTextPrintFormatter(markupText: markupText)
formatter.contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
formatter.startPage = 0

// initialize print renderer and set its formatter
let printRenderer = UIPrintPageRenderer()
printRenderer.addPrintFormatter(formatter, startingAtPageAtIndex: 0)
printRenderer.prepareForDrawingPages(NSMakeRange(0, 1))

// initialize print controller to present print dialog
let printController = UIPrintInteractionController.sharedPrintController()
printController.printPageRenderer = printRenderer
printController.presentAnimated(true, completionHandler: nil)

I was unable to find enough information about setting the range, though I thought prepareForDrawingPages method would do the trick. Any ideas about accomplishing this?

kubilay
  • 5,047
  • 7
  • 48
  • 66

1 Answers1

2

If you want to use a custom subclass of UIPrintPageRenderer, there is also a numberOfPages method you can override to calculate the number of pages. However, I believe you may just be forgetting to set the UIPrintInteractionController property showsPageRange to YES.

I solved this by writing my html into a PDF (NSData), and setting the printingItem property to that item. My code is in objective-c, but the same principle applies.

printingItem documentation

Generating PDF content

cbay
  • 72
  • 7
  • numberOfPages property is read-only: "Cannot assign to value: function call returns immutable value". Did set showsPageRange to true, but nothing has changed, same output. – kubilay Jan 20 '16 at 06:24
  • PDF thing was a good idea. UIPrintPageRenderer's drawPageAtIndex method was successful to create a single page PDF file so everthing went well after having that 1-paged PDF. Created the file with a little help of https://gist.github.com/nyg/b8cd742250826cb1471f – kubilay Jan 20 '16 at 06:52
  • @kubilay `numberOfPages` is also a method that calculates the number of pages, but you have to make a custom subclass of `UIPrintPageRenderer` to override it. I was using a custom subclass until I figured out the PDF thing was so much easier. I'll edit to acknowledge it only works in custom subclasses. – cbay Jan 20 '16 at 22:32