This problem can be solved in many ways, it all depends on how you need to make the impression.
If the system you are developing is in Delphi, the situation becomes much easier, there are components that make the work much easier, such as ACBR: http://acbr.sourceforge.net/drupal/
However, the system I am developing, quoted in the question above, was a JAVA EE application running on a local server, on the machine where the printer was connected.
My first attempt was to generate a PDF file, and send this file to print on the printer that was connected, however, these Argox printer models that I was using NAY accept pdf files. However, I realized that if I just generated the PDF, and printed it via S.O, the printer could print.
Only the requirement of the project was that the printing be done automatically, without user interaction. The situation above was enough to understand that the Operating System could solve the printing situation.
But if I can’t send a PDF, how will I set the layout?
These printers usually have a language of their own to define the layout of the applications, in the case of this model of Argox, was the language PPLB, just search through the documentation to learn how to create the layouts.
Code to Print
The code below makes the following steps:
- It detects the S.O
- Inject a command into the terminal to perform the printing
The.txt tag file is a file that was previously generated in the PPLB language, with the values to be printed on the label, it is saved at the root of the project, you can create your own label.txt with only a "Hello world" and execute the command on your S.O terminal to test printing.
public class Etiqueta {
//Imprime a etiqueta de acordo com o sistema operacional
public static void imprimirEtiqueta() throws IOException {
if (SystemUtils.IS_OS_WINDOWS) {
try {
String[] command = {"cmd",};
Process p = Runtime.getRuntime().exec(command);
new Thread(new ThreadSyncPipe(p.getErrorStream(), System.err)).start();
new Thread(new ThreadSyncPipe(p.getInputStream(), System.out)).start();
PrintWriter stdin = new PrintWriter(p.getOutputStream());
stdin.println("type etiqueta.txt > ImpressoraCompartilhadaNoWindows"); //No windows, você precisa compartilhar a impressora na rede e passar o nome dela no comando para imprimir
stdin.close();
int returnCode = p.waitFor();
} catch (Exception e) {
throw new ErrorPrintingLabelException("Erro ao imprimir: Etiqueta ou impressora não encontrada. Erro: "
+ e.getMessage());
}
} else if (SystemUtils.IS_OS_LINUX){
String[] args = new String[] {"/bin/bash", "-c", "cat etiqueta.txt > /dev/usb/lp1"}; //no linux é mais simples, apenas passe o local da impressora
Process proc = new ProcessBuilder(args).start();
} else {
System.out.println("O recurso de impressão de etiquetas não é compatível com este S.O");
}
}
Thread for the printing:
import java.io.InputStream;
import java.io.OutputStream;
class ThreadSyncPipe implements Runnable {
public ThreadSyncPipe(InputStream istrm, OutputStream ostrm) {
istrm_ = istrm;
ostrm_ = ostrm;
}
public void run() {
try {
final byte[] buffer = new byte[1024];
for (int length = 0; (length = istrm_.read(buffer)) != -1; ){
ostrm_.write(buffer, 0, length);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private final OutputStream ostrm_;
private final InputStream istrm_;
}
Despite being a great workaround, works super well in production, I had no problems with printing.
did you manage to resolve your doubt? I find myself in the same situation.
– DevAgil
I did get @Devagil, I commented in response.
– antunesleo_