Forum Archive

Share string/text to "Real printer" from app

tlinnet

Hi!

Wuhuuu!
In 4 evenings I have for the first time developed my own UI for iPhone through pythonista.

I know python, but I don't know Xcode/swift. And my 3 previous attempts has failed miserably.

But now. :) Wow it works! So, first of all. THANK YOU!

To the question:

I have developed an app, which contacts a REST api at a webserver.
It uses pythons request module, to ask for a list of "free vouchers" or to "generate a new voucher".

And it works beautifully. :)

A ui.TableView() contains the list of free vouchers, and a ui.TextView() contains the newest generated voucher code.

Now, I was wondering to buy a small thermal bluetooth printer. Like this:
https://www.amazon.com/Portable-Bluetooth-Printer-Wosports-Thermal/dp/B01H1NA7CE/ref=sr_1_1?s=electronics&ie=UTF8&qid=1480102437&sr=1-1&keywords=Thermal+Receipt+Printer+bluetooth+ios

The question:
Is it possible to share a string or text to a "real printer" ?
Is there a "print" console button?

Thanks again!

tlinnet

The "hack" around the problem is to mark the "Voucher code" in the ui.TextView().

"Share it" to iPhones "Notes" app, and from there "Share it" to a printer.

But the click-path is too long. :)

omz

You could shorten the click path by using dialogs.share_text instead of highlighting the text manually, and selecting Share from the menu.

You could also use the Objective-C bridge (objc_util) to use the iOS printing dialog directly. Here's a simple example:

from objc_util import *

@on_main_thread
def print_text(text):
    UIPrintInteractionController = ObjCClass('UIPrintInteractionController')
    UIPrintSimpleTextFormatter = ObjCClass('UISimpleTextPrintFormatter')
    controller = UIPrintInteractionController.sharedPrintController()
    formatter = UIPrintSimpleTextFormatter.alloc().initWithText_(text).autorelease()
    controller.setPrintFormatter_(formatter)
    controller.presentAnimated_completionHandler_(True, None)

print_text('Hello World')
tlinnet

Thanks!
This works great! :) I can get it all to work as expected.
Now the last little thing. :)


Can I change the font?

I try to "help" myself by understanding the objc
http://omz-software.com/pythonista/docs/ios/objc_util.html.

From Apple I get:
https://developer.apple.com/reference/uikit/uisimpletextprintformatter


Creating a Simple-Text Print Formatter
Returns a simple-text print formatter initialized with attributed text.

init(attributedText: NSAttributedString)

Returns a simple-text print formatter initialized with plain text.

init(text: String)

Getting and Setting the Text. A string of attributed text.

var attributedText: NSAttributedString?

A string of plain text.

var text: String?

Text Attributes for Printed Content; The font of the printed text:

var font: UIFont?

From the code:

@on_main_thread
def print_text(self, text):
    UIPrintInteractionController = ObjCClass('UIPrintInteractionController')
    UISimpleTextPrintFormatter = ObjCClass('UISimpleTextPrintFormatter')
    controller = UIPrintInteractionController.sharedPrintController()
    formatter = UISimpleTextPrintFormatter.alloc().initWithText_(text).autorelease()
    controller.setPrintFormatter_(formatter)
    controller.presentAnimated_completionHandler_(True, None)

I guess I should change "formatter".
But I can figure out how to combine the information. :)

tlinnet

Preferably the font: COURIER and size 10?

omz

@tlinnet Here you go:

from objc_util import *

@on_main_thread
def print_text(text, font_name='Helvetica', font_size=12):
    UIPrintInteractionController = ObjCClass('UIPrintInteractionController')
    UIPrintSimpleTextFormatter = ObjCClass('UISimpleTextPrintFormatter')
    controller = UIPrintInteractionController.sharedPrintController()
    formatter = UIPrintSimpleTextFormatter.alloc().initWithText_(text)
    font = ObjCClass('UIFont').fontWithName_size_(font_name, font_size)
    if font:
        formatter.setFont_(font)
    controller.setPrintFormatter_(formatter)
    controller.presentAnimated_completionHandler_(True, None)

print_text('Hello World', 'Courier', 10)
ccc

Like https://github.com/ColdGrub1384/Pyto/issues/197