取消创建用于创建新pdf文件的输出组的最佳方法是什么?

时间:2015-04-02 19:25:45

标签: xpages xpages-ssjs

我在一个打开pdf文件的按钮中有SSJS,写入其上的某些字段(Acroform),然后将文件下载到用户。一切都很好(使用pdfbox),但我想成为一名优秀的程序员,如果原来的pdf文件不可用,那么取消操作。否则,仍然会提示用户打开该文件,但Adobe Reader报告文件已损坏(显然会是这样)。我在我调用的Java类中执行pdf操作并传入响应对象的outputStream。以下是我的SSJS。如果我从newVal.outputPdf测试ret值并将所有其他代码放在if语句中,那么我的XPage只是空白。我假设因为响应和outputStream已经打开了?

霍华德

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完成的所有工作。我从一个按钮调用了这个方法。使用有效的URL工作正常,但当网址错误时会抛出错误。异常在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;
    }
}

2 个答案:

答案 0 :(得分:1)

全部取决于您告诉浏览器的内容。

您使用PDF文件的内容类型。浏览器打开(下载)PDF文件。您放入的任何内容(例如错误页面)都被视为PDF文件的内容。 因此,如果PDF生成失败,请不要设置该内容类型并将浏览器重定向到错误页面,或者返回原始XPage并进行说明。

public boolean outputAllInJavaPdf() {
    try {
        FacesContext context = FacesContext.getCurrentInstance();

        XspHttpServletResponse response = (XspHttpServletResponse) context.getExternalContext().getResponse();

        ServletOutputStream writer = response.getOutputStream();
        boolean servePdf = true;
        try {
            InputStream docUrl = new URL("http://localhost/pdfexportcc.nsf/certifxxicate.pdf").openStream();
            pdfDoc = PDDocument.load(docUrl);
            // do something to validate PDF
        } catch (Exception e) {
            //no PDF
            servePdf = false;
        }

        if (servePdf) {
            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\"");

        } else {
            // take care of no PDF response - redirect?
        }
            writer.flush();
            writer.close();
            context.responseComplete();
            return true;
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
        return false;
    }
}

答案 1 :(得分:0)

可能正在尝试渲染您的Xpage,但您想自己控制响应。要阻止呈现Xpage并控制编写器,请将rendered="false"添加到您的Xpage。