在 Google App Engine for java 上生成并通过电子邮件发送 pdf
我有一个要求,我想动态创建 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("[email protected]"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("[email protected]"));
msg.setSubject("testing PDF system");
Transport.send(msg);
I have requirement where i want to create pdf on the fly and mail it to a user on google app engine for java. i tried using pdfJet but it seems to have issue as app engine is throwing exceptions while trying to email the created pdf.
Anyone having a working sample using either pdfjet or some other library please advise..
with pdfJet my code looks like:
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("[email protected]"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("[email protected]"));
msg.setSubject("testing PDF system");
Transport.send(msg);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能有点晚了,但我想我会帮忙,以防其他人遇到这个问题。我认为问题在于您试图将文档附加到电子邮件的 html 部分中,而不是将其添加为附件。
首先,我使用 pdfjet 在此方法中创建 pdf(我在没有测试的情况下对其进行了一些编辑,但这应该可行)
这是我用来发送电子邮件的方法,将 pdf 作为字节数组传递。 (这正是代码,只是我更改了发件人电子邮件地址。请记住,发件人电子邮件地址应遵循此处的规则 https://developers.google.com/appengine/docs/java/mail/#Java_Sending_mail)
This might be a bit late but I thought I'd chip in in case anyone else out there comes across this. I think the problem is that you're trying to attach the document inside the html section of the email, rather than adding it as an attachment.
First I use pdfjet to create the pdf in this method (I've edited this a bit without testing, but this should work)
Here is the method I use to send an email, passing the pdf in as a byte array. (This is exactly the code except I've changed the from email address. Bear in mind that the from email address should follow the rules here https://developers.google.com/appengine/docs/java/mail/#Java_Sending_mail)