【问题标题】:Primefaces Print Dynamic ImagesPrimefaces 打印动态图像
【发布时间】:2012-12-13 13:38:31
【问题描述】:

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

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

我的 bean 是这样的:

    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;


}

好像她忘记了创建的图像。

谢谢。

【问题讨论】:

    标签: image dynamic printing primefaces


    【解决方案1】:
    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;
        }
    }
    
    • 在你看来:&lt;pou:graphicImage id="image" value="#{image}" /&gt;

    【讨论】:

      猜你喜欢
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      相关资源
      最近更新 更多