print on Samsung ML 1610

This commit is contained in:
Jörg Prante 2021-03-18 13:49:27 +01:00
parent 11254626e6
commit b5c01ed7b6
8 changed files with 60 additions and 46 deletions

View file

@ -0,0 +1,5 @@
module org.xbib.graphics.pdfbox.print {
exports org.xbib.graphics.pdfbox.print;
requires java.logging;
requires transitive java.desktop;
}

View file

@ -1,4 +1,4 @@
package org.xbib.graphics.printer; package org.xbib.graphics.pdfbox.print;
import javax.print.Doc; import javax.print.Doc;
import javax.print.DocFlavor; import javax.print.DocFlavor;
@ -27,7 +27,12 @@ import java.util.List;
*/ */
public class PrintUtility { public class PrintUtility {
public static void print(InputStream inputStream, Printer printer) public static void print(InputStream inputStream, DocFlavor docFlavor)
throws Exception {
print(inputStream, docFlavor, findDefaultPrinter(docFlavor));
}
public static void print(InputStream inputStream, DocFlavor docFlavor, Printer printer)
throws Exception { throws Exception {
if (inputStream == null || printer == null) { if (inputStream == null || printer == null) {
return; return;
@ -51,7 +56,7 @@ public class PrintUtility {
pas.add(Sides.TUMBLE); pas.add(Sides.TUMBLE);
break; break;
default: default:
pas.add(Sides.ONE_SIDED); break;
} }
} }
if (printer.isModeSupported()) { if (printer.isModeSupported()) {
@ -63,7 +68,7 @@ public class PrintUtility {
pas.add(Chromaticity.COLOR); pas.add(Chromaticity.COLOR);
break; break;
default: default:
pas.add(Chromaticity.MONOCHROME); break;
} }
} }
if (printer.isMediaSupported() && printer.getMediaSize() != null) { if (printer.isMediaSupported() && printer.getMediaSize() != null) {
@ -74,15 +79,14 @@ public class PrintUtility {
} }
PrintService service = printer.getService(); PrintService service = printer.getService();
DocPrintJob job = service.createPrintJob(); DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(inputStream, Doc doc = new SimpleDoc(inputStream, docFlavor, null);
DocFlavor.INPUT_STREAM.POSTSCRIPT, null);
job.print(doc, pas); job.print(doc, pas);
} }
public static Printer getPrinter(String printerName) { public static Printer getPrinter(String printerName, DocFlavor docFlavor) {
Printer printer = null; Printer printer = null;
if (printerName != null) { if (printerName != null) {
List<Printer> printers = findPrinters(); List<Printer> printers = findPrinters(docFlavor);
for (Printer p : printers) { for (Printer p : printers) {
if (printerName.equalsIgnoreCase(p.getName())) { if (printerName.equalsIgnoreCase(p.getName())) {
printer = p; printer = p;
@ -94,21 +98,25 @@ public class PrintUtility {
PrintService service = PrintServiceLookup.lookupDefaultPrintService(); PrintService service = PrintServiceLookup.lookupDefaultPrintService();
printer = createPrinter(service); printer = createPrinter(service);
} }
if (printer != null) { /*if (printer != null) {
printer.setCopies(1); printer.setCopies(1);
printer.setCollate(false); printer.setCollate(false);
printer.setDuplex(Printer.DUPLEX_SIMPLEX); printer.setDuplex(Printer.DUPLEX_SIMPLEX);
printer.setMode(Printer.STATUS_ACCEPTING_JOBS); printer.setMode(Printer.STATUS_ACCEPTING_JOBS);
printer.setMediaSize("A4"); printer.setMediaSize("A4");
} }*/
return printer; return printer;
} }
public static List<Printer> findPrinters() { public static Printer findDefaultPrinter(DocFlavor docFlavor) {
List<Printer> printers = findPrinters(docFlavor);
return printers.isEmpty() ? null : printers.get(0);
}
public static List<Printer> findPrinters(DocFlavor docFlavor) {
List<Printer> printers = new ArrayList<>(); List<Printer> printers = new ArrayList<>();
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT; PrintService[] printServices = PrintServiceLookup.lookupPrintServices(docFlavor, printRequestAttributeSet);
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, pras);
if (printServices != null) { if (printServices != null) {
for (PrintService service : printServices) { for (PrintService service : printServices) {
printers.add(createPrinter(service)); printers.add(createPrinter(service));
@ -150,11 +158,7 @@ public class PrintUtility {
if (collate == null) { if (collate == null) {
collate = SheetCollate.UNCOLLATED; collate = SheetCollate.UNCOLLATED;
} }
if (collate == SheetCollate.COLLATED) { printer.setCollate(collate == SheetCollate.COLLATED);
printer.setCollate(true);
} else {
printer.setCollate(false);
}
} }
boolean modeSupported = service.isAttributeCategorySupported(Chromaticity.class); boolean modeSupported = service.isAttributeCategorySupported(Chromaticity.class);
printer.setModeSupported(modeSupported); printer.setModeSupported(modeSupported);
@ -189,7 +193,7 @@ public class PrintUtility {
printer.setMediaSupported(mediaSupported); printer.setMediaSupported(mediaSupported);
if (mediaSupported) { if (mediaSupported) {
Object obj = service.getSupportedAttributeValues(Media.class, null, null); Object obj = service.getSupportedAttributeValues(Media.class, null, null);
if (obj != null && obj instanceof Media[]) { if (obj instanceof Media[]) {
Media[] medias = (Media[]) obj; Media[] medias = (Media[]) obj;
for (Media media : medias) { for (Media media : medias) {
if (media instanceof MediaSizeName) { if (media instanceof MediaSizeName) {

View file

@ -1,4 +1,4 @@
package org.xbib.graphics.printer; package org.xbib.graphics.pdfbox.print;
import javax.print.PrintService; import javax.print.PrintService;
import javax.print.attribute.standard.Media; import javax.print.attribute.standard.Media;
@ -29,7 +29,7 @@ public class Printer {
private int duplex; private int duplex;
private boolean mediaSupported; private boolean mediaSupported;
private String mediaSize; private String mediaSize;
private Map<String, Media> mediaSizeNames = new HashMap<>(); private final Map<String, Media> mediaSizeNames = new HashMap<>();
private int pageSize; private int pageSize;
private String pageRange; private String pageRange;
private PrintService service; private PrintService service;

View file

@ -0,0 +1,28 @@
package org.xbib.graphics.pdfbox.print.test;
import javax.print.DocFlavor;
import org.junit.jupiter.api.Test;
import org.xbib.graphics.pdfbox.print.PrintUtility;
import org.xbib.graphics.pdfbox.print.Printer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PrinterTest {
private static final Logger logger = Logger.getLogger(PrinterTest.class.getName());
@Test
public void testPrinterSelection() {
logger.log(Level.INFO, "printer = " + PrintUtility.findPrinters(DocFlavor.INPUT_STREAM.PDF));
}
@Test
public void print() throws Exception {
Printer printer = PrintUtility.getPrinter("Samsung_ML-1610", DocFlavor.INPUT_STREAM.PDF);
if (printer != null) {
PrintUtility.print(getClass().getResourceAsStream("/test.pdf"), DocFlavor.INPUT_STREAM.PDF, printer);
}
}
}

Binary file not shown.

View file

@ -1,5 +0,0 @@
module org.xbib.graphics.printer {
exports org.xbib.graphics.printer;
requires java.logging;
requires transitive java.desktop;
}

View file

@ -1,18 +0,0 @@
package org.xbib.graphics.printer.test;
import org.junit.jupiter.api.Test;
import org.xbib.graphics.printer.PrintUtility;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PrinterTest {
private static final Logger logger = Logger.getLogger(PrinterTest.class.getName());
@Test
public void testPrinterSelection() {
logger.log(Level.INFO, "printer = " + PrintUtility.findPrinters());
}
}

View file

@ -9,4 +9,4 @@ include 'graphics-ghostscript'
include 'graphics-pdfbox' include 'graphics-pdfbox'
include 'graphics-pdfbox-layout' include 'graphics-pdfbox-layout'
include 'graphics-pdfbox-groovy' include 'graphics-pdfbox-groovy'
include 'graphics-printer' include 'graphics-pdfbox-print'