omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Share string/text to "Real printer" from app

    Pythonista
    3
    7
    4840
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • tlinnet
      tlinnet last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • tlinnet
        tlinnet last edited by

        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. :)

        1 Reply Last reply Reply Quote 0
        • omz
          omz last edited by 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')
          
          1 Reply Last reply Reply Quote 0
          • tlinnet
            tlinnet last edited by

            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. :)

            1 Reply Last reply Reply Quote 0
            • tlinnet
              tlinnet last edited by

              Preferably the font: COURIER and size 10?

              1 Reply Last reply Reply Quote 0
              • omz
                omz last edited by

                @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)
                
                1 Reply Last reply Reply Quote 0
                • ccc
                  ccc last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post
                  Powered by NodeBB Forums | Contributors