在 java 中打印发票的最佳方式是什么
what is the best way to print invoice in java
在我的桌面应用程序(POS 系统)中。我使用 IText api 生成发票和打印,但我的打印机热敏发票打印机不支持 .pdf 文件。仅支持文本文件和 .docx 文件。我使用简单的文本文件打印机在长的垂直单字行中打印整张发票并且不自动切页。我使用了 .docx 文件,效果很好,我按设计打印了。但我的程序首先在 ms word 中打开文档然后给我打印。我的代码是:
try
{
FileOutputStream output = new FileOutputStream(FILE);
XWPFDocument doc = new XWPFDocument();
CTBody body = doc.getDocument().getBody();
if(!body.isSetSectPr()){
body.addNewSectPr();
}
CTSectPr section = body.getSectPr();
if(!section.isSetPgSz()){
section.addNewPgSz();
}
CTPageSz pageSize = section.getPgSz();
pageSize.setOrient(STPageOrientation.PORTRAIT);
int value = 4000+(gui.model.getRowCount()*1000);
pageSize.setW(BigInteger.valueOf(4050));
pageSize.setH(BigInteger.valueOf(value));
CTPageMar pageMar = section.addNewPgMar();
pageMar.setLeft(BigInteger.valueOf(400L));
pageMar.setTop(BigInteger.valueOf(0L));
pageMar.setRight(BigInteger.valueOf(0L));
pageMar.setBottom(BigInteger.valueOf(0L));
XWPFParagraph para = doc.createParagraph();
para.setAlignment(ParagraphAlignment.LEFT);
XWPFRun run = para.createRun();
para.setWordWrap(true);
run.setBold(true);
run.setFontSize(10);
run.setText(" "+address.shopName);
run.addBreak();
run.setText(" "+address.phoneNo);
run.addBreak();
run.setText(" "+address.description);
run.addBreak();
para = doc.createParagraph();
para.setAlignment(ParagraphAlignment.LEFT);
run = para.createRun();
para.setWordWrap(true);
run.setFontSize(10);
run.setText("Invoice No."+invoiceno);
run.addBreak();
run.setText("Type: "+table);
run.addBreak();
run.setText("Customer Name: "+name+" "+tempObj);
run.addBreak();
run.setText("--------------------------------------------------------");
run.addBreak();
run.setText("Product Qty Price Total");
run.addBreak();
run.setText("--------------------------------------------------------");
run.addBreak();
String temp = null;
for(int i = 0 ; i < gui.table.getRowCount(); i++){
temp = gui.table.getValueAt(i, 1).toString();
String quanstr = gui.table.getValueAt(i, 2)+"";
String unitPricestr = gui.table.getValueAt(i, 3)+"";
String totalstr =gui.table.getValueAt(i, 4)+"";
run.setText(temp);run.addBreak();
run.setText(" "+quanstr+" "+unitPricestr+" "+totalstr);
run.addBreak();
}
double subTotal = tableTotalCounter();
run.setText("--------------------------------------------------------");run.addBreak();
run.setText("Discount: "+dis+"%");run.addBreak();
run.setText("Sub total: "+(subTotal - (subTotal*dis/100)));run.addBreak();
run.setText("Cash: "+cash);run.addBreak();
run.setText("Balance: "+(cash-(subTotal - (subTotal*dis/100))));
run.addBreak();
doc.write(output);
output.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("Exception");
e1.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Exception");
}
if(confirmation("Print invoice?","Confirmation")==0){
Desktop desktop = Desktop.getDesktop();
try {
desktop.print(new File(FILE));
} catch (IOException e) {
e.printStackTrace();
}
}
请告诉我如何在不打开该文件的情况下进行打印。还有其他打印发票的方法。
将您的发票格式化为字符串并传递到我在下面粘贴的代码。在执行此代码之前打印测试页 (windows) (Linux) 以确保您的打印机配置正确。
public class GenerateInvoice {
public static void printInvoice(String invoice) {
try {
PrintService mPrinter = null;
Boolean bFoundPrinter = false;
PrintService[] printServices = PrinterJob.lookupPrintServices();
for (PrintService printService : printServices) {
String sPrinterName = printService.getName();
if (sPrinterName.equals("Black Cobra")) {
mPrinter = printService;
bFoundPrinter = true;
}
}
String testData = invoice+"\f";
InputStream is = new ByteArrayInputStream(testData.getBytes());
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE ;
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
System.out.println(service);
DocPrintJob job = service.createPrintJob();
Doc doc= new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, null);
pjDone.waitForDone();
is.close();
} catch (PrintException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static class PrintJobWatcher {
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
// Add a listener to the print job
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}
}
在我的桌面应用程序(POS 系统)中。我使用 IText api 生成发票和打印,但我的打印机热敏发票打印机不支持 .pdf 文件。仅支持文本文件和 .docx 文件。我使用简单的文本文件打印机在长的垂直单字行中打印整张发票并且不自动切页。我使用了 .docx 文件,效果很好,我按设计打印了。但我的程序首先在 ms word 中打开文档然后给我打印。我的代码是:
try
{
FileOutputStream output = new FileOutputStream(FILE);
XWPFDocument doc = new XWPFDocument();
CTBody body = doc.getDocument().getBody();
if(!body.isSetSectPr()){
body.addNewSectPr();
}
CTSectPr section = body.getSectPr();
if(!section.isSetPgSz()){
section.addNewPgSz();
}
CTPageSz pageSize = section.getPgSz();
pageSize.setOrient(STPageOrientation.PORTRAIT);
int value = 4000+(gui.model.getRowCount()*1000);
pageSize.setW(BigInteger.valueOf(4050));
pageSize.setH(BigInteger.valueOf(value));
CTPageMar pageMar = section.addNewPgMar();
pageMar.setLeft(BigInteger.valueOf(400L));
pageMar.setTop(BigInteger.valueOf(0L));
pageMar.setRight(BigInteger.valueOf(0L));
pageMar.setBottom(BigInteger.valueOf(0L));
XWPFParagraph para = doc.createParagraph();
para.setAlignment(ParagraphAlignment.LEFT);
XWPFRun run = para.createRun();
para.setWordWrap(true);
run.setBold(true);
run.setFontSize(10);
run.setText(" "+address.shopName);
run.addBreak();
run.setText(" "+address.phoneNo);
run.addBreak();
run.setText(" "+address.description);
run.addBreak();
para = doc.createParagraph();
para.setAlignment(ParagraphAlignment.LEFT);
run = para.createRun();
para.setWordWrap(true);
run.setFontSize(10);
run.setText("Invoice No."+invoiceno);
run.addBreak();
run.setText("Type: "+table);
run.addBreak();
run.setText("Customer Name: "+name+" "+tempObj);
run.addBreak();
run.setText("--------------------------------------------------------");
run.addBreak();
run.setText("Product Qty Price Total");
run.addBreak();
run.setText("--------------------------------------------------------");
run.addBreak();
String temp = null;
for(int i = 0 ; i < gui.table.getRowCount(); i++){
temp = gui.table.getValueAt(i, 1).toString();
String quanstr = gui.table.getValueAt(i, 2)+"";
String unitPricestr = gui.table.getValueAt(i, 3)+"";
String totalstr =gui.table.getValueAt(i, 4)+"";
run.setText(temp);run.addBreak();
run.setText(" "+quanstr+" "+unitPricestr+" "+totalstr);
run.addBreak();
}
double subTotal = tableTotalCounter();
run.setText("--------------------------------------------------------");run.addBreak();
run.setText("Discount: "+dis+"%");run.addBreak();
run.setText("Sub total: "+(subTotal - (subTotal*dis/100)));run.addBreak();
run.setText("Cash: "+cash);run.addBreak();
run.setText("Balance: "+(cash-(subTotal - (subTotal*dis/100))));
run.addBreak();
doc.write(output);
output.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("Exception");
e1.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Exception");
}
if(confirmation("Print invoice?","Confirmation")==0){
Desktop desktop = Desktop.getDesktop();
try {
desktop.print(new File(FILE));
} catch (IOException e) {
e.printStackTrace();
}
}
请告诉我如何在不打开该文件的情况下进行打印。还有其他打印发票的方法。
将您的发票格式化为字符串并传递到我在下面粘贴的代码。在执行此代码之前打印测试页 (windows) (Linux) 以确保您的打印机配置正确。
public class GenerateInvoice {
public static void printInvoice(String invoice) {
try {
PrintService mPrinter = null;
Boolean bFoundPrinter = false;
PrintService[] printServices = PrinterJob.lookupPrintServices();
for (PrintService printService : printServices) {
String sPrinterName = printService.getName();
if (sPrinterName.equals("Black Cobra")) {
mPrinter = printService;
bFoundPrinter = true;
}
}
String testData = invoice+"\f";
InputStream is = new ByteArrayInputStream(testData.getBytes());
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE ;
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
System.out.println(service);
DocPrintJob job = service.createPrintJob();
Doc doc= new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, null);
pjDone.waitForDone();
is.close();
} catch (PrintException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static class PrintJobWatcher {
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
// Add a listener to the print job
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}
}