Printing Continuous Content in iOS

I’ve recently been working on an application that needs to generate printed receipts, and I’ve been using AirPrint to handle the output. Overall, I’ve found that AirPrint works really well, and I’ve had little trouble incorporating it into my app.

However, one challenge I’ve run into is producing continuous content. AirPrint seems to be geared more towards paginated content, and it doesn’t appear to work particularly well with the roll-based print media typically found in receipt printers.

After struggling with this for the better part of a day, I finally came up with this solution:

class ContinuousPageRenderer : UIPrintPageRenderer, UIPrintInteractionControllerDelegate {
    let attributedText: NSAttributedString

    let margin: CGFloat = 72.0 * 0.125

    init(attributedText: NSAttributedString) {
        self.attributedText = attributedText

        super.init()

        let printFormatter = UISimpleTextPrintFormatter(attributedText: attributedText)

        printFormatter.perPageContentInsets = UIEdgeInsets(top: margin, left: margin, bottom: margin, right: margin)

        addPrintFormatter(printFormatter, startingAtPageAt: 0)
    }

    func printInteractionController(_ printInteractionController: UIPrintInteractionController, cutLengthFor paper: UIPrintPaper) -> CGFloat {
        let size = CGSize(width: paper.printableRect.width - margin * 2, height: 0)

        let boundingRect = attributedText.boundingRect(with: size, options: [
            .usesLineFragmentOrigin,
            .usesFontLeading
        ], context: nil)

        return boundingRect.height + margin * 2
    }
}

This class provides a renderer for producing continuous output based on the content of an attributed string. Internally, it uses an instance of UISimpleTextPrintFormatter to format the output. A 1/8″ border, represented by the margin constant, is established around the generated content.

The class conforms to the UIPrintInteractionControllerDelegate protocol and provides an implementation for the printInteractionController(_:cutLengthFor:) method, which, despite its somewhat misleading name, actually appears to control the length of the generated page. In this case, the cut length is determined by calculating the bounding rectangle of the attributed text using the current printable area minus the page margins.

In order to correctly calculate the page size, a instance of this class must be set both as the print page renderer and the delegate of the print interaction controller; for example:

// Generate attributed text
let attributedText = NSMutableAttributedString()

let text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n"
let attributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 10)]

attributedText.append(NSAttributedString(string: text, attributes: attributes))
attributedText.append(NSAttributedString(string: text, attributes: attributes))
attributedText.append(NSAttributedString(string: text, attributes: attributes))
attributedText.append(NSAttributedString(string: text, attributes: attributes))

// Print attributed text
let continuousPageRenderer = ContinuousPageRenderer(attributedText: attributedText)

printInteractionController.printPageRenderer = continuousPageRenderer
printInteractionController.delegate = continuousPageRenderer

Without the printInteractionController(_:cutLengthFor:) method, AirPrint attempts to break the content up into pages that, on my system, appear to have the same aspect ratio as “US Letter” (8 1/2 x 11) stock:

However, with the delegate method, the page size is correctly determined based on the length of the content:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s