Recent

Author Topic: LAMW: Print to network printer  (Read 865 times)

dseligo

  • Hero Member
  • *****
  • Posts: 1219
LAMW: Print to network printer
« on: November 26, 2023, 06:19:36 pm »
How to print directly to a network printer?

I found some example in Java, but I don't know how to translate it to Free Pascal: https://riggaroo.dev/simple-printing-in-android-to-a-network-printer/

Code: Java  [Select][+][-]
  1. DataOutputStream outToServer;
  2. Socket clientSocket;
  3. try {
  4.    createHtmlDocument(htmlString);
  5.    FileInputStream fileInputStream = new FileInputStream(android.os.Environment.getExternalStorageDirectory()  + java.io.File.separator+ "test.pdf");
  6.    InputStream is =fileInputStream;
  7.    clientSocket = new Socket(ipAddress, portNumber);
  8.    outToServer = new DataOutputStream(clientSocket.getOutputStream());
  9.    byte[] buffer = new byte[3000];
  10.    while (is.read(buffer) !=-1){
  11.       outToServer.write(buffer);
  12.    }
  13.    outToServer.flush();
  14.    return 1;
  15. }catch (ConnectException connectException){
  16.    Log.e(TAG, connectException.toString(), connectException);
  17.    return -1;
  18. }
  19.    Log.e(TAG, e1.toString(), e1);
  20.    return 0;
  21. } catch (IOException e1) {
  22.    Log.e(TAG, e1.toString(), e1);
  23.    return 0;
  24. }finally {
  25.    outToServer.close();
  26.  
  27. }

loaded

  • Hero Member
  • *****
  • Posts: 825
Re: LAMW: Print to network printer
« Reply #1 on: November 27, 2023, 07:01:10 am »
First of all, it is worth noting that LAMW uses java in the background.
I recommend you to examine LAMW examples on the subject.
Code: Pascal  [Select][+][-]
  1. ..\lazandroidmodulewizard-master\demos\GUI\
Probably the AppIntentDemoPrinting1 example can be of use to you.

I take the following approach: In my application, I first convert the output to pdf and then leave the initiative to the user. User ;
- If it wants, it can send it directly to the wired/wireless printer itself.
- Can save as pdf if desired then send to printer or bluetooth or whatsapp at a later time.
However, I do most of the above operations directly in Java in Android Studio.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

dseligo

  • Hero Member
  • *****
  • Posts: 1219
Re: LAMW: Print to network printer
« Reply #2 on: November 27, 2023, 09:22:08 am »
Probably the AppIntentDemoPrinting1 example can be of use to you.

I saw that example, but it is not direct printing, it calls 3rd party applications. Furthermore, first application wants upgrade to premium to use network printer, and second application is made for older version of Android.
I want to print without further user interaction (I have IP address of the printer stored in the settings).

Also, printing on Windows shared printers would also be great.

loaded

  • Hero Member
  • *****
  • Posts: 825
Re: LAMW: Print to network printer
« Reply #3 on: November 28, 2023, 07:15:30 am »
I saw that example, but it is not direct printing, it calls 3rd party applications. Furthermore, first application wants upgrade to premium to use network printer, and second application is made for older version of Android.
I want to print without further user interaction (I have IP address of the printer stored in the settings).

Also, printing on Windows shared printers would also be great.

I understand you're right.
I'm at work right now and unfortunately I don't have the opportunity to run these examples, so I want to tell you about what I do in such situations.
When I encounter such problems, I use Android Studio + LAMW to find solutions more easily.
When it comes to Android-related operations, I first make my experiments on Android Studio. After the project runs smoothly, I add java codes to LAMW as needed.

First of all, I believe that Master jmpessoa, one of the authors of LAMW, can easily find the solution to this problem.

Or if you examine the files below, you will see the working logic of LAMW more clearly. You can then modify it according to your needs.
Of course, all of these processes will require some trial and error.

Regarding the subject, Java Files used by LAMW. The java files here are transferred to the relevant projects. (You can make changes in these files.);
Code: Pascal  [Select][+][-]
  1. ...\lazandroidmodulewizard-master\android_wizard\smartdesigner\java

Pas files that provide links to Java files. (You can make changes in these files.);
Code: Pascal  [Select][+][-]
  1. ...\lazandroidmodulewizard-master\android_bridges

The java files used by the printing example mentioned in the previous message are;
Code: Pascal  [Select][+][-]
  1. ...\lazandroidmodulewizard-master\demos\GUI\AppIntentDemoPrinting1\src\org\lamw\appintentdemoprinting1

Note: This approach may not be very professional. I welcome experts to offer their criticisms.  :-X
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

 

TinyPortal © 2005-2018