I have been trying to figure out how to fill a PDF form with contact data and I found a helpful post here. I followed the instructions but it didn't say what to do with the code or how to set up the button. I tried putting the first two blocks of code in an apex class and then the third as a Visualforce page. I then tried to add a button on the contact to pull up that page but it doesn't let me choose the page.
Here is my Apex Class.
public class myXFDFController {
Private String getXmlString(Contact c) {
String s = '<?xml version="1.0" encoding="UTF-8"?>' +
'<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">' +
'<f href="https://www.riverblufftech.com/wp-content/uploads/2015/11/myForm.pdf"/>' +
'<fields>' +
'<field name="First Name, Middle Name, Last Name & Suffix"><value>' + c.firstname + ' ' + c.lastname + '</value></field>' +
'<field name="Email address"><value>' + c.Email + '</value></field>' +
'</fields><ids original="E1D407DB3A404A389E73EFCE11A5CE0B" modified="46721BE9257A47A3A1F3CCEE45E9F19A"/>' +
'</xfdf>';
return s;
}
public PageReference XFDFInit() {
Contact c = [SELECT Id, firstname, lastname, email FROM Contact WHERE id = :ApexPages.currentPage().getParameters().get('id')];
String xmlContent = getXmlString(c);
Attachment attachment = new Attachment();
attachment.Body = Blob.valueOf(xmlContent);
attachment.Name = c.lastname + c.firstname + '.XFDF';
attachment.ParentId = c.Id;
insert attachment;
PageReference contactPage = new PageReference('/' + c.id);
contactPage.setRedirect(true);
return contactPage;
}}
And this is my Visualforce Page, I feel like there should be more here but the post I linked to earlier seemed to say you only need this.
<apex:page showHeader="true" controller="myXFDFController" action="{!XFDFInit}"></apex:page>