Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(print): Add print request attributes #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import com.github.anastaciocintra.escpos.EscPos;
import com.github.anastaciocintra.output.PrinterOutputStream;

import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.JobName;
import java.io.IOException;

public class HelloWorld {
Expand All @@ -53,6 +56,10 @@ public class HelloWorld {

PrintService printService = PrinterOutputStream.getPrintServiceByName(args[0]);
PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService);
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new JobName("your job name", null));
printerOutputStream.setPrintRequestAttributeSet(attributes);

EscPos escpos = new EscPos(printerOutputStream);
escpos.writeLF("Hello world");
escpos.feed(5).cut(EscPos.CutMode.FULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.PrintRequestAttributeSet;

/**
* Supply OutputStream to the printer.
Expand All @@ -30,6 +31,7 @@ public class PrinterOutputStream extends PipedOutputStream {

protected final PipedInputStream pipedInputStream;
protected final Thread threadPrint;
private PrintRequestAttributeSet printRequestAttributeSet=null;

/**
* creates one instance of PrinterOutputStream.
Expand Down Expand Up @@ -57,7 +59,7 @@ public PrinterOutputStream(PrintService printService) throws IOException {
Doc d = new SimpleDoc(pipedInputStream, df, null);

DocPrintJob job = printService.createPrintJob();
job.print(d, null);
job.print(d, this.printRequestAttributeSet);
} catch (PrintException ex) {
throw new RuntimeException(ex);
}
Expand Down Expand Up @@ -164,4 +166,18 @@ public static PrintService getPrintServiceByName(String printServiceName) {
return foundService;
}

/**
* Set printer request attributes.
* <p>
* For example Job name:
* <i>attributes.add(new JobName("your job name", null));</i>
* Null by default
* Could be set before each printer job
* Save previous state attributes for next jobs.
* </p>
* @param attributes printer request attributes.
*/
public void setPrintRequestAttributeSet(PrintRequestAttributeSet attributes){
this.printRequestAttributeSet = attributes;
}
}