2012-12-13 115 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}" />