gpt4 book ai didi

java - 在 google app engine for java 上生成并通过电子邮件发送 pdf

转载 作者:行者123 更新时间:2023-11-30 11:53:46 26 4
gpt4 key购买 nike

我有要求,我想即时创建 pdf 并将其邮寄给 google app engine for java 上的用户。我尝试使用 pdfJet,但它似乎有问题,因为应用引擎在尝试通过电子邮件发送创建的 pdf 时抛出异常。

任何有使用 pdfjet 或其他库的工作示例的人请告知..

使用 pdfJet 我的代码如下所示:

ByteArrayOutputStream out = new ByteArrayOutputStream();
PDF pdf;
try {
pdf = new PDF(out);
log.info("#1");
pdf.setTitle("Using TextColumn and Paragraph classes");
pdf.setSubject("Examples");
pdf.setAuthor("Innovatics Inc.");
log.info("#2");

Page page = new Page(pdf, Letter.PORTRAIT);
pdf.flush();

Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setFileName("whatever.pdf");
log.info("#7");
htmlPart.setContent(out.toByteArray(), "application/pdf");
mp.addBodyPart(htmlPart);
log.info("#8");
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

Message msg = new MimeMessage(session);
msg.setContent(mp);
msg.setFrom(new InternetAddress("vik.ceo@gmail.com"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("vik.ceo@gmail.com"));


msg.setSubject("testing PDF system");
Transport.send(msg);

最佳答案

这可能有点晚了,但我想我会插手以防其他人遇到这个问题。我认为问题在于您试图将文档附加到电子邮件的 html 部分,而不是将其添加为附件。

首先,我使用 pdfjet 以这种方法创建 pdf(我在没有测试的情况下对此进行了一些编辑,但这应该可行)

private byte[] createPDF(String title) throws Exception
{
ByteArrayOutputStream out = new ByteArrayOutputStream();

PDF pdf = new PDF(out);
pdf.setTitle("Title");

Page page = new Page(pdf, Letter.PORTRAIT);
Font f1 = new Font(pdf, CoreFont.HELVETICA);
f1.setSize(16);

TextColumn column = new TextColumn();
column.setLineBetweenParagraphs(true);
column.setLineSpacing(1.0);

//Fill data

Paragraph title = new Paragraph();
title.setAlignment(Align.CENTER);
title.add(new TextLine(f1, text));
column.addParagraph(title);

column.setPosition(90, 90);
column.setSize(470, 100);
column.drawOn(page);

pdf.flush();
byte[] bytes = out.toByteArray();
return bytes;
}

这是我用来发送电子邮件的方法,将 pdf 作为字节数组传入。 (这正是代码,只是我更改了发件人电子邮件地址。请记住,发件人电子邮件地址应遵循此处的规则 https://developers.google.com/appengine/docs/java/mail/#Java_Sending_mail)

private void sendEmailWithPDF(String recipient, String content, byte[] pdf) throws Exception
{
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);

String htmlBody = content;

try {
javax.mail.Message msg = new MimeMessage(session);
MimeMultipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
MimeBodyPart attachment = new MimeBodyPart();

msg.setFrom(new InternetAddress("myaddress@mydomain.com"));
msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recipient,recipient));
msg.setSubject(content);

//prepare html part
htmlPart.setContent(htmlBody, "text/html");

//prepare attachment using a bytearraydatasource
DataSource src = new ByteArrayDataSource(pdf, "application/pdf");
attachment.setFileName("form " + new Date().toString() + ".pdf");
attachment.setDataHandler(new DataHandler(src));

//put the parts together into a multipart
mp.addBodyPart(htmlPart);
mp.addBodyPart(attachment);

//set the content of the message to be the multipart
msg.setContent(mp);
msg.saveChanges();

Transport.send(msg);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}

关于java - 在 google app engine for java 上生成并通过电子邮件发送 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6105704/

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