3

I am writing a service on the local server side which will accept the printer name and other inputs from the UI application and print the html file to the desired network printer. it is not a desktop application. I have a processed html file which I am reading into a string and want its output sent to the desired printer.

1 way I could found is to create an image by reading it into a JEditorPane(though using a swing class is not great approach) and then saving an image which is then sent to the printer. But it fails when the html has an tag and that image is not renderred within the image created by html. Can someone help me with a method that can solve my problem. The printer is able to support postscripts as well.

This is my approach

protected void generateDoc(DataObj data) {
DocFlavor dsc =
        DocFlavor.INPUT_STREAM.PNG;

   // get the html file's contents
   String receipt =
       getFileContents("Receipt.html");

   // process the html contents and insert the data details
    receipt = processHTMLContents(receipt, data);

    // create image of the receipt
    createReceiptImage(receipt);

    InputStream is =
        null;
    try {
        is =
            new FileInputStream(new File("testingToday.png")); // the same image which was created below
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    // create the doc to be sent to the printer
    Doc doc =
        new SimpleDoc(is, dsc, null);

    return doc;
}

/**
 * Create an image of the html receipt.
 * @param htmlReceipt processed html receipt
 * @return
 * @throws InterruptedException
 */
protected void createReceiptImage(String htmlReceipt) throws InterruptedException {
    JEditorPane pane =
        new JEditorPane();
    //pane.setEditable(false);
    pane.setEditorKit(new HTMLEditorKit());
    pane.setContentType("text/html");
    pane.setText(htmlReceipt);
    pane.setSize(650, 850);
    pane.setBackground(Color.white);

    // Create a BufferedImage
    BufferedImage image =
        new BufferedImage(pane.getWidth(), pane.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g =
        image.createGraphics();

    // Have the image painted by SwingUtilities
    JPanel container =
        new JPanel();
    SwingUtilities.paintComponent(g, pane, container, 0, 0, image
        .getWidth(), image.getHeight());
    g.dispose();


    ImageIO.write(image, "PNG", new File("testingToday.png")); // this would be replaced by a relative network location

}

and this doc is then sent to the printer. but this is not a desirable approach as it is swing class and it is not able to render any images inside the html. I have already spent around a week over it but still cant end upon a solution. how to fix this or what can be the solution?

Peyush Goel
  • 383
  • 2
  • 3
  • 13

1 Answers1

0

Although the question seems old, you can have a look at this question.. Basically it converts a HTML to PDF & then you print the PDF... hope this helps

Community
  • 1
  • 1
bhatanant2
  • 596
  • 4
  • 9
  • printing pdf through javax.print converts it to a formal most of the printers do not support. I tried reading a pdf and print it but it never worked for me – Peyush Goel Nov 01 '12 at 14:35