pdf로 현재 jsf 페이지를 내보내려고했습니다. 코드는 다음과 같이JSF2.0은 명령 단추를 사용하여 XHTML보기의 PDF 파일을 엽니 다.
public void writePDF() {
String PDF_FILE_NAME = "converted.pdf";
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext =facesContext.getExternalContext();
HttpServletRequest request = (HttpServletRequest)externalContext.getRequest();
HttpSession session = (HttpSession) externalContext.getSession(true);
String url = request.getRequestURL().append(";jsessionid=").append(session.getId().toString()).toString();
try
{
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new URL(url).toString());
renderer.layout();
HttpServletResponse response = (HttpServletResponse)externalContext.getResponse();
response.reset();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline;filename=\"" + PDF_FILE_NAME + "\"");
ServletOutputStream browserStream = response.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
renderer.createPDF(browserStream);
renderer.finishPDF();
facesContext.responseComplete();
}
catch (Exception ex)
{
ex.printStackTrace();
}
facesContext.responseComplete();
}
명령 버튼을 클릭에서만 빈 PDF 페이지가 더으로 생성되지 않습니다 다음과 같이 명령 단추와
페이지 index.html
<?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://java.sun.com/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:inputText value="#{bean.name}" id="name" ></h:inputText>
<h:commandButton value="click" id="btn" actionListener="#{bean.writePDF}"></h:commandButton>
</h:form>
</h:body></html>
writePDF()
동작 버튼에 대한 기능입니다 컨트롤과 가치. 내가 잘못하고있는 것을 친절하게 제안합니다.
미리 감사드립니다.
JSF 쪽이 멋지게 보입니다. iText/Flyingsaucer 쪽이 잘못 될 것입니다. 양식에 몇 개의 텍스트/이미지를 넣으려고 했습니까? 아마도 iText/Flyingsaucer는 입력 요소 및 버튼과 같은 양식 구성 요소를 먹지 않습니다. 그런 다음 JSF 렌더링을 변경하여 양식 구성 요소 대신 일반 텍스트로 관련 정보를 표시해야합니다. – BalusC