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
Sample URL
https://blogforce9.ap1.visual.force.com/apex/PDFAttacher?pdfSrcUrl=https://blogforce9.ap1.visual.force.com/apex/TestPageB&pid={!Account.Id}&pdfName=PDFAttacherTest.pdf&retUrl=/{!Account.Id}
- 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}
Code
<apex:page >
<apex:includeScript value="../../soap/ajax/29.0/connection.js" />
<script>
/*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]);
if(!result[0].getBoolean("success")){
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.readAsDataURL(this.response);
reader.onloadend = function() {
base64data = reader.result.replace('data:application/pdf;base64,','');
if(base64data.indexOf('text/html') == -1){
uploadAttachment(base64data,'{!$CurrentPage.parameters.pdfName}','application/pdf','{!$CurrentPage.parameters.pid}');
}
else{
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.responseType='blob'
oReq.open("get", "{!$CurrentPage.parameters.pdfSrcUrl}", true);
oReq.send();
}
</script>
<apex:outputPanel rendered="{!($CurrentPage.parameters.pdfSrcUrl != NULL && $CurrentPage.parameters.pid != NULL && $CurrentPage.parameters.pdfName != NULL)}">
<script>
loadPDF();
</script>
</apex:outputPanel>
</apex:page>
Project & Managed Package Installation : http://blogforce9dev-developer-edition.ap1.force.com/ProjectDetail?id=a029000000DrPpO
No comments:
Post a Comment