function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sandy Comeau 5Sandy Comeau 5 

Create PDF on Custom Object

I do not have any coding experience but hoping someone can help me write the code to Print to PDF from a Custom Object.  We have a Custom Object called "Executive Summary" and I would like to print to PDF when a button is clicked.  I was able to create the button but I can not get it to do anything.  Help Please?  Below is a Screen Shot of my custom object.
User-added image
Ryan GreeneRyan Greene
PDF pages are easy!

Create a VF Page as below. The one I have below is from a basic page that shows some Contact info. Where you want fields populated on your PDF insert {!sObject.FieldName}. Below I have it pulling from a Contact record. If you need it on a different object replace "Contact" with your object. For the button have it pointed to the VF Page name.

Also, you need to insert HTML if you want the PDF to render with styling. I use https://wordtohtml.net/. I'll create what I want the PDF to look like in Word, then copy paste it to that site to grab the HTML. Then just paste the HTML inbetween the <html> tags on the VF Page.
 
<apex:page standardController="Contact" renderAs="pdf">
<html>
   
<p style="text-align: center;"><strong><span style="font-size: 16.0pt; line-height: 107%; font-family: 'Arial','sans-serif';">Owner Information</span></strong></p>
<p><strong><span style="font-size: 12.0pt; line-height: 107%; font-family: 'Arial','sans-serif';">&nbsp;</span></strong></p>
<p style="line-height:0;"><span style="font-family: aril, helvetica, sans-serif; font-size: 12pt;">First Name: {!Contact.FirstName}&nbsp;&nbsp; Last Name: {!Contact.LastName}</span></p>
<p style="line-height:0;"><span style="font-family: aril, helvetica, sans-serif; font-size: 12pt;">Address: {!Contact.MailingStreet}</span></p>
<p style="line-height:0;"><span style="font-family: aril, helvetica, sans-serif; font-size: 12pt;">City: {!Contact.MailingCity}&nbsp;&nbsp; State: {!Contact.MailingState}&nbsp;&nbsp; Zip: {!Contact.MailingPostalCode}</span></p>
<p style="line-height:0;"><span style="font-family: aril, helvetica, sans-serif; font-size: 12pt;">Phone: {!Contact.Phone}</span></p>
<p style="line-height:0;"><span style="font-family: aril, helvetica, sans-serif; font-size: 12pt;">Date of birth: </span></p>
<p style="line-height:0;"><span style="font-family: aril, helvetica, sans-serif; font-size: 12pt;">Social Security Number: </span></p>

</html>
</apex:page>

 
Carissa CrittendenCarissa Crittenden
I'm trying to build a VF PDF from a custom object also - Is there a way to also pull in information from the child records associated with my custom object?
Ryan GreeneRyan Greene
Yes. However, you mentioned you do not have coding experience, this could be tricky for you. I provided a template below of an Apex Class you will need to create. You will also need to create very specific SOQL quries to make sure the one and only correct record is accessed. I provided some direction in line in comments. Let me know if you have any questions.
Sample Apex Class:
public class ProposalEC {
    //this first section is for the VF page 'Proposal' to pull the needed information from the Lead, Component, and User objects
    //Each List "gets" the records based on the query below, and "sets" the information for use from the VF Page
    public List<Lead> Ld {get;set;}
    public List<Component__c> Com {get;set;}
    public List<User> us {get;set;}
    //gets the Id of the current record
    public Id getid {get;set;}{
        getid = ApexPages.currentPage().getParameters().get('id');}    
    //method to get the proper records and all needed fields for the VF Page
    public ProposalEC(){
    //SOQL Queries here. Add the fields you need to pull to the VF Page
        Ld = [SELECT Id,City__c,State_Company__c,Zip_Code__c, FirstName, LastName FROM Lead WHERE Id =: getid];
    //In this example the Lead is the Parent object and Component__c and User are the Children. To pull the correct related record you must declare it in the query i.e. "Ld[0].Id" and "Ld[0].OwnerId"
    //The [0] indicates it is the first Lead record it finds, if you leave out the [0] there may be an error
        Com = [SELECT Id, Name FROM Component__c WHERE RecordTypeId = '0123F0000008XdC' AND Related_Lead__c =: Ld[0].Id];
        us = [SELECT FirstName, LastName FROM User WHERE Id =: Ld[0].OwnerId];
    }
}
//Custom code by Ryan Greene ryan.greene@outlook.com
Sample VF Page:
<apex:page controller="ProposalEC" renderAs="pdf">
    <!-- Adds the image from a Static Resource -->
    <apex:image url="{!$Resource.Logo}" style="left;" width="15%" height="15%"/>
    <html>
        <br/>
    </html>
    <!-- Section rendered only if Entity field equals Company LLC - Use API names to render -->
    <apex:outputPanel id="CO" rendered="{!Ld[0].Entity__c == 'CO'}">  
        <html>
            Company LLC
        </html>
    </apex:outputPanel>
    <!-- Section rendered only if Entity field equals Company Group - Use API names to render -->
    <apex:outputPanel id="CO Group" rendered="{!Ld[0].Entity__c == 'CO Group'}">  
        <html>
            CO Group
        </html>
    </apex:outputPanel>
    <apex:outputText value="{0,date,MMMM dd, yyyy}">
        <apex:param value="{!TODAY()}" />
    </apex:outputText>
    <html>
    <!-- Continue adding whatever other variables or HTML you need -->
        For: {!Ld[0].Company}<br/>
        {!Ld[0].Street__c}<br/>
        {!Ld[0].City__c}, {!Ld[0].State_Company__c} {!Ld[0].Zip_Code__c}<br/>          
        {!Com[0].Name}<br/><br/><br/><br/>
        {!Ld[0].FirstName},<br/><br/>
        {!us[0].FirstName} {!us[0].LastName}<br/>
    </html>
</apex:page>
<!-- Custom coding by Ryan Greene ryan.greene@outlook.com -->





 
RainerRichterRainerRichter
Hi Ryan, I'm in search for a solution, how I could skip a column in a table of a related list, if all values in this list are 0,-- or NULL. Would you have a head up for this? Thanks Rainer