Yuv*_*val 6 printing monitoring driver
我希望能够在网络打印机物理完成打印页面(和/或整个作业)时收到通知。这将用于我正在编写的用于通过网络进行打印管理的应用程序中,并且由于用户按每页收费,并且在页面实际完成之前费用不应下降。
我不确定这是否需要编写驱动程序、某种插件或客户端应用程序是否可以。我对我的平台很灵活,因为我的客户端还没有编写,所以我想听听任何适用于 Windows 或 Linux 的任何编程语言/级别的解决方案。
我知道假脱机程序和打印机之间存在差异。我正在尝试检查当页面或作业物理完成时,打印机可能会通过 IPP 通知机器的级别。
我目前正在研究 Java,使用jspi或cups4j包在 IPP 属性job-impressions-completed
更改时获取通知,或者轮询它。我正在使用 CUPS IPP 接口连接到本地打印机。运行一个简单的测试器(HelloPrint.java
附在下面;或CupsTest.java
包含在 cups4j 中),我没有收到任何job-impressions-completed
属性更改,也没有在我轮询时列出作业的属性。
所以这里是问题:
job-impressions-completed
属性可能未更新,特别是因为它充当真实打印机的假脱机程序。假设真正的打印机将通知或列出此属性,这是特定于打印机的还是必须任何支持 IPP 的打印机具有此属性可用和更新?系统信息:Ubuntu 11.10,CUPS 1.5.0,打印机是 Brother HL-2240D(此处提供 PPD)
注意:HL-2240D不是我将用于最终项目的打印机(具体来说,它不支持 IPP);我打算使用 HP HL4250DN 或三星 3741ND 或类似产品。
这是一个使用javax.print
包和 jspi的示例应用程序:
你好打印
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.*;
import de.lohndirekt.print.IppPrintService;
public class HelloPrint {
/**
* @param args
*/
public static void main(String[] args) {
// create request attributes
PrintRequestAttributeSet requestAttributeSet = new HashPrintRequestAttributeSet();
requestAttributeSet.add(MediaSizeName.ISO_A4);
requestAttributeSet.add(new Copies(1));
requestAttributeSet.add(Sides.DUPLEX);
// find an appropriate service
// using jspi (http://code.google.com/p/jspi/)
URI printerURI;
try {
printerURI = new URI("ipp://localhost:631/printers/HL2240D-local");
} catch (URISyntaxException e2) {
e2.printStackTrace();
return;
}
IppPrintService service = new IppPrintService(printerURI);
// by enumerating
// PrintService[] services = PrintServiceLookup.lookupPrintServices(
// DocFlavor.INPUT_STREAM.PDF, requestAttributeSet);
// for (PrintService service1 : services) {
// System.out.println(service1);
// }
// PrintService service = services[0];
// add listeners to service
service.addPrintServiceAttributeListener(new PrintServiceAttributeListener() {
@Override
public void attributeUpdate(PrintServiceAttributeEvent event) {
PrintServiceAttributeSet serviceAttributeSet = event
.getAttributes();
StringBuilder s = new StringBuilder();
s.append("=== PrintServiceAttributeEvent: (" + serviceAttributeSet.size() + " attributes)\n");
for (Attribute attribute : serviceAttributeSet.toArray()) {
PrintServiceAttribute printServiceAttribute = (PrintServiceAttribute) attribute;
s.append(printServiceAttribute.getCategory().getName()
+ "/" + printServiceAttribute.getName() + " = "
+ printServiceAttribute.toString() + "\n");
}
System.out.println(s.toString());
}
});
// add file (blank.pdf is a blank page exported as PDF from LibreOffice
// Writer)
FileInputStream inputStream;
try {
inputStream = new FileInputStream("blank.pdf");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
// create a new doc and job
DocAttributeSet docAttributeSet = new HashDocAttributeSet();
docAttributeSet.add(MediaSizeName.ISO_A4);
docAttributeSet.add(Sides.DUPLEX);
Doc doc = new SimpleDoc(inputStream, DocFlavor.INPUT_STREAM.PDF,
docAttributeSet);
DocPrintJob job = service.createPrintJob();
// listen to print job attribute change events
// attribute set is null, means this means to listen on all dynamic
// attributes that the job supports.
job.addPrintJobAttributeListener(new PrintJobAttributeListener() {
@Override
public void attributeUpdate(PrintJobAttributeEvent event) {
PrintJobAttributeSet jobAttributeSet = event.getAttributes();
StringBuilder s = new StringBuilder();
s.append("=== PrintJobAttributeEvent: (" + jobAttributeSet.size() + " attributes)\n");
for (Attribute attribute : jobAttributeSet.toArray()) {
PrintJobAttribute jobAttribute = (PrintJobAttribute) attribute;
s.append(jobAttribute.getCategory().getName() + "/"
+ jobAttribute.getName() + " = "
+ jobAttribute.toString() + "\n");
}
System.out.println(s.toString());
}
}, null);
// listen to print job events
job.addPrintJobListener(new PrintJobListener() {
@Override
public void printJobRequiresAttention(PrintJobEvent pje) {
System.out.println("=== PrintJobEvent: printJobRequiresAttention");
}
@Override
public void printJobNoMoreEvents(PrintJobEvent pje) {
// TODO Auto-generated method stub
System.out.println("=== PrintJobEvent: printJobNoMoreEvents");
System.out.println(pje.getPrintEventType());
System.out.println(pje.toString());
}
@Override
public void printJobFailed(PrintJobEvent pje) {
// TODO Auto-generated method stub
System.out.println("=== PrintJobEvent: printJobFailed");
System.out.println(pje.getPrintEventType());
System.out.println(pje.toString());
}
@Override
public void printJobCompleted(PrintJobEvent pje) {
// TODO Auto-generated method stub
System.out.println("=== PrintJobEvent: printJobCompleted");
System.out.println(pje.getPrintEventType());
System.out.println(pje.toString());
}
@Override
public void printJobCanceled(PrintJobEvent pje) {
// TODO Auto-generated method stub
System.out.println("=== PrintJobEvent: printJobCanceled");
System.out.println(pje.getPrintEventType());
System.out.println(pje.toString());
}
@Override
public void printDataTransferCompleted(PrintJobEvent pje) {
System.out.println("=== PrintJobEvent: printDataTransferCompleted");
System.out.println(pje.getPrintEventType());
System.out.println(pje.toString());
}
});
// print
try {
job.print(doc, requestAttributeSet);
} catch (PrintException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return;
}
// try polling
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
System.out.println("=== Polling: I'm alive and it's " + new Date());
System.out.println("Job attributes");
for (Attribute attribute : job.getAttributes().toArray()) {
System.out.println((attribute.getCategory().getName() + "/"
+ attribute.getName() + " = " + attribute.toString()));
}
System.out.println("Service attributes");
for (Attribute attribute : service.getAttributes().toArray()) {
System.out.println((attribute.getCategory().getName() + "/"
+ attribute.getName() + " = " + attribute.toString()));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
最终这一切都取决于打印机固件。IPP 将属性 job-impressions-completed 指定为 可选。这意味着,如果打印机无法判断已打印哪一页,您将无法读取它 - 无论您的编程是否正确。
制造商通常声称支持 IPP,但没有很好地记录他们可能已实施(或未实施)的可选部分。
ipptool
在进行任何编程之前,我建议使用CUPS 中的可用信息来读取所有可用的作业属性:
#!/usr/bin/env ipptool -tv -d job=482 ipp://192.168.2.113/ipp
{
OPERATION Get-Job-Attributes
GROUP operation-attributes-tag
ATTR charset attributes-charset utf-8
ATTR language attributes-natural-language en
ATTR uri printer-uri $uri
ATTR integer job-id $job
}
Run Code Online (Sandbox Code Playgroud)
job-state
是强制属性,应在一段时间后达到最终状态:completed
、aborted
或canceled
。如果您可以在其他地方获取作业页面的数量,这可能就足够了。
实现提示:IppJob提供方法waitForTermination()