我在一個打開PDF文件的按鈕中有SSJS,寫入它的一些字段(Acroform),然後將文件下載到用戶。所有的作品都很棒(使用pdfbox),但我想成爲一名優秀的程序員,如果原始pdf文件不可用,請取消操作。否則,用戶仍會被提示打開文件,但Adobe Reader報告該文件已損壞(顯然會是)。我在我調用的Java類中執行我的pdf操作,並傳遞響應對象的outputStream。以下是我的SSJS。如果我測試來自newVal.outputPdf的ret值並將所有其他代碼放在if語句中,那麼我的XPage只是空白。我假設,因爲響應和outputStream已經打開?取消創建用於創建新PDF文件的outputteam的最佳方法?
霍華德
importPackage(com.tlcc);
var newVal = new PdfBoxTest();
importPackage(java.net);
importPackage(java.lang);
var con = facesContext.getExternalContext();
var response:com.ibm.xsp.webapp.XspHttpServletResponse = con.getResponse();
try {
var writer:javax.servlet.ServletOutputStream = response.getOutputStream();
//get the stream
var ret = newVal.outputPdf(writer, "http://localhost/pdfexportcc.nsf/certificate.pdf");
// setting response headers for browser
print("Good output");
response.setContentType("application/pdf");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", -1);
response.setHeader("Content-Disposition", "attachment; filename=\"mypdf.pdf\"");
writer.flush();
writer.close();
print("in close");
facesContext.responseComplete();
} catch (e) {
var errorMessage = "An error has occured: " + e.toString();
_dump(errorMessage);
writer.close();
response.sendError(500, errorMessage);
}
與所有的工作再次嘗試是用Java語言實現。我從一個按鈕調用了這個方法。與有效的網址工作正常,但當網址不好時,會引發錯誤。異常在OutputStream已被使用時無法獲得Writer。
public boolean outputAllInJavaPdf() {
try {
FacesContext context = FacesContext.getCurrentInstance();
XspHttpServletResponse response = (XspHttpServletResponse) context.getExternalContext().getResponse();
ServletOutputStream writer = response.getOutputStream();
InputStream docUrl = new URL("http://localhost/pdfexportcc.nsf/certifxxicate.pdf").openStream();
pdfDoc = PDDocument.load(docUrl);
System.out.println("Number of pages is " + pdfDoc.getNumberOfPages());
setField("Student", "James Namce");
setField("CourseName", "XPages Development 2 for Notes and Domino 9");
setField("Instructor", "John Smith");
System.out.println("After set field");
pdfDoc.save(writer);
pdfDoc.close();
response.setContentType("application/pdf");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", -1);
response.setHeader("Content-Disposition", "attachment; filename=\"mypdf.pdf\"");
writer.flush();
writer.close();
context.responseComplete();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
return false;
}
}
我確實需要XPage。我想在頁面上有按鈕來創建pdf並下載/打開它。但XPage應該保持開放。 – Howard 2015-04-03 13:26:22