Skip to content

Instantly share code, notes, and snippets.

@dagvadorj
Created March 28, 2014 07:52
Show Gist options
  • Save dagvadorj/9827487 to your computer and use it in GitHub Desktop.
Save dagvadorj/9827487 to your computer and use it in GitHub Desktop.
Хэвлэх
/**
* OrderUtils.java
*/
public static JEditorPane getOrderPrintable(
ReceiptConfiguration configuration,
Order order) {
StringBuilder printing = new StringBuilder();
printing.append(configuration.getReceiptHeader());
printing.append("\n\n");
printing.append("Захиалга #: ").
append((configuration.getOrderIdOption() == 1) ?
order.getDayIndex() : order.getId()).append("\n");
printing.append("Огноо: ").
append(FormatUtils.toString(new Date(),
configuration.getDateFormat())).append("\n");
if (configuration.getIncludeDescription())
printing.append("Тайлбар: ").
append((order.getName() != null) ? order.getName() : "").
append("\n");
if (configuration.getIncludeCustomer())
printing.append("Харилцагч: ").
append((order.getCustomer() != null) ?
order.getCustomer() : "").
append("\n");
printing.append("\n\n");
printing.append(configuration.getReceiptFooter());
printing.append("\n");
JEditorPane editorPane = new JEditorPane("text/plain; charset=UTF-8",
printing.toString());
return editorPane;
}
/**
* Test.java
*/
PrinterJob job = null;
job = PrinterJob.getPrinterJob();
JEditorPane editorPane = null;
editorPane = OrderUtils.getOrderPrintable(configuration,
order);
PageFormat pageFormat = job.defaultPage();
Paper paper = pageFormat.getPaper();
paper.setImageableArea(0, 0,
pageFormat.getWidth(), pageFormat.getHeight());
pageFormat.setPaper(paper);
job.validatePage(pageFormat);
Printable printable = editorPane.
getPrintable(new MessageFormat(""), new MessageFormat(""));
job.setPrintable(printable, pageFormat);
try {
job.print();
} catch (PrinterException e) {
JOptionPane.showMessageDialog(null, "Хэвлэх үед алдаа гарлаа!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment