我很确定这将是一个简单的解决方案,但我无法找到任何关于我的问题的帮助。我正在创建一个pdf文件,目前用户必须在打开它之前保存它。但是我必须在用户决定保存之前打开它。但我找不到如何告诉iText在新标签中打开它。我该怎么办?
这是我的代码,我在“document.close();”之后立即调用此方法:
public void openPDF(String nomDoc, String nomFichier) throws IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext
.getResponse();
// File file = new File(getFilePath(), getFileName());
File file = new File(nomFichier);
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(file),
DEFAULT_BUFFER_SIZE);
response.reset();
response.setHeader("Content-Type", "application/pdf");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Content-Length", String.valueOf(file.length()));
// response.setHeader("Content-Disposition", "inline; filename=\"" +
// nomFichier + "\"");
response.setHeader("Content-Disposition", "form-data;filename=\""
+ nomDoc + "\"");
output = new BufferedOutputStream(response.getOutputStream(),
DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
} finally {
close(output);
close(input);
}
facesContext.responseComplete();
}
我的命令按钮:
<p:commandButton process="checkboxContrats"
icon="ui-icon-file-pdf" value="Editer les fiches de paie"
action="#{paieAgentView.printPaieAgent()}" update="@form"
escape="false" ajax="false" />
我的观点:
public void creerPDF(List<FichePaie> listFdP, Date dateDebut, Date dateFin) throws DocumentException,
IOException {
ServletContext servletContext = (ServletContext) FacesContext
.getCurrentInstance().getExternalContext().getContext();
String nomRelatif = servletContext.getRealPath(NOMFICHIER);
Date dtt = new Date();
Phrase para = new Phrase("Date d'édition : " + sdf.format(dtt),
FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL));
Document document = this.getDocumentPortraitSmallMargin(nomRelatif,
para);
document.open();
document.add(new Chunk(""));
document.close();
openPDF(filename, nomRelatif );
}
感谢您的帮助。
答案 0 :(得分:2)
在新窗口或标签页中打开PDF文件取决于您对HTML的调用。如果你正在使用GET请求,你可以像
那样做获取
<a href="/url/to/servlet" target="_blank"/>
以防POST请求
<form method="post" action="url/to/servlet"
target="_blank">
target="_blank"
将在哪里处理。
答案 1 :(得分:0)
谢谢你们,
首先我必须将内容处置更改为内联:
response.setHeader("Content-Disposition", "inline;filename=\""
+ nomDoc + "\"");
然后我不得不告诉我的命令按钮在新标签中打开它,目标在onclick中:
<p:commandButton process="checkboxContrats"
icon="ui-icon-file-pdf" value="Editer les fiches de paie"
action="#{paieAgentView.printPaieAgent()}" update="@form"
escape="false" ajax="false" onclick="this.form.target='_blank'"/>
答案 2 :(得分:-1)
尝试使用这些标题:
Response.ContentType = "application/" & "octet-stream"
Response.AddHeader("Content-disposition", "attachment; filename="myfile.pdf")
Response.AddHeader("Content-Length", buffer.Length.ToString())
'**** Change next line to use response output stream
Response.BinaryWrite(buffer)
Response.Flush()
Response.Close()
Response.End()
(代码在vb中,但在C#中应该没有太大差异)。这应该在浏览器底部的图标中下载文件,如果打开,则打开一个新选项卡或pdf阅读的默认应用程序。