在 Google App Engine for java 上生成并通过电子邮件发送 pdf

发布于 2024-11-09 15:06:21 字数 1560 浏览 4 评论 0原文

我有一个要求,我想动态创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扬花落满肩 2024-11-16 15:06:21

这可能有点晚了,但我想我会帮忙,以防其他人遇到这个问题。我认为问题在于您试图将文档附加到电子邮件的 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("[email protected]")); 
              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(); 
      }
  }

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)

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;
}

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)

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("[email protected]")); 
              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(); 
      }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文