2012-06-07 85 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並打印它,但它從來沒有爲我工作 –

相關問題