I have a problem with printing of several html documents using java. I need application which show ONE print dialog for all printable files (A file count could be big). First i tried to do this using standart java method:
if (Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.PRINT))
{
try {
File html1 = new File("c://file1.html");
File html2 = new File("c://file2.html");
desktop.print(html1);
desktop.print(html2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
But i saw a one dialog for each printable file, it doesn’t suit me. Then i tried to use a Java Printing API, but it turned out that my printer doesn’t support DocFlavor of html files, my list of supported DocFlavor look like this:
image/gif; class="[B"
image/gif; class="java.io.InputStream"
image/gif; class="java.net.URL"
image/jpeg; class="[B"
image/jpeg; class="java.io.InputStream"
image/jpeg; class="java.net.URL"
image/png; class="[B"
image/png; class="java.io.InputStream"
image/png; class="java.net.URL"
application/x-java-jvm-local-objectref; class="java.awt.print.Pageable"
application/x-java-jvm-local-objectref; class="java.awt.print.Printable"
application/octet-stream; class="[B"
application/octet-stream; class="java.net.URL"
application/octet-stream; class="java.io.InputStream"
Then i tried to print html file as image (png, which I drew in Paint:)), my code:
PrintRequestAttributeSet pras =
new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
PrintRequestAttributeSet aset =
new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(1));
aset.add(Sides.ONE_SIDED);
aset.add(Finishings.STAPLE);
PrintService printService[] =
PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =
PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
try {
FileInputStream fis = new FileInputStream("c://test//test.png");
DocAttributeSet das = new HashDocAttributeSet();
Doc doc1 = new SimpleDoc(fis, flavor, das);
FileInputStream fis2 = new FileInputStream("c://test//test2.png");
DocAttributeSet das2 = new HashDocAttributeSet();
Doc doc2 = new SimpleDoc(fis2, flavor, das2);
DocPrintJob job1 = service.createPrintJob();
DocPrintJob job2 = service.createPrintJob();
try {
job1.print(doc1, pras);
job2.print(doc2, pras);
} catch (PrintException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
It works perfectly, but convertation from html to image isn’t simple problem. I tried to use swing components, implements Printable interface and use Cobra library, but it demands the display of document on the form, it’s not nessesary for me, because i need a printing in “silent” mode, without opening a docs.
Any ideas?
In the end, I choose this way:
Convert html files to pdf.
Print pdf files using PDFBox in silent print mode.