2012-06-07 50 views
3

我写在本地服务器端的服务将接受来自UI应用程序中的打印机名称和其他投入和打印HTML文件所需的网络打印机。它不是桌面应用程序。我有我读入一个字符串处理后的HTML文件,并希望它的输出发送到所需的打印机。我能找到打印HTML输出到打印机上的服务器应用程序

1的方式是通过阅读它到JEditorPane创建映像(尽管通过使用摆动类不是很大的方法),然后保存图像,然后将其发送到打印机。但是,当HTML有一个标签和图像不被HTML创建的图像中renderred失败。有人可以帮助我解决我的问题的方法。打印机也能够支持后记。

这是我的做法

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 

}

,然后这个文档被发送到打印机。但因为它是摆动类,它是不能够呈现HTML内的任何图像,这是不是一个可取的办法。我已经花了大约一个星期的时间,但仍不能解决问题。如何解决这个问题或者可以解决什么问题?

回答

0

虽然这个问题似乎是旧的,你可以看看这个question。基本上这一个转换到HTML PDF &那么你打印PDF ...希望这有助于

+0

打印PDF通过的javax.print将其转换到正式的大部分打印机都不支持。我尝试阅读PDF并打印它,但它从来没有为我工作 –

相关问题