2012-12-13 192 views
1

我試圖動態加載圖像,並且一切正常。 圖像我加載並正確顯示在一個動態的, 我添加了打印標籤。 現在,如果我要求打印動態創建的圖像,我無法打印。Primefaces打印動態圖像

 <pou:commandButton value="Print" type="button" icon="ui-icon-print"> 
        <pou:printer target="image" /> 
     </pou:commandButton>  
     <pou:graphicImage id="image" value="#{printDynamicBean.graphicIMG}" /> 

我的豆確實是這樣的:

public StreamedContent getGraphicIMG() { 
     //Graphic 
     BufferedImage bufferedImg; 
     try { 
      bufferedImg = ImageIO.read(baseImage); 
     } catch (IOException e) { 

     } 
     try { 

      Graphics2D g2 = bufferedImg.createGraphics(); 
      g2.setColor(Color.black); 
      int style = Font.BOLD | Font.ITALIC; 
      Font f1 = new Font("TimesNewRoman", style , 60); 
      g2.setFont(f1); 
      g2.drawString("Hello Print", 80, 580); 
      ByteArrayOutputStream os = new ByteArrayOutputStream(); 
      ImageIO.write(bufferedImg, "png", os); 
      graphicIMG = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png"); 
     } catch (IOException ex) { 
      Logger.getLogger(PrintCartelliniBean.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return graphicIMG; 


} 

這是因爲如果她已經忘記創建的圖像。

謝謝。

回答

2
using CDI bean you can do this : 

@Model 
public class ImageBean { 

    private StreamedContent image; 

    @Produces 
    @Named 
    public StreamedContent getImage() { 
     if (FacesContext.getCurrentInstance().getRenderResponse()) { 
      // rendering the view, return a stub StreamedContent to generate right URL. 
      image = new DefaultStreamedContent(); 
     } else { 
      // requesting the image 
      image = your dynamic image; 
     } 

     return image; 
    } 
} 
    在你看來
  • <pou:graphicImage id="image" value="#{image}" />