2016-04-04 56 views
3

我的JavaFX程序准备并打印出一组VBox。JavaFX(OpenJFX)不让我打印

这是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 

仍然一无所获。

回答

0

我发现一些所谓的CUPS4J,这让我绕过了一份关于Java的试图访问一个64位的Ubuntu CUPS的问题。它使用Byte数组打印出来,幸运的是,JavaFX可以快照所选节点。

这有点模糊,但它足够好。 注:我不是专家,我不知道为什么这是必要的。但是这样做可以让我使用CUPS4J而没有错误,所以它一定是正确的。

  • 所以,首先,下载[ECLIPSE PROJECT]cups4j, 因为有必须被固定的依赖关系。将其导入到您的项目中。

编辑:为什么需要以下的原因是,不知何故,我的包没有与org.slf4j来。如果你的课程路径说明你有,请跳过这些步骤。

  • 接下来,每个班上有,(CASE 敏感)的Logger所有实例应与Log更换,修复导入(Ctrl + Shift + O)。这将建议Log的一个版本,并且LogFactory将被自动检测到。我的导入路径显示为org.apache.commons.logging.*

  • 最后,在Libraries下的构建路径中删除库依赖关系org.slf4j

(我敢肯定,使用运行的JAR是好的,但是这是我做的事,因为使用运行的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论坛上说有具有多页的问题,错误报告,但我还没有遇到。

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