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.DocFlavor;
@ -27,7 +27,12 @@ import java.util.List;
*/
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 {
if (inputStream == null || printer == null) {
return;
@ -51,7 +56,7 @@ public class PrintUtility {
pas.add(Sides.TUMBLE);
break;
default:
pas.add(Sides.ONE_SIDED);
break;
}
}
if (printer.isModeSupported()) {
@ -63,7 +68,7 @@ public class PrintUtility {
pas.add(Chromaticity.COLOR);
break;
default:
pas.add(Chromaticity.MONOCHROME);
break;
}
}
if (printer.isMediaSupported() && printer.getMediaSize() != null) {
@ -74,15 +79,14 @@ public class PrintUtility {
}
PrintService service = printer.getService();
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(inputStream,
DocFlavor.INPUT_STREAM.POSTSCRIPT, null);
Doc doc = new SimpleDoc(inputStream, docFlavor, null);
job.print(doc, pas);
}
public static Printer getPrinter(String printerName) {
public static Printer getPrinter(String printerName, DocFlavor docFlavor) {
Printer printer = null;
if (printerName != null) {
List<Printer> printers = findPrinters();
List<Printer> printers = findPrinters(docFlavor);
for (Printer p : printers) {
if (printerName.equalsIgnoreCase(p.getName())) {
printer = p;
@ -94,21 +98,25 @@ public class PrintUtility {
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
printer = createPrinter(service);
}
if (printer != null) {
/*if (printer != null) {
printer.setCopies(1);
printer.setCollate(false);
printer.setDuplex(Printer.DUPLEX_SIMPLEX);
printer.setMode(Printer.STATUS_ACCEPTING_JOBS);
printer.setMediaSize("A4");
}
}*/
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<>();
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(docFlavor, printRequestAttributeSet);
if (printServices != null) {
for (PrintService service : printServices) {
printers.add(createPrinter(service));
@ -150,11 +158,7 @@ public class PrintUtility {
if (collate == null) {
collate = SheetCollate.UNCOLLATED;
}
if (collate == SheetCollate.COLLATED) {
printer.setCollate(true);
} else {
printer.setCollate(false);
}
printer.setCollate(collate == SheetCollate.COLLATED);
}
boolean modeSupported = service.isAttributeCategorySupported(Chromaticity.class);
printer.setModeSupported(modeSupported);
@ -189,7 +193,7 @@ public class PrintUtility {
printer.setMediaSupported(mediaSupported);
if (mediaSupported) {
Object obj = service.getSupportedAttributeValues(Media.class, null, null);
if (obj != null && obj instanceof Media[]) {
if (obj instanceof Media[]) {
Media[] medias = (Media[]) obj;
for (Media media : medias) {
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.attribute.standard.Media;
@ -29,7 +29,7 @@ public class Printer {
private int duplex;
private boolean mediaSupported;
private String mediaSize;
private Map<String, Media> mediaSizeNames = new HashMap<>();
private final Map<String, Media> mediaSizeNames = new HashMap<>();
private int pageSize;
private String pageRange;
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-layout'
include 'graphics-pdfbox-groovy'
include 'graphics-printer'
include 'graphics-pdfbox-print'