JavaFX(OpenJFX)不允许我打印

dgood1

我的JavaFX程序准备并打印出一组VBox。

这是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");
    }
}

打印机已安装并设置为默认打印机,我的程序检测到它。但是当我打印时,没有错误弹出,并且打印机没有接收到任何作业。

我确定我的程序以前可以运行(A Lubuntu 15.10,32位)。但是现在,我将其转移到另一台计算机上。Lubuntu 15.10,64位。我已经安装了openjfx和openjdk版本“ 1.8.0_66-internal”。

我该怎么做才能找出为什么不打印?


尝试进行较小的打印作业,但效果相同。

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: Done

AstinePrinter是我的打印机的名称。


编辑:使用

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

依然没有。

dgood1

我找到了一个名为CUPS4J的东西,它使我可以绕过Java试图在64位Ubuntu中访问CUPS的问题。它使用Byte数组打印出来,幸运的是,JavaFX可以对选定的节点进行快照。

有点模糊,但是足够好。注意:我不是专家,也不知道为什么需要这样做。但是这样做使我可以毫无错误地使用CUPS4J,因此它一定是正确的。

  • 因此,首先,下载[Eclipse项目]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论坛上看到了错误报告,说多个页面有问题,但是我还没有遇到。

如果有人有更好的答案,请随时添加。但是到目前为止,这对我有用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章