将生成的png渲染到JSF中

时间:2013-07-26 22:45:29

标签: java jsf primefaces pdf-generation pdfbox

我在JSF支持bean中构建了一个图像形式的PDF doc,我需要在JSF页面中显示图像。我发现primefaces有一个名为的组件,我定义了一个变量:

    private StreamedContent pdfImage;  

根据此示例http://www.primefaces.org/showcase/ui/dynamicImage.jsf。在我的代码中 我使用一些数据和apache PDFBox构建了pdf并将文档保存到:

    private byte[] bytesPdf; 

我的jsf行是

<p:graphicImage value="#{myBean.pdfImage}" rendered="#{myBean.showImage}"/>

之后我调用以下方法将第一个PDF文档页面转换为PNG我得到:

public void buildPDFImage() throws IOException{
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    //Build bufferedImage from pdf bytearray
    InputStream input = new ByteArrayInputStream(bytesPdf);
    PDDocument archivo=new PDDocument();
    archivo=PDDocument.load(input);
    PDPage firstPage = (PDPage) archivo.getDocumentCatalog().getAllPages().get(0);
    BufferedImage bufferedImage = firstPage.convertToImage();   
    //File exit=new File("d:/exit.png"); if i do something like this and pass file as param to iowrite image is generated on file system
    try {
        ImageIO.write(bufferedImage, "png", os);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }        
    pdfImage = new DefaultStreamedContent(new      ByteArrayInputStream(os.toByteArray()), "image/png");          
}

当我在生成pdf图像后运行我的应用程序时,我得到此跟踪

  GRAVE: Error Rendering View[/pages/apphuella.xhtml]
  java.lang.IllegalStateException: PWC3999: Cannot create a session after the response   has been committed
at org.apache.catalina.connector.Request.doGetSession(Request.java:2880)
at org.apache.catalina.connector.Request.getSession(Request.java:2577)
at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:920)
at com.sun.faces.context.SessionMap.getSession(SessionMap.java:235)
at com.sun.faces.context.SessionMap.put(SessionMap.java:126)
at com.sun.faces.context.SessionMap.put(SessionMap.java:61)
at org.primefaces.component.graphicimage.GraphicImageRenderer.getImageSrc(GraphicImageRenderer.java:105)
at org.primefaces.component.graphicimage.GraphicImageRenderer.encodeEnd(GraphicImageRenderer.java:45)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1763)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
at 

如果我不能用它来显示生成的png(以及其他图像),我会知道我是否正确使用?,如果还有其他选项在JSF页面上显示运行时生成的图像。

提前致谢

1 个答案:

答案 0 :(得分:0)

首先,您需要将文件保留为临时目录。之后,您应该再次渲染文件。 请尝试以下

支持Bean

private String filePath;

public String filePath() {
    return filePath;
}

private String getSystemPath() {
    Object context = getFacesContext().getExternalContext().getContext();
    String systemPath = ((ServletContext)context).getRealPath("/");
    return systemPath;
}

public void buildPDFImage() throws IOException {
    // create any kind of file types.
    byte[] cotent =                             --> take byte array content of your files.
    Stirng fileName = "xxx.pdf" or "xxx.png"    --> your file name
    String dirPath = "/pdf/" or "/images/";     --> a directory to place created files.
    filePath = dirPath + fileName
    createFile(new File(getSystemPath() + filePath), content);
}

private void createFile(File file, byte[] content) {
    try {
        /*At First : Create directory of target file*/
        String filePath = file.getPath();
        int lastIndex = filePath.lastIndexOf("\\") + 1;
        FileUtils.forceMkdir(new File(filePath.substring(0, lastIndex)));

        /*Create target file*/
        FileOutputStream outputStream = new FileOutputStream(file);
        IOUtils.write(content, outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

页面

<h:form enctype="multipart/form-data">
    <h:commandButton value="Create"/>
    <!-- Your Files is image -->
    <p:graphicImage value="#{myBean.filePath}"/>

    <!-- 
        Your Files is pdf. You need PDF Viewer plugin for your browser.
        My FireFox vesion is 20.0. It have default PDF Viewer.
        If PDF File cannot display on your browser, it is depond on browser setting also.
    -->
    <p:media value="#{myBean.filePath}" width="100%" height="300px"/>  
</h:form>