Results 1 to 6 of 6
  1. #1
    ammameiya's Avatar
    ammameiya is offline Member
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    0

    Unhappy How to print a HTML file in browser look using JAVA

    Hello guys, :)

    Does anyone know how to print HTML output/file into browser look?
    I'm using DocPrintJob and the DocFlavor set to DocFlavor.INPUT_STREAM.AUTOSENSE.

    posted below is the code :
    public class BasicPrint {
    public static void main(String[] args) {
    try {
    // Open the image file
    String testData = "C:/new_page_1.html";
    InputStream is = new BufferedInputStream(new FileInputStream(testData));
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

    // Find the default service
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();
    System.out.println(service);

    // Create the print job
    DocPrintJob job = service.createPrintJob();
    Doc doc= new SimpleDoc(is, flavor, null);

    // Monitor print job events; for the implementation of PrintJobWatcher,
    // see e702 Determining When a Print Job Has Finished
    PrintJobWatcher pjDone = new PrintJobWatcher(job);

    // Print it
    job.print(doc, null);

    // Wait for the print job to be done
    pjDone.waitForDone();

    // It is now safe to close the input stream
    is.close();
    } catch (PrintException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    static class PrintJobWatcher {
    // true iff it is safe to close the print job's input stream
    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) {
    }
    }
    }

    }

    the printed ouput for this code will be look like this
    <html>
    <body>
    <div style="page-break-after:'always';
    background-color:#EEEEEE;
    width:400;
    height:70">
    testPrint</div>

    ABCDEFGHIJK<p>
    &nbsp;</p>
    </body>
    </html>

    however, the output that i want is the HTML in browser look not HTML code itself.

    i've tried to change the DocFlavor into any TEXT_HTML type but it gives error:
    sun.print.PrintJobFlavorException: invalid flavor
    if you guys has any idea or solution, can you share with me...:o

    Thanks in advanced.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    femme fatale is offline Member
    Join Date
    Jun 2008
    Posts
    1
    Rep Power
    0

    Default how to print html using browser.

    Hello
    I dont know if you already solved the problem but I have come face to face with
    somewhat same problem and I solved it like this:

    I created a temporary file and I set the content of it with the html content that I want to print. then I got the URL of the file and set it to my browsers url. then
    I used a small javascript code to print my browser's context. here is the source code that I used.

    Browser browser = new Browser(c,SWT.NONE);

    File temp = null;
    try {
    // Create temp file.
    temp = File.createTempFile("pattern", ".suffix");

    // Delete temp file when program exits.
    temp.deleteOnExit();

    // Write to temp file
    BufferedWriter out = new BufferedWriter(new FileWriter(temp));
    out.write(toPrint);
    out.close();
    } catch (IOException e) {
    }



    //get the uri and url and set the browsers URL

    try {
    browser.setUrl(temp.toURI().toURL().toString());
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    browser.execute("javascript:print()");
    }


    if you have questions dont hesitate to ask...
    deniz gul

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Hi femme,

    It's better to use code tags when you post a code again into the forum. It's much easy to read your code for others.

    Have a nice time on our community. :)

  5. #5
    WhiteLily is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Default

    Hi Femme,

    I am new to javascript and eclipse RCP. Could you please post your java script as well ? It would be of great help to me.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. How to print text file in java(dotmatrix printer)
    By yoganeethi in forum Advanced Java
    Replies: 4
    Last Post: 12-01-2010, 01:45 PM
  2. Include Java file in HTML Page
    By kathyc in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 03:51 AM
  3. Replies: 0
    Last Post: 12-06-2007, 06:10 PM
  4. Print a picture file
    By oli001 in forum New To Java
    Replies: 0
    Last Post: 11-26-2007, 01:40 PM
  5. how to call a JAR FILE from HTML
    By leonard in forum Java Applets
    Replies: 1
    Last Post: 08-05-2007, 06:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •