Primefaces打印动态图像

时间:2012-12-13 13:38:31

标签: image dynamic printing primefaces

我尝试动态加载图片,一切正常。 图像I在动态中正确加载和显示 我添加了打印标签。 现在,如果我要求打印动态创建的图像,我无法打印。

       <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;


}

就好像她忘记了所创造的图像一样。

感谢。

1 个答案:

答案 0 :(得分: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}" />