在Primefaces页面中显示Pdf文件

时间:2014-08-11 17:02:19

标签: jsf primefaces itext

我在jsf托管bean中生成带有Itextpdf包的pdf文件,现在我想要当用户点击p:commandButton时,用户可以从p:media看到page2.xhtml中的pdf文件。

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org    /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <p:inputText value="#{bean.text}" />
            <p:commandButton value="Show Pdf File" action="page2" />
        </h:form>

    </h:body>
</html>

和Page2.xhtml是:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Page 2</title>
    </h:head>
    <h:body>
        <p:media value="??????????" width="500px" height="500px" player="pdf" />
    </h:body>
</html>

我的bean文件是:

    package pack;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;


@ManagedBean
@RequestScoped
public class bean {

    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }



    /**
     * Creates a new instance of bean
     */
    public bean() {
        this.create_pdf();
    }

    public void create_pdf(){
        try {
            Document doc = new Document();
            OutputStream file = new FileOutputStream(new File("HelloWorld.pdf"));

            PdfWriter writer = PdfWriter.getInstance(doc, file);

            doc.open();
            doc.add(new Paragraph("A Hello World PDF document."));
            doc.add(new Paragraph(this.text));
            doc.close(); 
            file.close();            
        } catch (Exception e) {        }

    }

}

1 个答案:

答案 0 :(得分:3)

感谢Leo&amp; faissalb的回应。 这种回应的细节是:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>page 1</title>
    </h:head>
    <h:body>
        <p:media value="#{bean.streamedContent}" width="700px" height="700px" player="pdf" />
    </h:body>
</html>

和bean文件:

package pack;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author alfa
 */
@ManagedBean
@RequestScoped
public class bean {

    private static final long serialVersionUID = 1L;

    private StreamedContent streamedContent;


    @PostConstruct     
    public void init() {
        try {
        //----------------------------------
            Document doc = new Document();

            OutputStream out = new ByteArrayOutputStream();
            PdfWriter writer = PdfWriter.getInstance(doc, out);

            doc.open();
            doc.add(new Paragraph("Hello World. ok........"));
            doc.close(); 
            out.close();

            InputStream in =new ByteArrayInputStream(((ByteArrayOutputStream)out).toByteArray());

            streamedContent = new DefaultStreamedContent(in, "application/pdf");
        //-------
        Map<String, Object> session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        byte[] b = (byte[]) session.get("reportBytes");
        if (b != null) {
            streamedContent = new DefaultStreamedContent(new ByteArrayInputStream(b), "application/pdf");
        }            
        } catch (Exception e) {
        }

    }
    //==================================================================
    public StreamedContent getStreamedContent() {
        if (FacesContext.getCurrentInstance().getRenderResponse()) {
            return new DefaultStreamedContent();
        } else {
            return streamedContent;
        }
     }
    //==================================================================
    public void setStreamedContent(StreamedContent streamedContent) {
        this.streamedContent = streamedContent;
    }
    //=====================================================================

}