B4J Question B4J - Console application for text printing with jFX8print to thermal ticketprinter

JDR

Member
Hello

I'm currently rewriting some code for a replacement of a serial thermal ticketprinter to an USB one.
In the old b4j console application, the code writes serial bytes to the printer to print the ticket.

With the new printer I assume that I need to use the JFX8Print library in order to send a print command to the USB printer.
I watched different threaths on the forum how you could manage printing, but It all includes creating a pane or form.
I couldn't find a way to get this working without the needs of JavaFX, because it's a non graphic application I'm writing.
I tried it with a pane, installed openjfx on the device with the printer connected, but receives the error message: "error: package javafx.scene does not exist", even when I point to the lib folder of Javafx:

Bash command java with javafx:
 sudo java -jar b4j-bridge.jar --module-path /usr/lib/jvm/bellsoft-java13-arm32-vfp-hflt/legal/ --add-modules=javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web

This is the sample code I wrote:
B4J code for printing text:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private TICKETPANE As Pane
    Private fx As JFX
End Sub

Sub AppStart (Args() As String)
    printText
    StartMessageLoop   
End Sub

Private Sub printText
    Private TICKETPRINTER As Printer = Printer_Static.GetDefaultPrinter
    Private TICKETPRINTERATTRIBUTES As PrinterAttributes = TICKETPRINTER.GetPrinterAttributes
    Private TICKETPRINTERPAPER As Paper = TICKETPRINTERATTRIBUTES.GetDefaultPaper
    Private ticketprinterjob As PrinterJob = PrinterJob_Static.CreatePrinterJob2(TICKETPRINTER)
    Private ticketprinterpagelayout As PageLayout = TICKETPRINTER.CreatePageLayout(TICKETPRINTER.GetPrinterAttributes.GetDefaultPaper, PageOrientation_Static.PORTRAIT, 0.5, 0.5, 0.5, 0.5)
    ticketprinterjob.GetJobSettings.SetPageLayout(ticketprinterpagelayout)
    
    ' Create the ticket
    TICKETPANE.Initialize("")
    TICKETPANE.PrefWidth=80
    
    Private ticketHeaderlbl As Label
    ticketHeaderlbl.Initialize("")
    ticketHeaderlbl.Text="LAS VEGAS"
    ticketHeaderlbl.TextSize=15
    ticketHeaderlbl.Style="-fx-alignment:CENTER;-fx-font-weight:bold"
    TICKETPANE.AddNode(ticketHeaderlbl,0,0,80,15)
    
    Private ticketLocationlbl As Label
    ticketLocationlbl.Initialize("")
    ticketLocationlbl.Text="Mont-Noir Westouter"
    ticketLocationlbl.TextSize=12
    ticketLocationlbl.Style="-fx-alignment:CENTER"
    TICKETPANE.AddNode(ticketLocationlbl,0,0,80,10)
    
    Private ticketTitleReculbl As Label
    ticketTitleReculbl.Initialize("")
    ticketTitleReculbl.Text=$"Re${Chr(92)}u gagnant"$
    ticketTitleReculbl.TextSize=15
    ticketTitleReculbl.Style="-fx-alignment:CENTER;-fx-font-weight:bold"
    TICKETPANE.AddNode(ticketTitleReculbl,0,0,80,15)
    
    Try
        ticketprinterjob.PrintPage(TICKETPANE)
    Catch
        Log(LastException)
    End Try
    
End Sub

What can cause this issue?

Thank you in advance!
 

stevel05

Expert
Licensed User
Longtime User
If it were me for a non-ui app, I would look for a java (not Javafx) example on the internet that is known to work and then implement it via Inline-Java or JavaObject, although I have no idea about USB on nonFX java so it may be a non starter without additional libraries. It looks like you're on a Linux box, is it a server or Desktop or other?
 
Last edited:
Upvote 0

JDR

Member
Thank you for your answer!
So if there is inline Java code available without the use of java libraries, just use that?
I'm not sure how that's gone work out, but I can give it a try..

The application is indeed running on a Linux box, just a Raspberry Pi where a thermal ticketprinter is attached via USB.
Drivers and print commands are handled via CUPS, so maybe I can look into that?

I just thought about another way to handle it, create the ticket, save it is a file and then print it with a shell command in the B4J app, but it's not a lean way to handle this problem I guess.

Any other suggestions/ideas?
 
Upvote 0

emexes

Expert
Licensed User
What make and model printer is it?

Try sending plain old boring regular ASCII text to it, see what happens.

Plus try each of the usual three end-of-line / new-line codes:

B4X:
PrintText("AAAA  This is just CR" & CHR(13))
PrintText("     BBBB  This is just LF" & "CHR(10))
PrintText("          CCCCC  This is CR+LF" & CHR(13) & CHR(10))

Can any other apps print to it?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
JFX8Print can be executed in non-ui app
No because it relies on Javafx libraries. javax.print can but there is no library for B4j, and it is more complex than JavaFX printing. I have just been looking at it.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…