使用xpages将iText pdf直接发送到莲花电子邮件

时间:2013-07-25 06:36:32

标签: xpages xpages-ssjs

我正在尝试将新生成的pdf(使用itext)直接传输到lotus notes电子邮件的正文作为附件。但我从字节

设置电子邮件的正文时遇到以下错误
"com.ibm.jscript.types.GeneratedWrapperObject$StaticField incompatible with com.ibm.jscript.types.FBSValue"

以下是我完成的代码(放在xpage的按钮中)。任何帮助将不胜感激

    session.setConvertMIME(false);

outputStream:java.io.ByteArrayOutputStream = new java.io.ByteArrayOutputStream();
writePdf(outputStream);
var bytes = outputStream.toByteArray();
var inputStream:java.io.ByteArrayInputStream = new java.io.ByteArrayInputStream(bytes);

var db:NotesDatabase= session.getDatabase("","mail.box")

if (!db.isOpen()) {
    print ("No mailbox!")
}
else
{
    var doc:NotesDocument=db.createDocument()
    doc.replaceItemValue("Form","Memo")
    doc.replaceItemValue("From",context.getUser().getCommonName())
    doc.replaceItemValue("Principal",context.getUser().getCommonName())
    doc.replaceItemValue("SendTo","a@b.com");
    doc.replaceItemValue("Recipients","a@b.com");
    doc.replaceItemValue("CopyTo","a@b.com");

    doc.replaceItemValue("INetFrom","b@c.com");
    var strFileName="temp.pdf"

    var body:NotesMIMEEntity = doc.createMIMEEntity('Body');
    var hdr:NotesMIMEHeader = body.createHeader("Subject");
    hdr.setHeaderValAndParams("Subject")
    hdr=body.createHeader("MIME-Version")
    hdr.setHeaderValAndParams("1.0")
    body.setPreamble("multipart message in MIME")

    var child1:NotesMIMEEntity= body.createChildEntity()

    hdr = child1.createHeader("Content-Disposition")
    hdr.setHeaderValAndParams('attachment; filename="test.pdf"')
    var stream:NotesStream = session.createStream();
    stream.setContents(inputStream)
    child1.setContentFromBytes(stream, "application/pdf", body.ENC_IDENTITY_BINARY)
    child1.encodeContent(body.ENC_BASE64)
    doc.closeMIMEEntities(true,"Body")

    doc.save(true, true);
    // Restore conversion
    session.setConvertMIME(true);       
}
    function writePdf(outputStream) {

    var document:com.itextpdf.text.Document = new com.itextpdf.text.Document();
    var writer = com.itextpdf.text.pdf.PdfWriter.getInstance(document,outputStream);

    document.open();

    document.addTitle("Test PDF");
    document.addSubject("Testing email PDF");
    document.addKeywords("iText, email");
    document.addAuthor("Author");
    document.addCreator("Creator");

    var passChunk:com.itextpdf.text.Chunk = new com.itextpdf.text.Chunk("Hello");   
    document.add(new com.itextpdf.text.Paragraph(passChunk));

    document.close();
}

1 个答案:

答案 0 :(得分:1)

你最好还是写一个小的Java包装器类。 首先,你需要:

   var stream:NotesStream = session.createStream();
   stream.setContents(inputStream);
   stream.setPosition(0);

所以流就在开头。

更新的:
你也有:
var bytes = outputStream.toByteArray();
var inputStream:java.io.ByteArrayInputStream = new java.io.ByteArrayInputStream(bytes); stream.setContents(的inputStream);
我会写的地方:

   var bytes = outputStream.toByteArray();
   stream.write(bytes);

仍然,用Java做一个帮手。

注意:iText是GPL许可的。除非您构建的应用程序仅供内部使用,否则您需要购买商业许可证或GPL代码。请查看Apache PDFBox以获取替代