[SalesForce] Get Multiple pdf from a single visualforce page

I have a requirement where i have to generate multiple pdf file based upon the value of a field called Location.

If the Location is India, then i get all the records whose location is india and make a list from it which is then sent to visualforce page. The visualforce page is rendered as pdf. So, I get a Pdf.

Now, there will be other record as well whose location is some other value like USA, Demark, Norway, China e.t.c.
For all these location i have to generate other pdf files as well. How do i achieve this?

My visualforce page is something like this

<apex:page controller="MyController"  renderAs="pdf">
  <apex:form >

        <apex:pageBlock >
            <apex:pageBlockTable value="{!recordList}" var="record">
            <apex:column value="{!record.Name}"/>

            </apex:pageBlockTable>

        </apex:pageBlock>

  </apex:form>
</apex:page>

In the visualforce page i get a list of records and i am printing the name of each record.

public without sharing class MyController {
    List<SomeReport> reportList;

    public List<SomeReport> getReportList() {
        equipmentList = [SELECT Id, Name FROM SomeReport];
        return equipmentList;
    }
}

//Location is a field in the object SomeReport

In the apex i am sending all the record. Now i want to create a list of record with multiple locations and create multiple pdf. How do i achieve this?

public void UpdateContents() 
{
    String[] siteList = new String[]{'India','USA'};
        for(String siteValue: siteList) {
            try {
                PageReference pageRef = Page.InvoicePage;
                pageRef.getParameters().put('siteName', 'Kathmandu');

                PageContents = pageRef.getContent().toString().replace
                ('<html style="display:none !important;">', '<html>');
            } catch(exception ex) { 
                System.debug('Inside catch block');
                PageContents = 'An error has occurred while trying to generate this invoice.  Please contact customer service.';
            }
        }
}

I did not use blob as i don't know about it.

Best Answer

Options 1:

You can do it in apex.

create a Visualforce page with renderas='PDF' attribute.

You can then send location as parameter to the VF page and then generate the pdf multiple times in apex for different regions.

Now eventhough you generate multiple pdf's you will most probably show a list where all pdf's are shown and users can click and see/download.

what you could do is to insert all the different pdf's you are generating under case or whatever your parent object as attachments and expose the attachment/file list in UI.

Option 2:

Show a list in UI where it shows different PDF links to user only different being the region parameter and when they click that open the VF page(pdf) one for the region the user selected.

Something like please click below links to see sales information for different regions

  1. USA
  2. Canada
  3. India

These would be links which will open the VF page with respective region names as URL page parameters.

For Illustration:

Lets say you want to generate PDF and send it out or save it for 3 countries.

      List<String> sCountry = new List<String>();    
      sCountry.add('USA');
      sCountry.add('canada');
      sCountry.add('India');

      for (String eachCountry : sCountry) {//loop for all regions
       Pagereference pdf = Page.MYrenderasPDFVFpage;
       pdf.getParameters().put('region',eachCountry);
       Blob body;
       body = pdf.getContentAsPDF();//gives your PDF
      //your other code goes here
}

Be aware that you have to do all callouts if you are going to before any DML or else you will get uncommitted pending work error.

If you are running this for lot of records consider heap size limitations as well.

Related Topic