You need to sign in to do that
Don't have an account?
Lokesh Rayapati4
Styling the pdf
Hi,
I want to style my PDF. By using VF page I am able to get certain contact details through putting ID in url and able to edit and update and changed fields will reflect in salesforce org. After updating it will save as PDF in Notes & Attachments to that particular record. Vf page preview is like below.....
So, Now the problem is that I'm trying to change the style of PDF (that is saved to particular records) from below to look like the records fields in box style .As shown below pdf have no styling.
ShowContactDetail.vfp ========
<apex:page controller="UpdateContactDetail">
<style type="text/css">
#title {
font-size: 150%;
margin-left: 30%;
}
</style>
<h2 id="title">Contact</h2><br/><br/>
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!con}" var="b">
<apex:column headervalue="Title">
<apex:OutputText value="{!b.Title}" />
</apex:column>
<apex:column headervalue="First Name">
<apex:OutputText value="{!b.FirstName}" />
</apex:column>
<apex:column headervalue="Last Name">
<apex:OutputText value="{!b.LastName}" />
</apex:column>
<apex:column headervalue="Email">
<apex:inputText value="{!b.Email}" />
</apex:column>
<apex:column headervalue="Phone">
<apex:inputText value="{!b.Phone}" />
</apex:column>
<apex:column >
<apex:commandLink action="{!updateRecord}" value="Update"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
UpdateContactDetail.apxc =======
public class UpdateContactDetail {
public Contact con {get;set;}
public Id recId;
public UpdateContactDetail ()
{
recId = ApexPages.CurrentPage().getparameters().get('recordId');
getRecord();
}
public void getRecord() {
con = [select Id, Name,FirstName, LastName, Title, Email, Phone from contact where id =:recId];
}
public PageReference updateRecord(){
try{
update con;
}
catch(DmlException ex){
ApexPages.addMessages(ex);
}
return null;
}
}
GeneratePDFOfContactTrigger.apxt =======
trigger GeneratePDFOfContactTrigger on Contact (after update) {
GeneratePDFController.generateContactPDF(Trigger.new);
}
GeneratePDFController.apxc ==========
public class GeneratePDFController{
public static final String FORM_HTML_START = '<HTML><BODY>';
public static final String FORM_HTML_END = '</BODY></HTML>';
public static void generateContactPDF(list<contact> contactList){
String pdfContent = '' + FORM_HTML_START;
for(contact con : contactList){
try
{
pdfContent = '' + FORM_HTML_START;
pdfContent = pdfContent + '<H2>Contact Information</H2>';
//Dynamically grab all the fields to store in the PDF
Map<String, Schema.SObjectType> sobjectSchemaMap = Schema.getGlobalDescribe();
Schema.DescribeSObjectResult objDescribe = sobjectSchemaMap.get('Contact').getDescribe();
Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
//Append each Field to the PDF
for(Schema.SObjectField fieldDef : fieldMap.values()){
Schema.Describefieldresult fieldDescResult = fieldDef.getDescribe();
String name = fieldDescResult.getName();
if(name == 'Title' || name == 'FirstName' || name == 'LastName' || name == 'Email' || name == 'Phone'){
pdfContent = pdfContent + '<P>' + name + ': ' + con.get(name) + '</P>';
}
}
pdfContent = pdfContent + FORM_HTML_END;
}catch(Exception e){
pdfContent = '' + FORM_HTML_START;
pdfContent = pdfContent + '<P>THERE WAS AN ERROR GENERATING PDF: ' + e.getMessage() + '</P>';
pdfContent = pdfContent + FORM_HTML_END;
}
attachPDF(con,pdfContent);
}
}
public static void attachPDF(Contact con, String pdfContent){
try{
Attachment attachmentPDF = new Attachment();
attachmentPDF.parentId = con.Id;
attachmentPDF.Name = con.FirstName+' '+con.LastName+ '.pdf';
attachmentPDF.body = Blob.toPDF(pdfContent); //This creates the PDF content
insert attachmentPDF;
}catch(Exception e){
con.addError(e.getMessage());
}
}
}
Thank you in advance :)
I want to style my PDF. By using VF page I am able to get certain contact details through putting ID in url and able to edit and update and changed fields will reflect in salesforce org. After updating it will save as PDF in Notes & Attachments to that particular record. Vf page preview is like below.....
So, Now the problem is that I'm trying to change the style of PDF (that is saved to particular records) from below to look like the records fields in box style .As shown below pdf have no styling.
ShowContactDetail.vfp ========
<apex:page controller="UpdateContactDetail">
<style type="text/css">
#title {
font-size: 150%;
margin-left: 30%;
}
</style>
<h2 id="title">Contact</h2><br/><br/>
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!con}" var="b">
<apex:column headervalue="Title">
<apex:OutputText value="{!b.Title}" />
</apex:column>
<apex:column headervalue="First Name">
<apex:OutputText value="{!b.FirstName}" />
</apex:column>
<apex:column headervalue="Last Name">
<apex:OutputText value="{!b.LastName}" />
</apex:column>
<apex:column headervalue="Email">
<apex:inputText value="{!b.Email}" />
</apex:column>
<apex:column headervalue="Phone">
<apex:inputText value="{!b.Phone}" />
</apex:column>
<apex:column >
<apex:commandLink action="{!updateRecord}" value="Update"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
UpdateContactDetail.apxc =======
public class UpdateContactDetail {
public Contact con {get;set;}
public Id recId;
public UpdateContactDetail ()
{
recId = ApexPages.CurrentPage().getparameters().get('recordId');
getRecord();
}
public void getRecord() {
con = [select Id, Name,FirstName, LastName, Title, Email, Phone from contact where id =:recId];
}
public PageReference updateRecord(){
try{
update con;
}
catch(DmlException ex){
ApexPages.addMessages(ex);
}
return null;
}
}
GeneratePDFOfContactTrigger.apxt =======
trigger GeneratePDFOfContactTrigger on Contact (after update) {
GeneratePDFController.generateContactPDF(Trigger.new);
}
GeneratePDFController.apxc ==========
public class GeneratePDFController{
public static final String FORM_HTML_START = '<HTML><BODY>';
public static final String FORM_HTML_END = '</BODY></HTML>';
public static void generateContactPDF(list<contact> contactList){
String pdfContent = '' + FORM_HTML_START;
for(contact con : contactList){
try
{
pdfContent = '' + FORM_HTML_START;
pdfContent = pdfContent + '<H2>Contact Information</H2>';
//Dynamically grab all the fields to store in the PDF
Map<String, Schema.SObjectType> sobjectSchemaMap = Schema.getGlobalDescribe();
Schema.DescribeSObjectResult objDescribe = sobjectSchemaMap.get('Contact').getDescribe();
Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
//Append each Field to the PDF
for(Schema.SObjectField fieldDef : fieldMap.values()){
Schema.Describefieldresult fieldDescResult = fieldDef.getDescribe();
String name = fieldDescResult.getName();
if(name == 'Title' || name == 'FirstName' || name == 'LastName' || name == 'Email' || name == 'Phone'){
pdfContent = pdfContent + '<P>' + name + ': ' + con.get(name) + '</P>';
}
}
pdfContent = pdfContent + FORM_HTML_END;
}catch(Exception e){
pdfContent = '' + FORM_HTML_START;
pdfContent = pdfContent + '<P>THERE WAS AN ERROR GENERATING PDF: ' + e.getMessage() + '</P>';
pdfContent = pdfContent + FORM_HTML_END;
}
attachPDF(con,pdfContent);
}
}
public static void attachPDF(Contact con, String pdfContent){
try{
Attachment attachmentPDF = new Attachment();
attachmentPDF.parentId = con.Id;
attachmentPDF.Name = con.FirstName+' '+con.LastName+ '.pdf';
attachmentPDF.body = Blob.toPDF(pdfContent); //This creates the PDF content
insert attachmentPDF;
}catch(Exception e){
con.addError(e.getMessage());
}
}
}
Thank you in advance :)
Hi Lokesh,
Please add some CSS here in your code '
pdfContent = pdfContent + '<P>' + name + ': ' + con.get(name) + '</P>';
You can try below css if you want :
pdfContent = pdfContent + '<P style ="border:1px solid lightgrey; padding :5px 10px; margin :5px;">' + name + ': ' + con.get(name) + '</P>';
Please mark it as the best answer if it resolves your issue.
Thanks.