innoveit / play2-pdf

A PDF module for Play Framework 2
MIT License
77 stars 22 forks source link

PDF as an attachment #7

Closed ChickenSniper closed 9 years ago

ChickenSniper commented 9 years ago

Hello, I'm posing this question here as this project does not seem to have a chat. I need to both serve PDF directly to the client and/or as an attached document to an email. Can this be done with play2-pdf?

Thank you.

Also, in keeping with Play's vision of being 100% reactive and non-blocking, do you have any plans to migrate from java to scala?

marcosinigaglia commented 9 years ago

Yes, you can generate the pdf and attach to an email, not expected to porting to scala, watch for other fork.

doernemt commented 9 years ago

Hi, I generate PDFs and email them in my App (hosted on Heroku, for emailing I use the free plan from SendGrid). Basically, you just need to call PdfGenerator.toBytes which delivers a byte array.

 byte[] pdf = PdfGenerator.toBytes(listEmployeesPrint.render(employees), null);

 String tmpDir = File.createTempFile("Zeituebersicht", ".pdf").getAbsolutePath();
 FileOutputStream fos = new FileOutputStream(tmpDir);
 fos.write(pdf);
 fos.close();

and then mail it using SendGrid

 SendGrid sendgrid = new SendGrid(username, password);

 SendGrid.Email email = new SendGrid.Email();
 email.addTo(to);
 email.setFrom(username);
 email.setSubject(subject);
 email.setText(body);
 try {
            email.addAttachment(filename, attachment);
            Logger.debug("Added attachment");
  } catch (IOException e) {
      e.printStackTrace();
    return false;
}

Best, Tim

ChickenSniper commented 9 years ago

Thank you @doernemt !!!!

ChickenSniper commented 9 years ago

@marcosinigaglia

FYI...got this working in scala play project...let me know if you would like how I got it working so you can add to documentation...

marcosinigaglia commented 9 years ago

@ChickenSniper sure, send me your instructions.