Sending data to a printer in Java
Using only JPS you will have problems with Mac. My suggestion is use Java 2 Print API + Java Print Service.
Java 2 Print API is something like 1990 style. To avoid to create your code using Java 2 Print API you could use PDFBox http://pdfbox.apache.org as a framework.
With PDFBox you could create a PDF document (http://pdfbox.apache.org/1.8/cookbook/documentcreation.html) but instead of save, print it using that code:
PrinterJob printJob = PrinterJob.getPrinterJob();
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
printJob.setPrintService(service);
document.silentPrint(printJob);
It works fine in my Mac.
Related videos on Youtube
Andrew Lynch
A university student pursing a Master Degree in International Relations who is learning to code in Ruby on Rails. Contributor for memeburn.com Entrepreneur @ CableKiosk.co.za
Updated on June 04, 2022Comments
-
Andrew Lynch about 2 years
The code below sends data to a printer however, while it reaches the printer queue it comes back with a
Unable to convert PostScript file.
I thought that this would be overcome by specifying the flavor but this is not the caseimport javax.print.Doc; import javax.print.DocFlavor; import javax.print.DocPrintJob; import javax.print.PrintException; import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.SimpleDoc; import javax.print.attribute.PrintServiceAttribute; import javax.print.attribute.standard.PrinterName; public class New1 { public static void main(String[] args) { try { String s = "Hello"; // byte[] by = s.getBytes(); DocFlavor flavor = DocFlavor.STRING.TEXT_PLAIN; PrintService pservice = PrintServiceLookup.lookupDefaultPrintService(); DocPrintJob job = pservice.createPrintJob(); Doc doc = new SimpleDoc(s, flavor, null); job.print(doc, null); } catch (PrintException e) { e.printStackTrace(); } } }
-
Robert almost 12 yearsHave you tried using an
DocFlavor.INPUT_STREAM.AUTOSENSE
and anStringInputStream
instead? -
James Harpe about 11 yearsI'm having the same problem and DocFlavor.INPUT_STREAM.AUTOSENSE does not help.
-