无法在JavaFX热敏打印机上打印文本的全部内容

无法在JavaFX热敏打印机上打印文本的全部内容,java,javafx,javafx-11,Java,Javafx,Javafx 11,我使用textArea作为节点,在80mm热敏打印机上打印一串文本。但是打印出来后,文本的高度被切断了。下面是我得到的输出图像: 但我正在打印收据上显示的示例文本: TextArea textArea = new TextArea(); textArea.appendText("*********************************************" + "\n"); textArea.appendText(&

我使用textArea作为节点,在80mm热敏打印机上打印一串文本。但是打印出来后,文本的高度被切断了。下面是我得到的输出图像:

但我正在打印收据上显示的示例文本:

    TextArea textArea = new TextArea();
    textArea.appendText("*********************************************" + "\n");
    textArea.appendText("             " + companyName + "            \n");
    textArea.appendText("*********************************************" + "\n");
    textArea.appendText("Location: " + companyLocation + "\n");
    textArea.appendText("Contact Us: " + phoneNumber + "\n");
    textArea.appendText("TIN: " + tinNumber + "\n");
    textArea.appendText("Receipt No: " + transactionGeneratedId + "\n");
    textArea.appendText("Date: " + getCurrentTimeStamp() + "\n");
    textArea.appendText("---------------------------------------------" + "\n");
这是创建文本字符串并附加到文本区域的方法:

//*** issue print command
public void printReceipt(){
    // print(getPrintableText());
    printNode(getPrintableText());

}

//*** prepare text for printing
public Node getPrintableText(){

    TextArea textArea = new TextArea();

    textArea.appendText("*********************************************" + "\n");
    textArea.appendText("             " + companyName + "            \n");
    textArea.appendText("*********************************************" + "\n");

    textArea.appendText("Location: " + companyLocation + "\n");
    textArea.appendText("Contact Us: " + phoneNumber + "\n");
    textArea.appendText("TIN: " + tinNumber + "\n");
    textArea.appendText("Receipt No: " + transactionGeneratedId + "\n");
    textArea.appendText("Date: " + getCurrentTimeStamp() + "\n");
    textArea.appendText("---------------------------------------------" + "\n");

    // get each product and add to printing list
    for(Products product: productList){

        if(product.getProduct_name().length() > 10){
            // getting the first 11 characters of the product name
            String name = product.getProduct_name().substring(0, 9)+ "...";
            textArea.appendText(product.getQuantity_purchased() + " x " + name +  "          " + currencySymbol + product.getUnit_selling_price() + "\n");

        }else{
            textArea.appendText(product.getQuantity_purchased() + " x " + product.getProduct_name()
                    +  "            " + currencySymbol + product.getUnit_selling_price() + "\n");
        }

        textArea.appendText("Subtotal                 "  + currencySymbol + (Double.parseDouble(product.getQuantity_purchased()) *
                Double.parseDouble(product.getUnit_selling_price())) + "\n");
        textArea.appendText("---------------------------------------------" + "\n");

    }

    // get overall total
    textArea.appendText("Overall Total             " + currencySymbol + totalPrice + "\n");

    String cashReceived = fieldCashReceived.getText().trim();

    // get total amount
    textArea.appendText("Amount Paid               " + currencySymbol + cashReceived + "\n");

    double balance = totalPrice - Double.parseDouble(cashReceived);

    textArea.appendText("_____________________________________________" + "\n");

    // get balance
    textArea.appendText("Balance                   " + currencySymbol + balance + "\n");

    textArea.appendText("*********************************************" + "\n");

    textArea.appendText("Thank you so much!" + "\n");

    textArea.appendText("*********************************************");

    return textArea;
}
这是打印方法:

    public void printNode(Node node) {

      Printer printer = Printer.getDefaultPrinter();

      PrinterJob printerJob = PrinterJob.createPrinterJob(printer);

      Paper paper = PrintHelper.createPaper("Roll80", 80, 590, Units.MM);

      PageLayout pageLayout = printerJob.getPrinter().createPageLayout(paper, PageOrientation.PORTRAIT, 0, 0, 0, 0);

      double height = node.getLayoutBounds().getHeight();

      System.out.println("Height: " + height);

      double scale = 0.791;

      node.getTransforms().add(new Scale(scale, scale));

      if (printerJob != null) {
          boolean success = printerJob.printPage(pageLayout, node);
          if (success) {
             printerJob.endJob();
             System.exit(0);
          }
      }

}

我做得不对的是什么?

我最终用一个类似于@VGR的解决方案解决了这个问题。我希望这可能会帮助那些陷入同样问题的人。我只是使用StringBuilder类构造字符串,并将其设置为标签,最后将标签作为节点返回

请在此处查看:

//*** prepare text for printing
 public Node getPrintableText(){

   Label text = new Label();

   StringBuilder stringBuilder = new StringBuilder();

   stringBuilder.append("*********************************************" + "\n");
   stringBuilder.append("             " + companyName + "            \n");
   stringBuilder.append("*********************************************" + "\n");

   stringBuilder.append("Location: " + companyLocation + "\n");
   stringBuilder.append("Contact Us: " + phoneNumber + "\n");
   stringBuilder.append("TIN: " + tinNumber + "\n");
   stringBuilder.append("Receipt No: " + transactionGeneratedId + "\n");
   stringBuilder.append("Date: " + getCurrentTimeStamp() + "\n");
   stringBuilder.append("---------------------------------------------" + "\n");

 // get each product and add to printing list
 for(Products product: productList){

    if(product.getProduct_name().length() > 10){
        // getting the first 11 characters of the product name
        String name = product.getProduct_name().substring(0, 9)+ "...";
        stringBuilder.append(product.getQuantity_purchased() + " x " + name +  "          " + currencySymbol + product.getUnit_selling_price() + "\n");

    }else{
        stringBuilder.append(product.getQuantity_purchased() + " x " + product.getProduct_name()
                +  "            " + currencySymbol + product.getUnit_selling_price() + "\n");
    }

    stringBuilder.append("Subtotal                 "  + currencySymbol + (Double.parseDouble(product.getQuantity_purchased()) *
            Double.parseDouble(product.getUnit_selling_price())) + "\n");
    stringBuilder.append("---------------------------------------------" + "\n");

 }

   // get overall total
   stringBuilder.append("Overall Total             " + currencySymbol + totalPrice + "\n");

   String cashReceived = fieldCashReceived.getText().trim();

   // get total amount
   stringBuilder.append("Amount Paid               " + currencySymbol + cashReceived + "\n");

   double balance = totalPrice - Double.parseDouble(cashReceived);

   stringBuilder.append("_____________________________________________" + "\n");

   // get balance
   stringBuilder.append("Balance                   " + currencySymbol + balance + "\n");

   stringBuilder.append("*********************************************" + "\n");

   stringBuilder.append("Thank you so much!" + "\n");

   stringBuilder.append("*******************************************");

   text.setText(stringBuilder.toString());

   return text;
}

TextArea包含一个滚动条,不能一次显示所有文本。考虑创建一个并将其添加到它中。