Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't print text file using Java 8 in Windows 7

I've created a report and exported it as a text file, to print in a matrix printer, however, the result of the job is a blank page. I did the same in ubuntu and it is printing correctly. Is it a Java bug?

This is a example code I did to show you the problem:

public class PrintError extends Application {

    public static void main(String args[]) {
        launch(args);
    }

    public void start(Stage stage) throws PrintException {
        PrinterJob printerJob = PrinterJob.createPrinterJob();
        printerJob.showPrintDialog(stage);
        PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
        printRequestAttributeSet.add(new Copies(printerJob.getJobSettings().getCopies()));
        printRequestAttributeSet.add(new JobName("test", Locale.getDefault()));
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        Doc mydoc = new SimpleDoc(ClassLoader.class.getResourceAsStream("/should-be-printed.txt"), flavor, null);
        DocPrintJob job = getPrintService(printerJob.getPrinter().getName()).createPrintJob();
        job.print(mydoc, printRequestAttributeSet);
    }

    private PrintService getPrintService(String name) {
        for (PrintService printService : java.awt.print.PrinterJob.lookupPrintServices()) {
            if (name.equalsIgnoreCase(printService.getName())) {
                return printService;
            }
        }
        return null;
    }
}

This example was created in JavaFx 8 and is running in Java build 1.8.0-b132 in Windows 7. I've also created a simple project at github

like image 734
brevleq Avatar asked Oct 30 '14 20:10

brevleq


People also ask

How do I print a .Java file?

In Java, we usually use the println() method to print the statement. It belongs to the PrintStream class.

How do I read a notepad file in Java?

There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader, or Scanner to read a text file. Every utility provides something special e.g. BufferedReader provides buffering of data for fast reading, and Scanner provides parsing ability. Methods: Using BufferedReader class.


1 Answers

From the documentation:

Recommended DocFlavors

The Java Print Service API does not define any mandatorily supported DocFlavors. …

When you have a PrintService instance, you can use the method getSupportedDocFlavors() to find out which flavors it supports.

When you find out that none of the DocFlavor. INPUT_STREAM. TEXT_PLAIN_… flavors is in the list, it doesn’t help to use AUTOSENSE as that simply means “best guess” and it’s unlikely that a PrintService will guess a type it doesn’t support, instead, it’s more likely that the data will be misinterpreted as one of the formats it supports.

On my Windows machine, none of the provided PrintServices supports printing plaintext…

like image 87
Holger Avatar answered Sep 20 '22 14:09

Holger