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}"/>




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.


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>();    

      for (String eachCountry : sCountry) {//loop for all regions
       Pagereference pdf = Page.MYrenderasPDFVFpage;
       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.

|improve this answer|||||
  • Option 2 will not work because PDF generation process will be done using Apex Scheduler. Regarding Option 1, If I send location as parameter to the VF then i will be able to send only one location. Can you please explain in code by sending multiple location and generate pdf. – user51525 Feb 22 '18 at 4:53
  • updated code for 2 – RedDevil Feb 22 '18 at 7:09
  • Now, In VF page you added region as parameter. I thought we can only put Id in it. If you don't mind then can you add the visualforce part as well (i.e. capturing the region parameter in vf page) – user51525 Feb 22 '18 at 8:25
  • Check this out. you would do this in the constructor of the VF render as pdf page controller salesforce.stackexchange.com/questions/69808/…. if you are still not able to do this i can add relevant code – RedDevil Feb 22 '18 at 8:32
  • I did what you mentioned but unfortunately the pdf is sent only once with one location only.I have updated the question and added my apex code. – user51525 Feb 22 '18 at 10:18

Your Answer

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