Posted By

Xhitman on 08/19/10


Tagged

pdf Apex salesforce Visualforce


Versions (?)

Visualforce and Apex to Populate PDF Forms


 / Published in: Other
 

URL: http://blog.sforce.com/sforce/2008/12/use-visualforce-and-apex-to-populate-pdf-forms.html

Credits to Jesse Lorenz http://wiki.developerforce.com/index.php/Adobe_XFDF

  1. private String getXmlString(Contact c)
  2. {
  3. String s = '<?xml version="1.0" encoding="UTF-8"?>' +
  4. '<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">' +
  5. '<f href="http://mysites-developer-edition.na1.force.com/resource/0/myform.pdf"/>' +
  6. '<fields>' +
  7. '<field name="DOB"><value>' + c.birthdate + '</value></field>' +
  8. '<field name="Name"><value>' + c.firstname + ' ' + c.lastname + '</value></field>' +
  9. '<field name="SS"><value>' + c.SS_Number__c + '</value></field>' +
  10. '<field name="Phone"><value>' + c.phone + '</value></field>' +
  11. '<field name="Citizenship"><value>' + c.Citizenship__c + '</value></field>' +
  12. '<field name="CountryOfBirth"><value>' + c.Country_of_Birth__c + '</value></field>' +
  13. '<field name="Tax"><value>' + c.Country_of_Tax_Residence__c + '</value></field>' +
  14. '<field name="GovId"><value>' + c.Government_Identification__c + '</value></field>' +
  15. '<field name="IdNumber"><value>' + c.ID_Number__c + '</value></field>' +
  16. '</fields><ids original="FEBDB19E0CD32274C16CE13DCF244AD2" modified="5BE74DD4F607B7409DC03D600E466E12"/>' +
  17. '</xfdf>';
  18.  
  19. return s;
  20. }
  21.  
  22.  
  23.  
  24. public PageReference XFDFInit() {
  25. Contact c = [SELECT Id, firstname, lastname, SS_Number__c, birthdate,
  26. phone, Citizenship__c,
  27. Country_of_Birth__c, Country_of_Tax_Residence__c,
  28. Government_Identification__c, ID_Number__c,
  29. Account.Name, Account.BillingStreet, Account.BillingCity,
  30. Account.BillingState, Account.BillingPostalCode
  31. FROM Contact
  32. WHERE id = :ApexPages.currentPage().getParameters().get('id')];
  33.  
  34. String xmlContent = getXmlString(c);
  35.  
  36. Attachment attachment = new Attachment();
  37. attachment.Body = Blob.valueOf(xmlContent);
  38. attachment.Name = c.lastname + c.firstname + '.XFDF';
  39. attachment.ParentId = c.Id;
  40.  
  41. insert attachment;
  42.  
  43. PageReference contactPage = new PageReference('/' + c.id);
  44. contactPage.setRedirect(true);
  45. return contactPage;
  46. }
  47.  
  48.  
  49.  
  50. <apex:page showHeader="true"
  51. controller="myXFDFController"
  52. action="{!XFDFInit}"
  53. showHeader="false">
  54. </apex:page>

Report this snippet  

You need to login to post a comment.