- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我必须使用 Java 通过热敏打印机打印收据。我已经做了一切。我的程序从数据库中获取数据并使用特殊字符、制表符和\n 转换为一个字符串。然后将字符串传递给另一个将其转换为图形的方法。
问题是当我单击打印按钮时,会出现白纸。我注意到我的字符串的前 4-5 个字符打印在纸币最末端右上角的账单的最后一行。我的打印机是 Epson TM - T81。
public void printThisBill()
{
DefaultTableModel mod = (DefaultTableModel) jTable1.getModel();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
//get current date time with Date()
Date date = new Date();
Date time = new Date();
String Date = dateFormat.format(date);
String Time = timeFormat.format(time);
String Header =
" ****Super Market**** \n"
+ "Date: "+Date+" Time: "+Time+"\n"
+ "---------------------------------\n"
+ "Name Qty Rate Amt\n"
+ "---------------------------------\n";
String amt =
"\n \n \nTotal Amount = "+ amt() +"\n"
+ "Tax =" + tax() + "\n"
+ "*********************************\n"
+ "Thank you. \n";
String bill = Header;
int i = 0;
do
{
String name = ""+ mod.getValueAt(i, 2);
String qty = ""+ mod.getValueAt(i, 3);
String rate = ""+ mod.getValueAt(i, 4);
String amount = ""+ mod.getValueAt(i, 6);
if(name.length() > 12)
{
name = name.substring(0, 12)+" ";
}
else
{
for(int j= name.length()-12; j<= name.length(); j++);
{
name = name+" ";
}
}
if(qty.length()<=5)
{
for(int j= 0; j<= qty.length()-5; j++);
{
qty = qty+" ";
}
}
rate = rate;
String items =
name+"\t"+qty+"\t"+rate+"\t"+amount+"\n";
bill = bill+ items;
i++;
} while(i <= mod.getRowCount()-1);
bill = bill+amt;
System.out.println(bill);
printCard(bill);
dispose();
}
而打印账单的方法是:
public static void printCard(final String bill )
{
Printable contentToPrint = new Printable(){
@Override
public int print(Graphics graphics, PageFormat pageFormat, int page) throws PrinterException
{
if (page > 0) {
return NO_SUCH_PAGE;
}
pageFormat.setOrientation(PageFormat.LANDSCAPE);
Graphics2D g2d = (Graphics2D)graphics.create();
g2d.setPaint(Color.black);
g2d.setFont(new Font("Arial", Font.BOLD, 10));
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableX());
g2d.drawString(bill, 0, 0);
return PAGE_EXISTS;
}
};
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(contentToPrint);
//You can show a print dialog before printing by job by wrapping the following blocks with a conditional statement if(job.printDialog()){...}
try
{
job.print();
} catch (PrinterException e)
{
System.err.println(e.getMessage());
}
}
问题是什么,我该如何解决?我认为我没有在 drawString() 方法中设置正确的参数。
还是别的什么?任何帮助将不胜感激。!
最佳答案
我发现了这个:
Java Pos Thermal Printer Example
它对我有用。无需外部库。
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.ArrayList;
import java.util.List;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
public class PrinterService implements Printable {
public List<String> getPrinters(){
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
PrintService printServices[] = PrintServiceLookup.lookupPrintServices(
flavor, pras);
List<String> printerList = new ArrayList<String>();
for(PrintService printerService: printServices){
printerList.add( printerService.getName());
}
return printerList;
}
@Override
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/*
* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now we perform our rendering */
g.setFont(new Font("Roman", 0, 8));
g.drawString("Hello world !", 0, 10);
return PAGE_EXISTS;
}
public void printString(String printerName, String text) {
// find the printService of name printerName
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
PrintService printService[] = PrintServiceLookup.lookupPrintServices(
flavor, pras);
PrintService service = findPrintService(printerName, printService);
DocPrintJob job = service.createPrintJob();
try {
byte[] bytes;
// important for umlaut chars
bytes = text.getBytes("CP437");
Doc doc = new SimpleDoc(bytes, flavor, null);
job.print(doc, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void printBytes(String printerName, byte[] bytes) {
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
PrintService printService[] = PrintServiceLookup.lookupPrintServices(
flavor, pras);
PrintService service = findPrintService(printerName, printService);
DocPrintJob job = service.createPrintJob();
try {
Doc doc = new SimpleDoc(bytes, flavor, null);
job.print(doc, null);
} catch (Exception e) {
e.printStackTrace();
}
}
private PrintService findPrintService(String printerName,
PrintService[] services) {
for (PrintService service : services) {
if (service.getName().equalsIgnoreCase(printerName)) {
return service;
}
}
return null;
}
}
还有主类:
public class Main {
public static void main(String[] args) {
PrinterService printerService = new PrinterService();
System.out.println(printerService.getPrinters());
//print some stuff. Change the printer name to your thermal printer name.
printerService.printString("EPSON-TM-T20II", "\n\n testing testing 1 2 3eeeee \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
// cut that paper!
byte[] cutP = new byte[] { 0x1d, 'V', 1 };
printerService.printBytes("EPSON-TM-T20II", cutP);
}
}
关于java - 在java中使用热敏打印机打印收据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17505070/
我一直在尝试学习Grails,但是我遇到了以下问题。我已经创建了四个域类(Receipts,Items,类别和所有者),并且为每个域类(ReceiptsController,ItemsControll
我正在构建一个销售点应用程序,我想打印一张收据。问题是我使用的打印机无法打印纯文本的任何图形,我在 javafx 中只能找到使用 Print API 打印节点或使用像 jasper 这样都包含图形的报
我正在 Swift 3.0 应用程序中实现应用程序内购买,因此我需要获取应用程序收据以根据 iTunes 商店对其进行验证。这是我获得收据的方式: func getReceipt() -> Data?
我想在我的数据库中保存非消耗品的 IAP 收据。我们有一个用于 IAP 恢复的两步下载机制。 使用 StoreKit 机制检索所有交易(包括收据)。 从所有收据(即这些收据背后的产品)列表中选择稍后要
我想验证 iOS 收据。 我想我会向 App Store 验证服务器发送收据(https://sandbox.itunes.apple.com/verifyReceipt 或 https://buy.
我需要打印具有相同产品数量等的销售订单的 POS 收据 在销售订单中,我创建了一个按钮“打印 POS 收据”。使用此按钮,我想触发一种方法,该方法打印出带有销售订单行的收据。 所以我需要找到创建 PO
我们现在有一个正在生产中的应用程序,它会将 IAP 收据发送到我们的服务器,这些收据显然太短,而且我们的服务器没有经过 apple 的验证。 Apple 正确验证的长收据长度为 3192。短收据长度均
我正在构建一个 iOS 应用程序,它提供我们网站已经提供的服务。这是一项基于订阅的服务,订阅后所有功能都会启用。为了让人们在应用程序上注册定期付款,我似乎必须通过 Apple 的应用程序内购买 API
我需要一种方法来捕获笔记本中打印的所有内容。 为什么? 我有一个 PHP 系统,有时我需要打印(php_printer) 一些发票,但这会花费很多纸张。 打印在后台发送,没有任何弹出窗口供用户确认。
我对此有点困惑。所以关注this approach我得到了包含一堆解密字段的 json。其中 original_purchase_date。 我需要的是跟踪在这个新版本免费之前是否购买了应用程序,我认
首先,我说的不是调用https://buy.itunes.apple.com/verifyReceipt/ ,相反,我要问的是如何验证来 self 们的一位用户的 iTuneStore 收据。 我们的
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 5 年前。
所以我要建立一个在线商店,我想向客户发送包含收据的订单确认。我搜索了一下,但我还没有设法创建任何东西。 这是我发现并尝试使用的东西,但没有成功创建任何东西。 所以 ajax 调用会将 cartCon
所以我要建立一个在线商店,我想向客户发送包含收据的订单确认。我搜索了一下,但我还没有设法创建任何东西。 这是我发现并尝试使用的东西,但没有成功创建任何东西。 所以 ajax 调用会将 cartCon
我正在尝试实现自动续订订阅。我的应用程序可以在不同的设备(Android、Web)上使用,因此我需要实现状态轮询技术,以便在 App Store 设法续订过期订阅时确认我的服务器。收据正在我的服务器上
我正在尝试验证来自 C++ 服务器的 iphone 收据(我有使用 base64 加密的收据,我正在根据苹果文档编写一个 json 对象,然后我打开一个到沙箱的套接字并发送一个 POST请求)。 服务
首先,这个问题与 iOS7+ 收据有关,因此所有关于 latest_receipt 和 latest_receipt_info 的问题/答案都不适用(因为它们已被弃用并且正在离开)。似乎关于在 SO
谁能告诉我如何使用 Exchange Web 服务 API 获取电子邮件正文、收据、发件人、抄送信息?我只知道如何获取主题。 ExchangeService service = new Exchang
有关服务器到服务器通知的 Apple 文档没有指定 cancellation_date 字段中的数据格式。我正在尝试为我的通知处理程序设置一些单元测试,但我不确定要将哪些数据放入我的模拟响应中。 我假
我正在尝试使用 Citizen CT-S651 热敏打印机从网络浏览器打印 HTML/CSS 媒体格式的收据。我们已经能够正确格式化收据,但问题是在打印收据后,打印机继续吐纸而不是在内容的末尾切割。
我是一名优秀的程序员,十分优秀!