Java Printing - Docprintjob

Asked

Viewed 1,003 times

1

I have this method to make impression:

public void imprimir(String texto) {
    PrintService[] printService = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE, null);
    PrintService impressoraPadrao = PrintServiceLookup.lookupDefaultPrintService();
    DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    HashDocAttributeSet hashDocAttributeSet = new HashDocAttributeSet();

    InputStream stream = new ByteArrayInputStream((texto + "\n").getBytes());
    Doc doc = new SimpleDoc(stream, docFlavor, hashDocAttributeSet);
    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
    PrintService printServico = ServiceUI.printDialog(null, 300, 200, printService, impressoraPadrao, docFlavor, printRequestAttributeSet);
    if (printServico != null) {
        DocPrintJob docPrintJob = printServico.createPrintJob();
        try {
            docPrintJob.print(doc, printRequestAttributeSet);
        } catch (PrintException e) {
            JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
        }
    }
}

There is no mistake.

But it also doesn’t print, and when I send the printing to Pdfcreator the page comes out blank.

But if I send a picture like this it prints normal:

public void imprimir(String texto) {
    PrintService[] printService = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE, null);
    PrintService impressoraPadrao = PrintServiceLookup.lookupDefaultPrintService();
    DocFlavor docFlavor = DocFlavor.INPUT_STREAM.PNG;
    HashDocAttributeSet hashDocAttributeSet = new HashDocAttributeSet();

    try {
         FileInputStream stream = new FileInputStream("C:\\Users\\Rodrigo\\Desktop\\Trab4_Luc\\teste.png");
         Doc doc = new SimpleDoc(stream, docFlavor, hashDocAttributeSet);
         PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
         PrintService printServico = ServiceUI.printDialog(null, 300, 200, printService, impressoraPadrao, docFlavor, printRequestAttributeSet);
         if (printServico != null) {
         DocPrintJob docPrintJob = printServico.createPrintJob();
         try {
             docPrintJob.print(doc, printRequestAttributeSet);
         } catch (PrintException e) {
             JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
         }
    } catch (FileNotFoundException ex) {
         JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
    }
}

What am I doing wrong?

1 answer

4


The difference between the code that works is

DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

for

DocFlavor docFlavor = DocFlavor.INPUT_STREAM.PNG;

I suggest changing to:

DocFlavor docFlavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;
HashDocAttributeSet hashDocAttributeSet = new HashDocAttributeSet();

InputStream stream = new ByteArrayInputStream((texto + 
"\n").getBytes(StandardCharsets.UTF_8));

Using a hex editor, I’m sure he’s sending this to the printer. I can’t say that the printer knows how to print what I’m sending.

If this doesn’t work for you, then the only way, seems to be to render the text in image, and proceed with the alternate path you quoted.

  • I was able to make the impression... My question now is to send ESC/POS commands to the printer, I created a topic with the question, if you can help me: http://answall.com/questions/130901/esc-pos-impress%C3%A3o Thanks

  • @Rodrigolima Mark the answer as accepted if I have solved your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.