1

I have a requirement to create separate pdf files with informations of HCPs having been called where samples are dropped. I can extract in csv, but how to do that in a presentable format in pdf? Please let me know if an apex class can be written for this. Please find my apex page here.

<apex:page controller="AccountPDF" setup="true" sidebar="false" showheader="false" renderas="pdf">

<head>
<style type="text/css" media="print">
    @page
    {
    @top-center
    {
    content: element(header);
    }
    @bottom-left
    {
    content: element(footer);
    }

    }

div.header
{
padding: 10px;
position: running(header);
}

div.footer
{
display: block;
padding: 5px;
position: running(footer);
}

.pagenumber:before
{
content: counter(page);
}

.pagecount:before
{
content: counter(pages);
}

</style>
</head>

<div class="header">
<div> <center><i> Health Care Persons.</i></center></div>
</div>

<div class="footer">
    <div align="center">
   Otsuka Pharmaceutical Group.
    </div>

    <div align="left">
    Page <span class="pagenumber"/> of <span class="pagecount"/>
    </div>
</div>

<apex:pageBlock title="hcps)"> {!NOW()}
         <TABLE width="100%">
                <TR style="background-color:#F5F7FF; font-family: sans-serif;font-size:12px">
                    <TH>First Name</TH>
                    <TH>Last Name</TH>
                     <TH>Name</TH>
                    <TH>Call Name</TH>
                    <TH>Call Date</TH>
                    <TH>Call Created By</TH>                                   
                </TR>
                <apex:repeat value="{!m1}" >
                    <TR style="height:40px; background-color:#D5DDF4; font-family: sans-serif;">
                        <TD wrap="true" colspan="3"  style="color:blue;padding-top: 3px;padding-left: 5px;font-size:12px"><B>{!m1[Name]}</B> <!--<a href="/{!m1[ID]}"></a>-->

                        </TD>
                        <TD style="color:blue;font-size:12px">

                        </TD>

                        <TD style="color:blue;font-size:12px" colspan="6">

                        </TD>
                    </TR>                   
                    <apex:repeat value="{!m1.Call2_vod__r}">
                        <TR style="height:20px; background-color:#F5F7FF; font-family: sans-serif;font-size:11px">
                            <TD wrap="true">{!m1[FirstName]}</TD>
                            <TD wrap="true">{!m1[LastName]}</TD>
                            <TD wrap="true">{!m1[Name]}</TD>
                            <TD wrap="true">{!m1[Call2_vod__r.Name]}</TD>
                            <TD wrap="true">{!m1[Call2_vod__r.call_Date_vod__c]}</TD>
                            <TD wrap="true">{!m1[Call2_vod__r.Createdby.Name]}</TD>                                                                                                       
                        </TR>                      
                    </apex:repeat>
                </apex:repeat>
            </TABLE>       
    </apex:pageBlock>
</apex:page>

The class from where refering as below.

public class AccountPDF 
{   Public void AccountPDF(){}
    Public Set<ID> s = new Set<ID>();
    Public String query;

    public Map<ID,Call2_vod__c> getm1()
    {  for(Call2_vod__c c:[SELECT Account_vod__c FROM Call2_vod__c where Is_Sampled_Call_vod__c =true limit 10 offset 11])
     {s.add(c.Account_vod__c);
      }
        //List<Call2_vod__c > m1 = [SELECT Account_vod__r.FirstName,Account_vod__r.Lastname,Account_vod__r.Name,Name,Call_datetime_vod__c,Createdby.Username FROM Call2_vod__c where Is_Sampled_Call_vod__c =true order by Account_vod__c];

      //Set<ID> L1=[SELECT Account_vod__c FROM Call2_vod__c where Is_Sampled_Call_vod__c =true];
      Map<ID,Call2_vod__c> m1= new Map <ID,Call2_vod__c>([SELECT Account_vod__r.FirstName,Account_vod__r.Lastname,Account_vod__r.Name,Name,Call_datetime_vod__c,Createdby.Username FROM Call2_vod__c where Is_Sampled_Call_vod__c =true and Account_vod__c in :s ]);
      return m1;
    }

 }

