Password Protect PDF Docs in Salesforce using Heroku

May 15, 2014 Appirio

By Ankit Kumar Goyal and Neeraj Gupta

One of our clients had a requirement to send monthly invoice statements to their customers. The statements are generated in Apex as pdf documents. For an extra layer of security, we wanted to have these monthly statements password protected. Unfortunately, we could not find a native solution that could do that.


We turned to Heroku to solve our issue. Heroku can run java code and manipulate a pdf blob.

The final solution involved the following steps:

  1. Salesforce generates the pdf, saves it as an attachment.
  2. A workflow rule fires and invokes Heroku Java server via outbound messaging.
  3. Record Id and Session Id are passed to Heroku server.
  4. Heroku uses the Session Id and Record Id to query back the attachment data.
  5. Java code is invoked on the attachment blob and password protects it.
  6. The Attachment is saved back in Salesforce.

[snippet caption=”Java that runs on Heroku for password protection”]String desiredPassword = ‘“Topsecret”

try {

PdfReader pdfReader = new PdfReader(request.getInputStream());

PdfStamper pdfStamper = new PdfStamper(pdfReader ,resp.getOutputStream());

pdfStamper.setEncryption(desiredPassword.getBytes(), desiredPassword.getBytes(),


if(pdfStamper != null) {

try {


} catch (DocumentException e) {

throw e



} catch (Exception e) {


throw e;



Heroku is a great solution for augmenting Salesforce capabilities. It especially has proven to be an excellent platform for manipulating blobs or images.

Heroku can tap Salesforce data by leveraging Salesforce APIs, and do complex back-end processing. And one of the great features of Heroku is that we can scale up and down the Heroku server depending on the user load. Heroku can also function independently exposing custom webservices, supporting mobile applications, or building custom UI, all on the cloud.

Previous Article
Activity Contact Lookup Override
Activity Contact Lookup Override

By Bryan Leboff I recently asked a good friend of mine what was his number one issue with Salesforce and wi...

Next Article
Real World Experience with Salesforce1 Heroku Connect
Real World Experience with Salesforce1 Heroku Connect

Today Heroku announced the general availability of Salesforce1 Heroku Connect. Heroku Connect makes it easy...