A print job sent to the printer but did not print anything. Java

I have a problem with a print service in Java. I need to print a simple text document on my default printer. I use HP Deskjet as my printer on a Windows computer and all drivers are installed. This is the source code I'm using:

import java.io.*; import javax.print.*; public class PrintTest { public static void main(String[] args) throws IOException { File file = new File("print.txt"); InputStream is = new BufferedInputStream(new FileInputStream(file)); //Discover the default print service. PrintService service = PrintServiceLookup.lookupDefaultPrintService(); //Doc flavor specifies the output format of the file. DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; // Create the print job DocPrintJob job = service.createPrintJob(); //Create the Doc Doc doc = new SimpleDoc(is, flavor, null); //Order to print try { job.print(doc, null); } catch (PrintException e) { e.printStackTrace(); } is.close(); System.out.println("Printing done...."); } } 

I see a print job in the printer queue a few milliseconds before it disappears. But nothing is printed. I heard about this because the Java print service in JDK 1.6 is still not working. But I'm not quite sure. Any ideas why?

+4
source share
1 answer

I know this is a very late answer, but I had the same problem on Windows with PDF files (not text). It seems that printers cannot deal with native PDF files, so the work is accepted, but nothing happens (error too). I solved it using a third-party library, Apache PdfBox , and it worked like a charm.

I wrote some code examples, answering a similar question fooobar.com/questions/1391800 / ....

0
source

Source: https://habr.com/ru/post/1391799/


All Articles