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
In Java, we usually use the println() method to print the statement. It belongs to the PrintStream class.
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.
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 PrintService
s supports printing plaintext…
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With