【发布时间】:2014-08-11 17:02:18
【问题描述】:
我在 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) { }
}
}
【问题讨论】:
标签: jsf primefaces itext