1

I've been looking in many sites and I didn't find an answer. Is it possible to password protect a PDF generated with "renderAs = PDF"? In a negative case I need to find another way to protect my PDF document against copy. Can anyone helps me?

2
4

You can create a Username - Password step before allowing anyone to download your PDF. If you inform the user that he can download a PDF after inserting a Username and Password you can do something like this.

Put a link in your Visualforce page to access the PDF. When the user clicks on that link he will see a login page, if the username and password are correct you will redirect him to the page where the PDF is located or automatically start the download.

So, here is a small code that can help you with the access page. You can also use a checkbox, 'PDF Allow', on the User page layout to allow access to you PDF fiel/s. If the check is true you can redirect the user to the page with the PDF. For this, you need to elaborate your code a bit and make the correct changes, this code is a starting point.

So you can start with something like this:

public class accessPDFpage{

  public String password { get; set; }
  public String username { get; set; }

public PageReference registerNewUser() {
    PageReference newPage = new PageReference('/apex/newUserPage');
    newPage.setRedirect(true);
    return newPage;
  }
 }

This is the Visualforce code:

  <apex:page controller="accessPDFpage" showHeader="false" sidebar="false" standardStylesheets="true">
   <apex:Pagemessages id="msg"/>
       <apex:form >
            <apex:pageBlock title="PDF Access">
                   <p><b>Login Page</b><br /></p>
               <apex:panelGrid columns="1" style="margin-top:1em;">
                   <p><b>UserName</b><br />
                 <apex:inputText required="true" id="username" value="{!username}"/>
                 </p>
                   <p><b>Password</b><br />
                <apex:inputSecret id="password" value="{!password}"/>
                 </p>
         <apex:pageBlockSection >
    <apex:commandButton action="{!registerUser}" value="Save" id="save"/>
    <apex:commandButton action="{!registerUser}" value="Cancel" id="cancel"/>
       </apex:pageBlockSection>
         <apex:pageblockSection >
          <apex:commandButton action="{!registerUser}" value="Register" id="register" immediate="true"/>
   </apex:pageblockSection>
    </apex:panelGrid>
   </apex:pageBlock>
  </apex:form>
</apex:page>

Like you can see this is very simple code but you can do a lot of things. You can run an SOQL on the current user and if the user 'PDF Allow' checkbox is true, you can send the user to the PDF without inserting any Username or Password.

You can also allow the users to register or ask for access to the PDF.Is really up to you and to your business requirements. I hope this helps you to build a solution :)

2
  • 2
    Love the work you put into this answer. One point though, it does not prevent the PDF from being "shared" and thus access to those who should not have it. Understanding that the downloader could also share the password if set on the PDF but that would be their problem...
    – Eric
    Feb 19 '17 at 18:40
  • 1
    Thank you, Eric. Yes, you right, that will not stop from sharing the PDF with people that don't have access, but that will be practically impossible to do :( Feb 19 '17 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.