Issue: Can't find the records in apex page

4

Edit, see the history for previous version

So, you want a method to generate multiple PDFs, 1 for each Account. This pretty much rules out a 1-page solution as it'll be capable of making 1 collated PDF at best.

I think you should start with focusing on single Account, making single page work perfectly:

public AccountVodPdf{
    Id accId;
    public AccountVodPdf(ApexPages.standardController sc){
        accId = sc.getId();
    }

    public List<Call2_vod__c> getVods(){
        return [SELECT Name, Call_datetime_vod__c,Createdby.Username
            FROM Call2_vod__c
            WHERE Account_vod__c = :accId AND Is_Sampled_Call_vod__c =true
            LIMIT 10];
    }
}

<apex:page standardController="Account" extensions="AccountVodPdf" readonly="true" renderAs="pdf">
{!Account.Name}
    <apex:repeat value="{!vods}" var="v">
        {!v.Name}
    </apex:repeat>
</apex:page>

Once you're happy with the content of that one page you can think about how you'd like to reuse it:

  • create 1 master page with list of accounts and links that point to this helper page?
  • generate the content in apex and save as attachment under every account?
  • create an email template with attachment(s)?

If all you want is to generate download links - just query for accounts on another page and display links similar to /apex/AccountVodPdf?id=someAccountId.

If you're interested in version 2 - read about Page.getContentAsPdf() and inserting to Attachment table. Jeff Douglas' post should give you some ideas how to start. There are some limitations but the main thing is to get the "helper" right, then you have plenty of options where to reuse it.

For version 3 you'd need a Visualforce Email Template or you could look into hand-crafting the message yourself, using combination of getContentAsPdf and setFileAttachment

  • Thanks eyescream. I want to present the query results in a tabular format in pdf. Records having same Account IDs will remain in same pdf. Please help me here. My Apex page can not receive data from controller. – Testing_SFDC Aug 8 '14 at 3:10
  • Do you want 1 or multiple files? How do you imagine this will work? You access a page = it will be displayed as one collated pdf at best. Maybe A list of links to click would work for you? 1 link / Account and this would generate 1 PDF each? Or maybe you want to save the PDFs as attachments under account? (alternatively save them to Documents or Chatter files). Or do you want email template with multiple attachments? And I'd say your page does receive this data, you're just not iterating over it correctly (as I wrote - if you use a map - you need a helper Set<Id> to iterate over it instead). – eyescream Aug 8 '14 at 5:49
  • Thanks. I need multiple PDFs. I don't even need a page. I need those PDFs generated. One PDF file having all records corresponding to one Account.Can you please help me here? – Testing_SFDC Aug 8 '14 at 7:19
2

You can create Visualforce pages which are rendered as pdf. There are different ways to use these, but your question isn't clear enough to really understand what you need.

<apex:page standardController="Account" renderAs="pdf" applyBodyTag="false">

I suggest you get started with trying to render a pdf from visualforce, and than come back with more specific questions on what you're trying to do. Have a look at the documentation about this

  • can you answer for this question salesforce.stackexchange.com/questions/46217/… – SFDC LEARNER Aug 7 '14 at 8:36
  • Thanks Samuel. I have to create separate PDF files for different Accounts where header will be Account Names and there will be a table under it having records from a custom object Call whose parent is that Account. – Testing_SFDC Aug 7 '14 at 8:43
  • 2
    @SFDCLEARNER the question you stated is already answered. The limits of the salesforce PDF rederer are clearly described. That's it. If you need more sophisticated PDF rendering, you are on your own. – Uwe Heim Aug 7 '14 at 9:31
  • @Samuel Can you please help me here? I am not having any session ID of page reference just print records of an SOQL query result in pdf – Testing_SFDC Aug 7 '14 at 14:27
  • You'll have to add more context to your question, and or look up more about this subject in the documentation or other online resources. The more specific your question is on what you understand, have tried and are stuck with, the better people can help and answer. I'm not an expert on pdf generation myself either. – Samuel De Rycke Aug 7 '14 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.