我有要求我想要动态创建pdf并将其邮寄给谷歌应用程序引擎上的用户。我尝试使用pdfJet但似乎有问题,因为app引擎在尝试通过电子邮件发送创建的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);
答案 0 :(得分:1)
这可能有点晚了,但我想我会插手以防万一其他人遇到这个问题。我认为问题在于您尝试将文档附加到电子邮件的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();
}
}