I am a newbie, but from reading the b4a forum I may have misunderstood if it is "easy" to print to wireless network printers, or not.
I downloaded from Google Play Store the free app "Lets Print Droid" which seems to say it can do so, plus in the help file it says how to handle
android.intents
Here is the section from their installed help file, and I would welcome comments from anyone who has tried to print from b4a natively.
I downloaded from Google Play Store the free app "Lets Print Droid" which seems to say it can do so, plus in the help file it says how to handle
android.intents
Here is the section from their installed help file, and I would welcome comments from anyone who has tried to print from b4a natively.
B4X:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Printing from Another App using LPD
Let’s Print Droid (LPD) can be used by other Android apps to allow them to print.
This is done via Android Intents. Using the “android.intent.action.SEND” or VIEW Intent
and a properly formed URI you can programmatically send a file to LPD to be printed.
Before you begin you must ensure the user has installed the LPD app.
Here is a short example that gives the user a choice of apps with which to print.
It would include LPD if installed:
Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("application/pdf");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://dir/file.pdf"));
startActivity(Intent.createChooser(i, "Print file.pdf"));
If you know that the user will have the LPD app pre-installed you can be more direct and
avoid the app chooser dialog:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setClassName("com.blackspruce.lpd","com.blackspruce.lpd.ReceiveSentFile");
i.setData( Uri.parse("file://dir/file.jpg"));
startActivityForResult(i, 99);
LPD will print the file and return control to your app via the onActivityResult method :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// returning from Let’s Print Droid
if (data != null&& requestCode == 99 && resultCode == RESULT_OK) {
// the file printed OK if resultCode == RESULT_OK
}
But WAIT, there’s more! If you act now we’ll include the ability to specify the printer details
in the intent. LPD will look for Extended Data in the intent so that the user does not need
to configure the LPD App printer in advance. All you need to do is call “android.content.Intent.putExtra()” and pass a string of JSON data to the LPD app.
Here is an example:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setClassName("com.blackspruce.lpd","com.blackspruce.lpd.ReceiveSentFile");
i.setData( Uri.parse("file://dir/file.jpg"));
JSONObject object = new JSONObject();
try {
object.put("name", "My Cool App - Print Server" );
object.put("port", 631 ); // standard CUPS/IPP port
object.put("address", "192.164.0.10" ); // IP addr or full DNS name
object.put("queue", "/printers/MyPrinter" ); // CUPS printer/class for IPP
object.put("protocol", "IPP" ); // valid values : IPP(S), LPR,HTTP, etc.
object.put("PDL", "PCL" ); // valid values : JPG, PCL, PCL-C, PS, PDF
} catch (JSONException e) {
e.printStackTrace();
}
i.putExtra("jsonPrinter",object.toString());
startActivityForResult(i, 99);
There are plenty of options for the Protocol and PDL (Page Definition Language) and consequently plenty of ways to screw up, so a little caution and plenty of testing may be required.
Here is a quick list of the protocols:
IPP | IPPS – CUPS Internet Printing Protocol and CUPS with SSL encryption. Standard port 631.
LPR – Line Printer Remote. Standard Port 515.
RAW – Jet Direct / RAW. Standard Port 9100.
SMB – SAMBA / Server Message Block (Microsoft Printer Sharing protocol). Standard port 445.
GCP – Google Cloud Print. Standard port 443. (This is not a full integration to Google Cloud Print ; just a pass thru to their web client.)
HTTPS | HTTPS – Hyper Text Transfer Protocol (POST command) and
HTTP with SSL. Ports are 80 for HTTP and 443 for HTTPS.
FTP | FTPS – File Transfer Protocol and FTP with SSL.
SHARE – Generate a Send Intent with selection dialog, instead of actually printing the
output PDL. Use Port 0 and IP Address “null”. Queue =”Select Dialog”.
In all cases the meaning of the “queue” JSON value morphs to reflect what is needed by
the protocol. It may help to think of the JSON string as an URL and envision it this way
(The PDL will represent the format of the actual data being sent):
protocol://address:port/queue
For IPP the queue is typically like “/printers/MyPrinter”.
For LPR it will be an actual printer name similar to the Unix LPR command ;
e.g. “lpr –H IPAddress:Port –P “QueueName” file.ext”
For RAW, it is a queue name
for SMB, the shared printer name
for GCP, it is ignored
for HTTP it is the URL path
for FTP the remote directory.
The options for PDL (Page Definition Language) are :
JPG – Really just RAW format. Send the document to be printed as it exists; if it is a JPG
send the JPG, if PDF send the PDF and assume the destination printer can handle
the rendering and rasterization. You would be surprised what some modern laser printers
understand. Most CUPS installations using Foomatic/Ghostscript will understand this format.
PCL – HP Printer Command Language level 5. Generic black and white PCL with dithered images.
PCL-C – HP PCL level 5C. Generic colour PCL.
PS – Generic Postscript level 2.
PDF – Portable Document Format (Adobe) level 1.3
One important note: some types of source input files cannot be converted to some destination
PDL formats. In particular this version of LPD does not yet contain rendering and raster-ization
of source PDF, MS Word Docs , etc. Only JPG, PNG, GIF, TXT and HTML can be converted to
PDL.
Rendering/raster-ization of source input PDF will appear in later levels of the app. Future
versions will also accept a JSON string specifying print job parameters such as, pages to print,
duplexing , portrait/landscape, etc.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *