PDFAttacher - Attach Visualforce PDF Using Ajax Toolkit

Working with VisualForce and JS always fascinates me, I am constantly trying to do things a bit differently, trying to use more JS on my pages. And here is something from the same stable.

PDFAttacher - What it is ?

Its a VF page that is capable to pull PDF from another VF page (rendered as PDF) and attach the same to a record as an attachment.

Whats so special about this ?

Well pulling PDF from a VF source and attaching to a record can easily be done using apex, but PDFAttacher on the other side doesn't uses apex and solely depends upon JS and AJAX Toolkit.
  • No Apex Solution : Makes it possible to run in orgs that doesn't have access to APEX but they have access to API and VF. Its really very helpful for those orgs as it gives you ability to easily attach a Dynamically generated PDF.
  • Configurable & Reusable : Page run on some of the easily configurable parameters, that can be changed to attach attachments to different Parent Records and even Objects.

URL Parameters

  • pdfSrcUrl : VF page url from where the PDF needs to be extracted
  • pid : Parent record Id where the attachment needs to be attached
  • pdfName : Filename to be used to save the PDF
  • retUrl : URL where browser needs to be redirected on successful generation of PDF

Other Configurations 

  • Make sure Developer Footer is off.
  • Make sure the Source VF page is rendered as PDF.
  • API access is needed

How to use ?

Create a Button on a Object and set the URL as described in the "URL Parameters" section.

Sample URL 


  • Source PDF is : https://blogforce9.ap1.visual.force.com/apex/TestPageB
  • Parent Record Id : {!Account.Id}
  • Generated File Name : PDFAttacherTest.pdf
  • Return URL : /{!Account.Id}


<apex:page >  
   <apex:includeScript value="../../soap/ajax/29.0/connection.js" />  
     /*Mehtod to upload attachment to a parent record*/  
     function uploadAttachment(filecontent, filename, filetype, parentId) {  
       var attachment     = new sforce.SObject('Attachment');  
       attachment.Name    = filename;  
       attachment.IsPrivate  = false;  
       attachment.ContentType = filetype;  
       attachment.Body    = filecontent;  
       attachment.Description = filename;  
       attachment.ParentId  = parentId;  
       var result = sforce.connection.create([attachment]);  
         alert('PDF download failed.');  
       else if({!$CurrentPage.parameters.retUrl != NULL}){  
         window.location.href = '/{!$CurrentPage.parameters.retUrl}';  
     /*Method to convert the attachment to base64 and attach the same to a record*/  
     function reqListener () {  
       var reader = new window.FileReader();  
        reader.onloadend = function() {  
               base64data = reader.result.replace('data:application/pdf;base64,','');   
               if(base64data.indexOf('text/html') == -1){        
                 alert('Please check the PDF source URL.');  
     /*Method to request the PDF and retreive the BLOB*/  
     function loadPDF(){  
       sforce.connection.sessionId = '{!$Api.Session_ID}';  
       var oReq = new XMLHttpRequest();  
       oReq.onload = reqListener;  
       oReq.open("get", "{!$CurrentPage.parameters.pdfSrcUrl}", true);  
   <apex:outputPanel rendered="{!($CurrentPage.parameters.pdfSrcUrl != NULL && $CurrentPage.parameters.pid != NULL && $CurrentPage.parameters.pdfName != NULL)}">  

No comments:

Post a Comment