Windows printing

This commit is contained in:
Jörg Prante 2021-03-18 14:32:42 +01:00
parent b5c01ed7b6
commit 2bfb9c1803
7 changed files with 33 additions and 12 deletions

View file

@ -25,6 +25,9 @@ ext {
subprojects { subprojects {
apply plugin: 'java-library' apply plugin: 'java-library'
repositories {
mavenCentral()
}
apply from: rootProject.file('gradle/ide/idea.gradle') apply from: rootProject.file('gradle/ide/idea.gradle')
apply from: rootProject.file('gradle/compile/java.gradle') apply from: rootProject.file('gradle/compile/java.gradle')
apply from: rootProject.file('gradle/test/junit5.gradle') apply from: rootProject.file('gradle/test/junit5.gradle')

View file

@ -36,6 +36,7 @@ artifacts {
} }
tasks.withType(JavaCompile) { tasks.withType(JavaCompile) {
options.encoding('UTF-8')
options.compilerArgs << '-Xlint:all' options.compilerArgs << '-Xlint:all'
} }

View file

@ -54,7 +54,8 @@ if (project.hasProperty("signing.keyId")) {
} }
} }
nexusPublishing { if (project.hasProperty('ossrhUsername')) {
nexusPublishing {
repositories { repositories {
sonatype { sonatype {
username = project.property('ossrhUsername') username = project.property('ossrhUsername')
@ -63,4 +64,5 @@ nexusPublishing {
} }
} }
clientTimeout = Duration.ofSeconds(600) clientTimeout = Duration.ofSeconds(600)
}
} }

View file

@ -0,0 +1,3 @@
dependencies {
api "org.apache.pdfbox:pdfbox:${project.property('pdfbox.version')}"
}

View file

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

View file

@ -1,5 +1,8 @@
package org.xbib.graphics.pdfbox.print; package org.xbib.graphics.pdfbox.print;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;
import javax.print.Doc; import javax.print.Doc;
import javax.print.DocFlavor; import javax.print.DocFlavor;
import javax.print.DocPrintJob; import javax.print.DocPrintJob;
@ -18,6 +21,7 @@ import javax.print.attribute.standard.PrinterMakeAndModel;
import javax.print.attribute.standard.SheetCollate; import javax.print.attribute.standard.SheetCollate;
import javax.print.attribute.standard.Sides; import javax.print.attribute.standard.Sides;
import java.awt.print.PrinterJob;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -83,6 +87,14 @@ public class PrintUtility {
job.print(doc, pas); job.print(doc, pas);
} }
public static void print(InputStream inputStream, Printer printer) throws Exception {
PDDocument document = PDDocument.load(inputStream);
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document));
job.setPrintService(printer.getService());
job.print();
}
public static Printer getPrinter(String printerName, DocFlavor docFlavor) { public static Printer getPrinter(String printerName, DocFlavor docFlavor) {
Printer printer = null; Printer printer = null;
if (printerName != null) { if (printerName != null) {

View file

@ -14,15 +14,14 @@ public class PrinterTest {
@Test @Test
public void testPrinterSelection() { public void testPrinterSelection() {
logger.log(Level.INFO, "printer = " + PrintUtility.findPrinters(DocFlavor.INPUT_STREAM.PDF)); logger.log(Level.INFO, "printer = " + PrintUtility.findPrinters(DocFlavor.INPUT_STREAM.AUTOSENSE));
} }
@Test @Test
public void print() throws Exception { public void print() throws Exception {
Printer printer = PrintUtility.getPrinter("Samsung_ML-1610", DocFlavor.INPUT_STREAM.PDF); Printer printer = PrintUtility.getPrinter("Samsung ML-1610 (USB001)", DocFlavor.INPUT_STREAM.AUTOSENSE);
if (printer != null) { if (printer != null) {
PrintUtility.print(getClass().getResourceAsStream("/test.pdf"), DocFlavor.INPUT_STREAM.PDF, printer); PrintUtility.print(getClass().getResourceAsStream("/test.pdf"), printer);
} }
} }
} }