innoveit / play2-pdf

A PDF module for Play Framework 2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PDF as an attachment

ChickenSniper opened this issue · comments

commented

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?

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

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

commented

Thank you @doernemt !!!!

commented

@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...

@ChickenSniper sure, send me your instructions.