Printing reports

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Printing reports

Post by Henko »

The built printfile is just a sequence of strings. If i pick up the local IP adress of my printer, it might be possible to send the printfile to the printer by using a sequence of the following statement: HTTP URL$ HEADER H$ POST T$.

Am i right? I will try it, and report back.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Printing reports

Post by rbytes »

Could be. My HP network printer actually provides an email address that I can send documents to. Unfortunately something isn't quite right with their system, because I get very odd letter spacing in my fonts, making the output look quite amateurish.

I have been doing additional testing with the SB browser. It seems that quite a number of JavaScript commands that work in Safari and other professional browsers don't work in the SB browser. Anything that involves a button seems to fail. The button appears, but doesn't do what the JavaScript says it should. Possibly apps that are standalone browsers have more access to iOS than a browser within a programming language.
The only thing that gets me down is gravity...

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Printing reports

Post by Henko »

PROBLEM SOLVED!
I could not find the solution myself, so i asked my son, who had a solution very quickly.
He made a simple webserver on his Windows 10 system using Python. The server listens to port 80 for incoming post messages (a string to be printed). If the string is recieved completely, it is printed on the (default) printer using Notepad (Notepad /p file$).
This webserver was started, and from SB on my iPhone i sent a two lines text (chr$(13) and chr$10) to separate the lines) to the URL of the windows machine, using the "http post" command. And it was printed right away.

Now this has to be implemented on my own Windows system (install Python, webserver on the PC and a shortcut, or autostart when the PC is started).
Next the necessary SB coding to pack a report in one string, and other features to produce nice reports with proper headings on each new page of a multi page report.

With such a printing facility SB becomes a real general pupose programming tool (IMHO)

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Printing reports

Post by rbytes »

This will really be useful. I hope you will post the detailed steps needed to implement it
The only thing that gets me down is gravity...

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Printing reports

Post by Henko »

Yes, it works already on my own Windows system.
This evening is football Brasil vs. Belgium. Tomorrow i will describe the steps (rather simple) and publish the webserver code (some 10 lines only, but i have asked and gotten the permission of the programmer). Python is needed however, the little program is in Python.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Printing reports

Post by rbytes »

I have the Pythonista app, so maybe that will work.
The only thing that gets me down is gravity...

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Printing reports

Post by Henko »

Printable strings are sent from SB on an iOS device to a webserver on a PC system with an attached default printer. For the actual printing, the webserver uses the Windows program Notepad.
As far as i know, Pythonista is an iOS app and not available on PC. Why should it, because on PC the original Python is free for use. Installing Python on the PC is a matter of minutes.

I guess it should not be too difficult to make a comparable printing facility on an Apple Mac system.

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Printing reports

Post by Henko »

Direct printing from a SB program on an iOS device to a default printer in a PC system.

The headline is as follows: A SB program prepares a string to be printed on the printer. The program sends the string via the SB command "HTTP URL$ HEADER H$ POST T$" to the URL of a PC using WiFi. There it is recieved by a small Python webserver program on port 80. The webserver then uses a NOTEPAD command to print the string on the defaultprinter.

Of course a number of Alternatives are possible (MAC in stead of PC, other language for the webserver), but the current chosen solution is well tested and works all right.

The minimal steps to implement and test this feature are as follows:

1. Install Python on the PC system (free, website "Python.org" -> "Download"), if not already installed.
2. Create or choose a directory where to run the webserver (i created C:\Henk)
3. Copy/paste the webserver code into Notepad and save it in the chosen directory giving it a filename with extension .py. I used "iOS2print.py". The python code follows:

Code: Select all

# Software by Michiel Overtoom, motoom@xs4all.nl

import os
from http.server import BaseHTTPRequestHandler, HTTPServer

class MyPrintService(BaseHTTPRequestHandler):
    def do_POST(self):
        content_length = int(self.headers["Content-Length"])
        post_data = self.rfile.read(content_length)        
        with open("report.txt", "wb") as f:
            f.write(post_data)
        os.system("notepad /p report.txt")

if __name__ == "__main__":
    print("Print Server started...")
    HTTPServer(("0.0.0.0", 80), MyPrintService).serve_forever() 
4. Open the command line window (CMD), switch to rhe webserver directory ("CD \Henk" in my case) and start the server: "python iOS2print.py" (in my case). The message "Print Server started" should appear.
5. Make a little testprogram, or use mine:

Code: Select all

option base 1
url$="192.168.2.9"   ' the URL of the host PC
dim h$(2)

' construct a string to measure how many caracters per line and
' how many lines fit on one page
'
t$="1234567890"
for i=1 to 80 ! s$&="regel "&i&chr$(13)&chr$(10) ! next i
for i=1 to 10 ! s$&=t$ ! next i

h$(1) = "content-type:text/html"   ' make header info
h$(2) = "content-length:" & len(s$)

HTTP URL$ HEADER H$ POST s$   ' send doc to the webserver
end

6. Switch the printer on, wait until it is ready and run the testprogram. The content of the string should be printed now.
7. The webserver may be stopped with CTRL-C.

Some remarks:
- i have zero knowledge about the server program. My son Michiel designed the method and coded the program. He is willing to discuss about it via email (see header of the program).
- step 4. may be strongly simplified by making a .bat file and create a shortcut for it on the desktop.
- This solution realizes a "low level" print facility for iOS devices. In the coming period i will create some functions to produce neat reports.

Happy printing! ;)

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Printing reports

Post by rbytes »

Nice solution! I will give it a try. :D
The only thing that gets me down is gravity...

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: Printing reports

Post by GeorgeMcGinn »

To print directly from either an App designed in SmartBASIC or from the SmartBASIC IDE, you need to use App Extensions, which allows either the IDE or your App designed in SmartBASIC to access printing apps like Print Pro or "printing" to Apps like Notepad, Pages, MS Word, Adobe PDF, etc.

App Extensions refers to either the square box with the arrow pointed up, or in apps like Pages, a link called "Locations" to print/save to any or all cloud apps loaded on your iPad/iPhone.

This needs to be coded in both the core code of the interpreter and set up in the SDK.

I started to look into this, but at the time I was not a paid member of Apple Developers program, which I am now.

I was originally thinking it only had to be added to the XCode project, but more code needs to enable it.

Otherwise, the only way to print is to create an output dataset to a cloud, or a file that you can cut/paste to your favorite notes/journaling/word processor and print it that way.
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

Post Reply