我的JavaFX程序准备并打印出一组VBoxes。
我是ModPrintCycle。它是提供打印选项的窗口。
public PrintCycle data;
//PrintCycle is a HashMap of VBoxes containing all the details
PrinterJob pj;
ChoiceBox<String> cbxPrinters = new ChoiceBox<String>();
ArrayList<Printer> arrPrinters = new ArrayList<Printer>();
//util.say just pops out a messagebox attached to ModPrintCycle.
public void printAll(ArrayList<String> pageList){
if(cbxPrinters.getSelectionModel().getSelectedIndex() >=0){
if (data.tables.size() > 0){
Printer curP = Printer.getDefaultPrinter();
if(arrPrinters.size() > 0 ){
curP = arrPrinters.get(cbxPrinters.getSelectionModel().getSelectedIndex());
}
try{
pj = PrinterJob.createPrinterJob(curP);
PageLayout pp = curP.createPageLayout(Paper.LEGAL, PageOrientation.PORTRAIT, MarginType.DEFAULT);
PageLayout pl = curP.createPageLayout(Paper.LEGAL, PageOrientation.LANDSCAPE, MarginType.DEFAULT);
for(String p : pageList){
Printable pt = data.tables.get(p);
pt.scaleToFit();
if(pt.isLandscape()){
pj.printPage(pl,pt);
}
else{
pj.printPage(pp,pt);
}
}
pj.endJob();
}catch(Exception e){
util.say(ModPrintCycle.this, "Error on Print");
}
}else{
util.say(ModPrintCycle.this, "Nothing to print");
}
}
else{
util.say(ModPrintCycle.this, "No Printer Selected");
}
}打印机被安装并设置为默认值,而我的程序会检测到它。但是,当我打印时,没有出现错误,打印机也没有收到任何作业。
我确信我的程序以前是有效的(一个Lubuntu 15.10,32位)。但现在,我把它转到了另一台电脑上。Lubuntu 15.10,64位。我安装了openjfx和openjdk版本“1.8.0_66-内部”。
我能做些什么来找出为什么它不是印刷的?
试着做一个小一点的印刷工作,但效果相同。
Button testPrint = new Button("Test Print");
testPrint.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent arg0) {
try{
Printer p = Printer.getDefaultPrinter();
PrinterJob pj = PrinterJob.createPrinterJob(p);
//util.say(ModShortcuts.this, "Print: " + pj.getJobStatus());
Boolean k = pj.printPage(p.createPageLayout(Paper.LEGAL,PageOrientation.PORTRAIT,MarginType.DEFAULT), new Text("Hey"));
//util.password(); //reused for a showAndWait() dialog
//util.say(ModShortcuts.this, "Print: " + pj.getJobStatus());
//util.say(ModShortcuts.this, "attempted Print using: " + pj.getPrinter().getName());
if(k){
//util.say(ModShortcuts.this, "Print: " + pj.getJobStatus());
pj.endJob();
//util.say(ModShortcuts.this, "Print: " + pj.getJobStatus());
}
}catch(Exception e){
e.printStackTrace();
}
}
});
vbox.getChildren().add(testPrint);没有注释,输出是
Print: Not Printing
Print: Printing
attempted Print using: AstinePrinter
Print: Printing
Print: DoneAstinePrinter是我的打印机的名字。
编辑:使用
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer 我安装了Oracle Java 8,但仍然存在相同的问题。
编辑:也是Oracle Java 7。
编辑:
试图禁用防火墙,以防出现端口问题。
sudo ufw disable还是一无所获。
发布于 2016-04-21 09:20:01
我发现了一个叫做CUPS4J的东西,它允许我绕过它遇到的问题,它试图在64位Ubuntu中访问CUPS。它使用Byte数组打印出来,幸运的是,JavaFX有一种方法来快照所选的节点。
有点模糊,但已经够好了。注意:我不是专家,我不知道为什么需要这样做。但是这样做可以让我在没有错误的情况下使用CUPS4J,所以它一定是正确的。
cups4j,因为有些依赖项必须修复。将其导入您的项目中。编辑:需要以下内容的原因是,不知何故,我的包没有附带org.slf4j。如果您的类路径说您有它,请跳过这些步骤。
Logger (cAsE sEnSiTiVe)的所有实例替换为Log,并修复导入(Ctrl+Shift+O)。这将提示Log的一个版本,并将自动检测到LogFactory。我的导入路径是org.apache.commons.logging.*org.slf4j的库依赖项。(我确信使用Runnable Jar是可以的,但这就是我所做的,因为使用Runnable Jar给了我错误)
这是我为打印功能所做的简化。
private void print(Region node){
//Make the image with the proper sizes
WritableImage wi = new WritableImage(
(int) Math.round(Math.ceil(node.getWidth())),
(int) Math.round(Math.ceil(node.getHeight())));
//shoot the image
wi = node.snapshot(new SnapshotParameters(), wi);
//write the image into a readable context
ByteArrayOutputStream out = new ByteArrayOutputStream();
try{
ImageIO.write(SwingFXUtils.fromFXImage(wi, null), "png", out);
}catch(Exception e){
System.out.println("Error with SnapShot function");
}
//Get your printer
CupsClient cc = new CupsClient();
CupsPrinter cp = cc.getDefaultPrinter();
//print the readable context
cp.print(new PrintJob.Builder(out.toByteArray()).build());
//unlike PrinterJob, you do not need to end it.
}我不确定,但我在CUPS4J论坛上看到bug报告说多个页面有问题,但我还没有遇到这种情况。
如果有人有一个更好的答案,可以随意添加。但到目前为止,这对我有用。
https://stackoverflow.com/questions/36393305
复制相似问题