簡體   English   中英

Apache FOP - PDF流式傳輸到Commons Email Attachment

[英]Apache FOP - PDF streamed to Commons Email Attachment

我正在使用Apache Fop和XSL-FO生成PDF。 我正在嘗試將pdf作為apache.commons.mail.HtmlEmail的附件流式傳輸; 我收到附件的電子郵件,但是我收到以下錯誤。 長度為0個字節,無編碼。 我能夠在文件系統上創建一個沒有任何問題的pdf,所以我知道這個代碼的FOP部分沒有任何問題,所以我不確定為什么它不起作用。 有人能告訴我我錯過了什么嗎?

我的代碼。

private void sendBroadcastNofications(PurchaseRequest pr) {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //Fop sevice used to generate pdf.
        this.xmlPDFGeneratorService.generatePDF(pr, outputStream);
        byte[] bytes = outputStream.toByteArray();

        //I'm not sure if "application/pdf" would be an issue since fop is giving 
        //it the MimeConstants.MIME_PDF
        DataSource source = new ByteArrayDataSource(bytes, "application/pdf");            
        EmailAttachment attachment = new EmailAttachment(source, "purchase_requisition.pdf", "Broadcast Purcahse Requisition");

        Util.email("Purchase Request " + pr.getPrNumber(), getGenerateMessage(pr), pr.getAuthorizer().getEmail(), attachment);

    } catch (IOException ex) {
        Logger.getLogger(EmailServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}


public static void email(String subject, String message, String recipient, EmailAttachment attachment) {
    email(subject, message, Collections.singleton(recipient), Collections.singleton(attachment));
}

public static void email(String subject, String message, Set<String> recipients, Set<EmailAttachment> attachments) {
    try {
        HtmlEmail email = new HtmlEmail();

        email.setHostName("mailhost");
        email.setSubject(subject);
        email.setHtmlMsg(message);
        email.setFrom("company@domain.com");

        for (EmailAttachment attachment : attachments) {
            try {
                email.attach(attachment.getDataSource(), attachment.getName(), attachment.getDescription());
            } catch (EmailException ex) {
                System.err.println("Email Attachment Exception: " + ex.getMessage());
            }
        }

        for (String recipient : recipients) {
            email.addTo(recipient);
        }
        email.send();
    } catch (EmailException ex) {
        System.err.println("Email Failed to Send: " + ex.getMessage());
    }
}

Fop類

public class XMLPDFGeneratorServiceImpl implements XMLPDFGeneratorService {

private static final File baseDir = new File(".");
private static final File xsltfile = new File(baseDir, "./PurchaseRequestPDF.xsl");

// configure fopFactory as desired
private final FopFactory fopFactory = FopFactory.newInstance();

public void generatePDF(PurchaseRequest pr, ByteArrayOutputStream outStream) {

    // configure foUserAgent as desired
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

    try {

        // Setup output
        outStream = new ByteArrayOutputStream();

        // Construct fop with desired output format
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outStream);

        // Setup XSLT
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));

        // Set the value of a <param> in the stylesheet
        transformer.setParameter("versionParam", "2.0");

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        generateXML(pr, stream);  

        byte[] out = stream.toByteArray(); 
        stream.close();

        ByteArrayInputStream in = new ByteArrayInputStream(out);
        // Setup input for XSLT transformation
        Source src = new StreamSource(in);

        // Resulting SAX events (the generated FO) must be piped through to FOP
        Result res = new SAXResult(fop.getDefaultHandler());

        // Start XSLT transformation and FOP processing
        transformer.transform(src, res);

    } catch (TransformerException ex) {
        Logger.getLogger(XMLPDFGeneratorServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(XMLPDFGeneratorServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FOPException ex) {
        Logger.getLogger(XMLPDFGeneratorServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public void generateXML(PurchaseRequest pr, ByteArrayOutputStream stream) {
    try {
        // create JAXB context and instantiate marshaller
        JAXBContext context = JAXBContext.newInstance(PurchaseRequest.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
        m.marshal(pr, new PrintWriter(stream));
    } catch (JAXBException ex) {
        Logger.getLogger(XMLPDFGeneratorServiceImpl.class.getName()).log(Level.SEVERE, "JAXB Failed to produce xml stream", ex);
    }
}

}

你正在覆蓋generatePDF()中的outStream,因此你傳入的那個保持為空。 刪除該行,然后重試。

outStream = new ByteArrayOutputStream();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM