Skip to content

Commit c1e7fc5

Browse files
committed
Send document to printer
1 parent 5cb2fd6 commit c1e7fc5

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed

src/main/java/demo/AppUI.java

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
import com.vaadin.ui.*;
88
import com.vaadin.ui.themes.ValoTheme;
99

10+
import javax.print.*;
11+
import javax.print.attribute.HashPrintRequestAttributeSet;
12+
import javax.print.attribute.PrintRequestAttributeSet;
13+
import javax.swing.*;
14+
import java.io.StringReader;
15+
16+
import static com.vaadin.ui.Notification.Type.HUMANIZED_MESSAGE;
17+
import static com.vaadin.ui.Notification.Type.WARNING_MESSAGE;
18+
1019
@Push(transport = Transport.WEBSOCKET)
1120
@Theme(ValoTheme.THEME_NAME)
1221
public class AppUI extends UI {
@@ -15,12 +24,68 @@ protected void init(VaadinRequest request) {
1524
TextField nameField = new TextField();
1625
nameField.setCaption("Your name");
1726

18-
Button button = new Button("Hello", event ->
19-
new Notification("Hello " + nameField.getValue()).show(getPage())
20-
);
27+
Button button = new Button("Print Hello", event -> {
28+
Runtime runtime = Runtime.getRuntime();
29+
30+
printHelloDocument(String.format(
31+
"Hello %s!\n" +
32+
"Your PC is so powerful:\n" +
33+
"%s processors\n" +
34+
"%s free memory\n" +
35+
"%s max memory",
36+
nameField.getValue(),
37+
runtime.availableProcessors(),
38+
runtime.freeMemory(),
39+
runtime.maxMemory()));
40+
});
2141

2242
VerticalLayout content = new VerticalLayout();
2343
content.addComponents(nameField, button);
2444
setContent(content);
2545
}
46+
47+
private void printHelloDocument(String value) {
48+
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
49+
50+
DocFlavor flavor = DocFlavor.READER.TEXT_PLAIN;
51+
Doc doc = new SimpleDoc(new StringReader(value), flavor, null);
52+
53+
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, aset);
54+
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
55+
56+
if (services.length == 0) {
57+
if (defaultService == null) {
58+
new Notification("No printer found", WARNING_MESSAGE).show(getPage());
59+
} else {
60+
DocPrintJob job = defaultService.createPrintJob();
61+
printDocument(doc, aset, job);
62+
}
63+
} else {
64+
SwingUtilities.invokeLater(() -> {
65+
PrintService service = ServiceUI.printDialog(null, 200, 200,
66+
services, defaultService, flavor, aset);
67+
if (service != null) {
68+
DocPrintJob job = service.createPrintJob();
69+
printDocument(doc, aset, job);
70+
}
71+
});
72+
}
73+
}
74+
75+
private void printDocument(Doc doc, PrintRequestAttributeSet aset, DocPrintJob job) {
76+
try {
77+
job.print(doc, aset);
78+
79+
getUI().access(() ->
80+
new Notification("See the result!", HUMANIZED_MESSAGE)
81+
.show(getPage())
82+
);
83+
} catch (PrintException e) {
84+
// may be called from Swing thread
85+
getUI().access(() -> {
86+
new Notification("Unable to print file, please check settings",
87+
WARNING_MESSAGE).show(getPage());
88+
});
89+
}
90+
}
2691
}

0 commit comments

Comments
 (0)