gpt4 book ai didi

java - Apache FOP - PDF 流式传输到 Commons 电子邮件附件

转载 作者:行者123 更新时间:2023-11-29 05:56:15 25 4
gpt4 key购买 nike

我使用带有 XSL-FO 的 Apache Fop 来生成 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());
}
}

一流的

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();

关于java - Apache FOP - PDF 流式传输到 Commons 电子邮件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12009696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com