0
我的要求: 點擊JSF2中的「打印」按鈕,打開PDF文件從XHTML頁面中的數據打印。如何使用PDF JSF2及iText的顯示XHTML頁面內容只,PDF格式不顯示任何數據
下面的代碼打開一個沒有數據的空白pdf文件。我想PDF顯示從XHTML頁面(URL)數據
JSF代碼: JSF有一些領域來顯示數據
<h:commandButton id="printButton" value="Print" actionListener="#{myBean.print}" />
MyBean.Java代碼:
public void print()
{
ExternalContext externalContext =FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response = (HttpServletResponse)externalContext.getResponse();
String url = request.getRequestURL();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document(PageSize.LETTER);
PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph(" Hello"));
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline;filename=\"" + PDF_FILE_NAME + "\"");
response.setContentLength(baos.size());
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
document.close();
System.out.println("Done.");
FacesContext.getCurrentInstance().responseComplete();
}
任何幫助將是讚賞。 在此先感謝,