2

I am trying to print very simple HTML file using JAVA PrintServices API.

This is what I wrote for that -

public class WebTest {
public static void main(String args[]) throws Exception {
    String filename = "C:/tmp/PrintTest.html";
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    if (defaultService != null) {
        DocPrintJob job = defaultService.createPrintJob();
        FileInputStream fis = new FileInputStream(filename);
        DocAttributeSet das = new HashDocAttributeSet();
        Doc doc = new SimpleDoc(fis, flavor, das);
        job.print(doc, pras);
    }
    System.exit(0);
}

However, my output file is not formatted properly - This is what I see in printed output -

<!DOCTYPE html>
<html>
 <body>
  <h2>Hello World.</h2>
 </body>
</html>

I want to see Output as -

Hello World.

I also tried using Microsoft command for this - "C:\\Program Files\\Microsoft Office\\Office14\\msohtmed.exe\" /p C:/tmp/PrintTest.html

However its prompting me PRINT box which I want to get rid off.

My objective is just to get correctly printed output.

Please suggest suitable options.

I already referred other links, but couldn't find exact answer for what I am looking for.

Your help much appreciated.

1 Answer 1

2

The HTML page should be rendered before printing (calculate margins, arrange text on the page, etc...). The simplest way to render and print html page is by using JEditorPane (in the following snippet without any additional formats, attributes and confirmation dialog):

public static void main(String[] args) throws PrintException, IOException, PrinterException {
    JEditorPane editorPane = new JEditorPane();
    editorPane.setEditable(false);
    URL urlToPage = new File("/home/me/Temp/page.html").toURI().toURL();
    editorPane.setPage(urlToPage);  
    editorPane.print(null, null, false, PrintServiceLookup.lookupDefaultPrintService(), null, false);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. Yes it worked for Simple HTML. Is it possible to extend this to slightly complex HTML, say HTML having some tables and cascaded style sheets. When I tried same code for HTML with CSS its giving me error - Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 9 at javax.swing.text.CompositeView.getView(CompositeView.java:143) at javax.swing.text.View.forwardUpdate(View.java:1130) at javax.swing.text.BoxView.forwardUpdate(BoxView.java:223) at javax.swing.text.View.changedUpdate(View.java:767)
For complex HTML you need more sophisticated HTML rendering engine (like CSSBox)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.